Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpContext.Cache Expiration

Is there a way to specify how long data is held in HttpContext.Cache?

like image 695
Micah Avatar asked Jun 11 '09 21:06

Micah


People also ask

Is HttpContext current Cache thread safe?

HttpContext access from a background threadHttpContext isn't thread-safe.


1 Answers

You can specify it in the 4th parameter of Cache.Add():

public Object Add(     string key,     Object value,     CacheDependency dependencies,     DateTime absoluteExpiration,  // After this DateTime, it will be removed from the cache     TimeSpan slidingExpiration,     CacheItemPriority priority,     CacheItemRemovedCallback onRemoveCallback ) 

Edit:

If you access the cache via the indexer (i.e. Cache["Key"]), the method that is called uses no expiration and remains in the cache indefinitely.

Here is the code that is called when you use the indexer:

public void Insert(string key, object value) {     this._cacheInternal.DoInsert(true, key, value, null, NoAbsoluteExpiration, NoSlidingExpiration, CacheItemPriority.Normal, null, true); } 
like image 112
John Rasch Avatar answered Oct 05 '22 21:10

John Rasch