DateTime expireWeights = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59, 999); Cache.Insert("CacheItemName", list, null, expireWeights, System.Web.Caching.Cache.NoSlidingExpiration);
您可以在 Cache 对象上设置一个 absoluteExpiration 时间,即 DateTime。
您还可以将 absoluteExpiration 与 SqlCacheDependency 结合使用。
关于缓存过期不拉取新数据的问题:你可以连接一个CacheItemRemovedCallback来接收过期通知,到时候刷新缓存。
或
在模型中
public class NoCache : ActionFilterAttribute
{
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
filterContext.HttpContext.Response.Cache.SetValidUntilExpires(false);
filterContext.HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
filterContext.HttpContext.Response.Cache.SetNoStore();
base.OnResultExecuting(filterContext);
}
}
控制器:-
[NoCache]
[Authorize]
public ActionResult Home()
{
////////...
}
您必须使用以下行代码设置到期时间:DateTime.UtcNow.AddMinutes(1350)。
否则 (1350*60)--> 每次动作调用时,持续时间和控制器都必须检查您的文件
[OutputCache(Duration = 81000, VaryByParam = "none")]
public ActionResult Index()
{
//reset cache
User u = new User();
return Content(u.getCountryDetails());
//send notification
}
或
您可以在控制器中向 HttpContext.Response 添加 Header
HttpContext.Response.Headers.Add("refresh", "1350; url=" + Url.Action("Index"));
您可以通过多种方式进行缓存,这取决于您的舒适度。
希望能帮到你。