Understanding HttpContext.Current.Cache

3.4k Views Asked by At

I've been making some changes to a site and have noticed that when I make a change in the database, ie. add a new person value, the person's name doesn't appear in a dropdown list.

If I reset the application pool a few times the name does finally appear for me. Though doesn't always seem to for others.

It does seem to be controlled heavily by cache and I've found the following:

HttpContext.Current.Cache.Insert("PersohnList", d, Nothing, 
       DateTime.MaxValue, New System.TimeSpan(0, 30, 0), 
       Caching.CacheItemPriority.High, Nothing)

I've noticed a few values here of interest,

a)  DateTime.MaxValue  
b)  New System.TimeSpan(0, 30, 0)

Will the cache expire after 30 mins and insert the new person's name or will it not expire because of DateTime.MaxValue.

Any ideas?

2

There are 2 best solutions below

1
On BEST ANSWER

I am not certain which would win out. It seems that this code is setting the slidingExpiration and the absoluteExpiration. I would assume the latter would win in this but the MSDN documentation says that you should only use one or the other. If using the slidingExpiration, you should set the absoluteExpiration parameter to NoAbsoluteExpiration and if using the absoluteExpiration, you should set the slidingExpiration to NoSlidingExpiration.

If I were you, I would just invalidate the cache when the value is being updated (HttpContext.Current.Cache.Remove("PersohnList") and reset it again.

0
On

+1 for sliding window. Each time another user is touching that key, "PersohnList", the cache expiration adds 30 more minutes. Also, check out System.Runtime.Caching as a more modern alternative.

From http://msdn.microsoft.com/en-us/library/system.runtime.caching%28v=vs.110%29.aspx

"The System.Runtime.Caching namespace contains types that let you implement caching in NET Framework applications.

The classes in this namespace provide a way to use caching facilities like those in ASP.NET, but without a dependency on the System.Web assembly."