System.Runtime.Caching Absolute Expiration evicts data as designed, but how to implement a non-evicting expiring data strategy?

StackOverflow https://stackoverflow.com/questions/9476775

سؤال

I am using the System.Runtime.Caching MemoryCache in my MVC3 application to cache seldom changed, but often accessed data. I have set an absoluteExpiration of 30 minutes. This works great. Every 30 minutes, the application expires the data in the cache, then evicts the data.

The next time the data is queried, the system reloads the cache from the database.

However, what if the database was temporarily unavailable after the cache is evicted? This would result in no records available either from the MemoryCache or the database. It would be ideal to expire the cache, but NOT evict the data until I was sure I could reload it. If I am unable to reload the data from the database, I would want to extend the expiration of the cache by say...5 minutes or so and have it try again. Eventually the database will come back.

The strategy being that I would rather have slightly stale data than no data at all.

My question is how should I go about creating such a caching strategy using System.Runtime.Caching. I don't see an event that allows me to prevent eviction at the time of expiration. Any ideas?

هل كانت مفيدة؟

المحلول

I think you need to implement your own OutputCacheProvider, that will let you control exactly what happens when it's time to remove the item from the cache:

http://msdn.microsoft.com/en-us/library/system.web.caching.outputcacheprovider.aspx

You could also consider the changing strategies and looking at the SqlCacheDependency, which I don't believe will invalidate the cached item if the database is unavailable.

http://msdn.microsoft.com/en-us/library/system.web.caching.sqlcachedependency.aspx

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top