1

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?

1 Answer 1

1

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:

  1. SharedCache
  2. AppFabric
  3. NCache
  4. MemCached.NET

Amongst others.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.