Is there a way to specify how long data is held in HttpContext.Cache?
HttpContext access from a background threadHttpContext isn't thread-safe.
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 )
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); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With