MemoryCache:缓存的项目未正确到期

时间:2011-07-30 08:39:10

标签: c# .net caching

我正在创建我的缓存服务并使用Memorycache来缓存我的数据。缓存数据配置为在12小时后过期。一小时后,我的缓存数据不再存在。 IIS(7.0)工作池配置为在1740分钟后过期。我正在缓存大约200 M的数据,每个缓存项少于100个字节。

之前有人遇到过这个问题,或者我可能在这里做错了什么?

以下是我用来实例化MemoryCache的代码。

private const string ConstCacheMemoryLimitMegabytes = "cacheMemoryLimitMegabytes";
private const string ConstCacheMemoryLimitMegabytesValue = "2000"; 
private const int CacheDefaultTimeOut = 720; 

// Percentage of server memory to use 
private const string ConstPhysicalMemoryLimitPercentage = "physicalMemoryLimitPercentage";
private const string ConstPhysicalMemoryLimitPercentageValue = "100";

// The maximum time that can occur before memory statistics are updated.
private const string ConstPollingInterval = "pollingInterval";
private const string ConstPollingIntervalValue = "02:00:00";

var config = new NameValueCollection 
{
   { ConstCacheMemoryLimitMegabytes, ConstCacheMemoryLimitMegabytesValue },
   { ConstPhysicalMemoryLimitPercentage,  ConstPhysicalMemoryLimitPercentageValue },
   { ConstPollingInterval, ConstPollingIntervalValue}
}; 

var _cachePolicy = new CacheItemPolicy
{
    AbsoluteExpiration = ConvertIntToMinDateTimeOffSet(CacheDefaultTimeOut) // Setting this to expire 12 hours later.
};

_memoryCache = new MemoryCache("MyCustomCache", config);
var cacheItem = new CacheItem(key, item); 
_memoryCache.Add(cacheItem, _cachePolicy);


private static DateTimeOffset ConvertIntToMinDateTimeOffSet(int cacheExpiryIntervalInMinute)
{
    return new DateTimeOffset(DateTime.Now.AddMinutes(cacheExpiryIntervalInMinute));
}

时区为UTC + 8:00(吉隆坡,新加坡)

1 个答案:

答案 0 :(得分:0)

不确定是否会出现这种情况,但有几点:

var config = new NameValueCollection 

不应该是:

var config = new NameValueCollection()

并且不应该

var _cachePolicy = CacheItemPolicy

是:

var _cachePolicy = new CacheItemPolicy()

另外,我不知道CacheDefaultTimeOut设置在哪里,但我假设它在某处设置为720(12小时)?

就像我说的那样,不确定这是什么问题,因为我希望你的代码不能编译,如果它是,并且听起来你已经让它运行了。但有时它只是简单的事情......