I have an ASP.NET MVC 3 app hosted on IIS. This app is using MemoryCache to work with some data in memory to help minimize hits to my database. I would like this data to stay cached for up to a single day. In my code, I’m setting the following:
List<MyObject> myItems = GetMyItems();
CacheItemPolicy cachePolicy = new CacheItemPolicy();
cachePolicy.AbsoluteExpiration = new DateTimeOffset(DateTime.Now.AddMinutes(1440));
Cache.Set("myCacheKey", myItems, cachePolicy);
While my caching seems to be working. However, it does not seem to stay in the cache for the intended duration. Is there a setting I need to make in the web.config, if so, what setting? If not, is it an IIS setting that I need to set to enable a longer cache duration?
The problem most likely lies in that your app pool is recycling after periods of non-use. When app pools are recycled, everything you had in memory is lost.
If you need a more durable cache, you will need to switch to something that is not hosted in process. There are a number of commercial and open-source caching services; I would recommend going with one of those.
Here are just a few cache solutions:
Amongst others.