我有以下代码来缓存一些昂贵的代码。
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?
这取决于您使用的特定缓存提供程序。例如,如果您正在使用默认的内存中缓存,如果服务器开始内存不足或应用程序池被回收,它可能会过期。但是,如果您使用其他一些缓存提供程序,例如像 memcached
或 AppFactory
这样的分布式缓存,这将取决于特定的实现。
经验法则是永远不要假设某些东西在缓存中,因为你以前存储过它。始终首先检查缓存中是否存在该项目,如果不存在,则获取它并再次存储在缓存中。
关于c# - 如果我不设置明确的过期时间,默认情况下东西会在 httpcache 中保留多长时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6533874/