【问题标题】:MemoryCache : cached item does not expire correctlyMemoryCache : 缓存项未正确过期
【发布时间】:2011-10-16 11:00:07
【问题描述】:

我正在创建我的缓存服务并使用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(吉隆坡、新加坡)

【问题讨论】:

  • 记录您居住的时区。

标签: c# .net caching


【解决方案1】:

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

var config = new NameValueCollection 

不应该是这样的:

var config = new NameValueCollection()

不应该

var _cachePolicy = CacheItemPolicy

是:

var _cachePolicy = new CacheItemPolicy()

另外,我看不到 CacheDefaultTimeOut 的设置位置,但我假设它在某处设置为 720(12 小时)?

就像我说的,不确定这是不是问题,因为我希望您的代码无法编译,如果它是的话,听起来您已经让它运行了。但有时它是简单的事情......

【讨论】:

    猜你喜欢
    • 2013-06-03
    • 2015-11-04
    • 2018-05-10
    • 1970-01-01
    • 2019-08-13
    • 2014-04-06
    • 2021-06-26
    • 1970-01-01
    • 2013-08-28
    相关资源
    最近更新 更多