【问题标题】:MVC clear cache after certain time of dayMVC 在一天中的某个时间后清除缓存
【发布时间】:2017-05-02 00:00:11
【问题描述】:

我有一个带有OutputCache 属性的控制器操作,如下所示:

[OutputCache(Duration = 14400)] // 4 hours
public ActionResult Index()
{
    var model = // fill out model

    return View(model);
}

所以我将动作缓存了 4 个小时。我想做的是不管这 4 个小时有多长,如果是在晚上 10pm 之后,我想重置/清除缓存。这可能吗?

【问题讨论】:

    标签: asp.net-mvc caching outputcache


    【解决方案1】:
    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"));
    

    您可以通过多种方式进行缓存,这取决于您的舒适度。

    希望能帮到你。

    【讨论】:

    • 我一直在努力让它发挥作用。我看不到第一行代码应该放在哪里。我喜欢 Attribute 方式,假设这是一个单独的解决方案?唯一的问题是我无法在一天中的某个时间重置它。不确定我是否清楚这一点。我希望缓存每隔 4 小时过期一次。除了一个例外,当时钟在晚上 10 点之后,我想使缓存过期并以 4 小时的间隔再次开始缓存。
    • 您可以创建类并能够访问应用程序中的任何位置。您正在使用相同的解决方案吗?如果不是,你保持相同的解决方案。
    • 好的。代码的哪一部分告诉缓存重新开始?当我尝试这个时,它只会在一段时间后完全停止缓存。
    【解决方案2】:

    您对问题的思考方式使其过于复杂。如果您想在晚上 10 点失效并缓存 4 小时,那么缓存的时间总是相同的,因此在晚上 10 点、凌晨 2 点、早上 6 点等开始新的缓存。

    我认为你可以这样实现它:

    public override string GetVaryByCustomString(HttpContext context, string custom)
    {
            if (!string.IsNullOrWhiteSpace(custom))
            {
                switch (custom)
                {
                    case "4HoursStart10":
                        var baseDate = DateTime.Now.AddHours(-2);
                        return "CacheKey-" + (baseDate.Hour / 4);
                        break;
                }
            }    
            return base.GetVaryByCustomString(context, custom);
    }
    

    这将每 4 小时为您提供一个新的缓存键:

    • 2017 年 9 月 7 日下午 6:00:40 的 CacheKey-4
    • 2017 年 9 月 7 日晚上 7:00:40 的 CacheKey-4
    • 2017 年 9 月 7 日晚上 8:00:40 的 CacheKey-4
    • 2017 年 9 月 7 日晚上 9:00:40 的 CacheKey-4
    • 2017 年 9 月 7 日晚上 10:00:40 的 CacheKey-5
    • 2017 年 9 月 7 日晚上 11:00:40 的 CacheKey-5
    • 2017 年 9 月 8 日凌晨 12:00:40 的 CacheKey-5
    • 2017 年 9 月 8 日凌晨 1:00:40 的 CacheKey-5
    • 2017 年 9 月 8 日凌晨 2:00:40 的 CacheKey-0
    • 2017 年 9 月 8 日凌晨 3:00:40 的 CacheKey-0
    • 2017 年 9 月 8 日凌晨 4:00:40 的 CacheKey-0
    • 2017 年 9 月 8 日凌晨 5:00:40 的 CacheKey-0
    • 2017 年 9 月 8 日上午 6:00:40 的 CacheKey-1
    • 2017 年 9 月 8 日上午 7:00:40 的 CacheKey-1
    • 2017 年 9 月 8 日上午 8:00:40 的 CacheKey-1
    • 2017 年 9 月 8 日上午 9:00:40 的 CacheKey-1
    • 2017 年 9 月 8 日上午 10:00:40 的 CacheKey-2
    • 2017 年 9 月 8 日上午 11:00:40 的 CacheKey-2
    • 2017 年 9 月 8 日下午 12:00:40 的 CacheKey-2
    • 2017 年 9 月 8 日下午 1:00:40 的 CacheKey-2
    • 2017 年 9 月 8 日下午 2:00:40 的 CacheKey-3
    • 2017 年 9 月 8 日下午 3:00:40 的 CacheKey-3
    • 2017 年 9 月 8 日下午 4:00:40 的 CacheKey-3
    • 2017 年 9 月 8 日下午 5:00:40 的 CacheKey-3

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-28
      • 1970-01-01
      • 1970-01-01
      • 2017-07-21
      相关资源
      最近更新 更多