c# - 如果我不设置明确的过期时间,默认情况下东西会在 httpcache 中保留多长时间?

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

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

  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;
   }

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

最佳答案

will this stay in the cache forever?

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

经验法则是永远不要假设某些东西在缓存中,因为你以前存储过它。始终首先检查缓存中是否存在该项目,如果不存在,则获取它并再次存储在缓存中。

关于c# - 如果我不设置明确的过期时间,默认情况下东西会在 httpcache 中保留多长时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6533874/

相关文章:

c# - 为什么在将新段落添加到文档 block 时运行的父级变为空?

c# - 从第二个列表初始化一个列表

java - 为什么 eclipse 或 cmd 控制台会缓存操作系统环境变量?

.net - 在 WPF 中缓存大量实体

c# - 如何在不使用测量工作室的情况下将模拟测量从 National Instruments USB 6008 获取到 C# 应用程序中?

c# - Web API 返回 JSON 但不返回 Xml

javascript - 如何使用 knockout 保留页面之间的值?

asp.net-mvc - ASP.NET MVC : using same ViewModel for rendering page and getting results

java - 并发HashMap : remove on condition

c# - 在 Windows 窗体中使用 OpenFileDialog 读取文本文件