MemoryCache : cached item does not expire correctly
I'm creat开发者_高级运维ing my caching service and use Memorycache to cache my data. Cached data is configure to expire 12 hours later. After an hour, my cached data cease to exist. IIS (7.0) Worker pool is configure to expire in 1740 minutes later. I'm caching about 200 M of data and each cache item is less than 100 bytes.
Anyone face this problem before or i might be doing something wrong here?
Here is the code that i used to instantiate my 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));
}
The time zone is UTC + 8:00 (KL, Singapore)
Not sure if this might be the case, but a couple of things:
var config = new NameValueCollection
Shouldn't that be:
var config = new NameValueCollection()
And shouldn't
var _cachePolicy = CacheItemPolicy
be:
var _cachePolicy = new CacheItemPolicy()
Also, I don't see where CacheDefaultTimeOut
is set, but I'm assuming it's set to 720 (12 hours) somewhere?
Like I said, not sure this is the problem since I would expect your code to not compile if it was, and it sounds like you've got it running. But sometimes it's the simple things...
精彩评论