I currently try to insert items into the MemoryCache.Default instance, which works. But the items don't expire! Polling interval is on default setting 2 minutes, no specific settings have been done for MemoryCache.
Add item routine is like this:
string key = "someKey";
var faultyItem = GetFaultyItemToBlockFromProcessingTheNextThreeMinutes();
if (!MemoryCache.Default.Contains(key))
{
MemoryCache.Default.Add(
new CacheItem(key, faultyItem),
new CacheItemPolicy()
{
AbsoluteExpiration = DateTimeOffset.UtcNow.AddMinutes(3)
});
}
However, the items never expire in cache! I waited for more than 10 minutes, and they're still there in the cache. Main purpose is to prevent them from flooding the database with queries, by attempting to process them every few seconds. They are checked every couple of seconds, but don't have a SlidingExpiration. They should be checked again after roughly 3 minutes, 5 minutes as a max delay would still be tolerable.
I don't want to do any configuration outside code, in XML or such. If it's not possible, is there a good alternative to MemoryCache? Perhaps with numeric/object keys and configuration through parameters and properties?