views:

341

answers:

2

If I have the following line, when should I expect the cache to expire?

System.Web.HttpRuntime.Cache.Insert("someKey", "Test value");
+3  A: 

This will insert the object without an explicit expiration set. This means the object will not automatically be removed from the cache, unless the runtime decides to remove stuff from the cache due to high memory usage.

Calling this overload is the same as calling

Cache.Insert(
  key, value,
  null,                     /*CacheDependency*/
  NoAbsoluteExpiration,     /*absoluteExpiration*/
  NoSlidingExpiration,      /*slidingExpiratioin*/
  CacheItemPriority.Normal, /*priority*/
  null                      /*onRemoveCallback*/
);

BTW: you can use .NET reflector to find out such things.

M4N
+5  A: 

"Never", that is, as soon as memory is low and ASP.NET Cache thinks it has something more important to keep.

markus
Is restarting of iis/application pool under which web site is running will cause object to be removed from cache? In short, is there any connection between iis/application pool and cache?
Neil
Yes, all your cache items will be gone if your application pool or your application (change in BIN folder or web.config) restarts, as long as you don't use any out-of-process cache provider.So it actually is just what it is: a cache. Don't use it for anything else. You can never make assumptions on what is in there (only what should not be in there anymore).
markus