1

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?

1 Answer 1

0

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

1
  • Thanks for the info. Good stuff! Jul 30, 2013 at 2:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.