When you add an item to the System.Web.Caching.Cache
with an absolute expiration date, as in the following example, how does Asp.Net behave? Does it:
Simply mark the item as expired, then execute the
CacheItemRemovedCallback
on the next access attempt?Remove the item from the cache and execute the
CacheItemRemovedCallback
immediately?HttpRuntime.Cache.Insert(key, new object(), null, DateTime.Now.AddSeconds(seconds), Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, OnCacheRemove);
MSDN appears to indicate that it happens immediately. For example, the "Expiration" section of the "ASP.NET Caching Overview" says "ASP.NET automatically removes items from the cache when they expire." Similarly, the example from the topic "How to: Notify an Application When an Item Is Removed from the Cache" says "If more than 15 seconds elapses between calls to GetReport
[a method in the example], ASP.NET removes the report from the cache."
Still, neither of these is unambiguous. They don't say "the callback is executed immediately" and I could conceive of how their writers might have thought option 1 above counts as 'removing' an item. So I did a quick and dirty test, and lo, it appears to be executing immediately - I get regular sixty-second callbacks even when no one is accessing my site.
Nonetheless, my test was quick and dirty, and in the comments to my answer to Is there a way to run a process every day in a .Net web application without writing a windows service or SQL server jobs, someone has suggested that Asp.Net actually defers removal and execution of the callback until something tries to access the cache again.
Can anyone settle this authoritatively or is this just considered an implementation detail?