Last post May 26, 2011 11:29 AM by BostonDilbert
May 25, 2011 07:08 PM|biancaJ|LINK
I am creating this cache which is set to expire at 6pm . The cache is set in the manner shown below.
Cache.Add("myKey", 55, null, DateTime.Today.AddDays(0).AddHours(18), Cache.NoSlidingExpiration, CacheItemPriority.High, null)
So once it is created it is supposed to last till 6pm. But somehow it expires after a couple of hours of inactivity. Does the IIS remove cache after a certain time period? If yes, how do I avoid that?
Any help is appreciated :)
May 25, 2011 07:51 PM|biancaJ|LINK
Another thing I noticed while trying to figure the problem, is that the application variables also disappear after a certain period of time. In spite of there being no application restart or redeploy in this timeframe.
May 26, 2011 01:40 AM|sirdneo|LINK
IIS Removes items from cache if there is not sufficent memory on server. Also see your application pool settings in IIS. They can also cause IIS restarts which could delete cache items.
May 26, 2011 05:58 AM|Dave Sussman|LINK
Yes, the cache is not guaranteed; ASP.NET can remove items from the cache any time it likes if it needs the memory. Your code should always follow a pattern that checks for the item in the cache and uses it it's there, or if not, fetch it and re-cache it.
If this is a persistent problem, look at the memory usage on the machine and use the performance monitor to see how much memory is being used by iIS, the cache and what cache hit ratios are.
May 26, 2011 09:18 AM|sirdneo|LINK
Generally I use this type of code which checks that if item is available in cache then return that item from cache, otherwise load that item from db, save it in cache and then return it from cache. Here is a sample code snippet:-
public LookupEntity GetValue()
string cacheKey = "SomeKey";
//Check that record Exist In Cache or not
if (HttpRuntime.Cache[cacheKey] == null)
//If not then load Record from Database or any other location
var records = LookupEntity.GetRecord();
Cache.NoAbsoluteExpiration,//No Absolute Expiration
TimeSpan.FromMinutes(30));//Expire in 30 mins
return HttpRuntime.Cache[cacheKey] as LookupEntity;
May 26, 2011 11:29 AM|BostonDilbert|LINK
Your app by default uses the default app pool. The app pool kills the worker process after 20 minutes of inactivity. It also recycles the worker process after 1740 minutes. That would also explain why your application variables keep disappearing.
So what you need to do is create a new app pool. Remove the check boxes that say recylce worker process and shut down worker process. Use this app pool for your website.