【问题标题】:how long, by default does stuff stay in httpcache if i don't put an explicit expiration?如果我没有明确过期,默认情况下,东西会在 httpcache 中停留多长时间?
【发布时间】:2011-09-25 21:46:05
【问题描述】:

我有以下代码来缓存一些昂贵的代码。

  private MyViewModel GetVM(Params myParams)
    {
        string cacheKey = myParams.runDate.ToString();
        var cacheResults = HttpContext.Cache[cacheKey] as MyViewModel ;
        if (cacheResults == null)
        {
            cacheResults = RunExpensiveCodeToGenerateVM(myParams);
            HttpContext.Cache[cacheKey] = cacheResults;
        }                
   return cacheResults;
   }

这会永远留在缓存中吗?直到服务器重新启动或内存不足?

【问题讨论】:

    标签: c# asp.net-mvc caching httpcontext.cache


    【解决方案1】:

    这会永远留在缓存中吗?

    这取决于您使用的特定缓存提供程序。例如,如果您使用默认的内存缓存,则如果服务器开始内存不足或应用程序池被回收,它可能会过期。但是,如果您使用其他缓存提供程序,例如像 memcachedAppFactory 这样的分布式缓存,这将取决于特定的实现。

    经验法则是永远不要假设缓存中存在某些内容,因为您之前存储了它。始终首先检查缓存中是否存在该项目,如果不存在,则将其获取并再次存储在缓存中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-20
      • 1970-01-01
      • 1970-01-01
      • 2021-10-19
      • 1970-01-01
      • 2017-04-12
      • 2021-06-02
      • 1970-01-01
      相关资源
      最近更新 更多