ASP.NET Cache supports CacheItemRemovedCallback option since .NET 1.1 (http://msdn.microsoft.com/en-us/library/aa478965.aspx), and we can do some interesting things such as logging inside the callback function. But a big limitation on it is that we can’t do cache validation to keep or refresh cache data in this callback function:
"One potential use for this feature would be to refresh cached data in the background so that users never need to wait for the data to be populated, but the data is kept relatively fresh. Unfortunately in practice, this doesn't work very well with the current version of the caching API, because the callback doesn't fire or complete execution prior to the cached item being removed from the cache. Thus, a user will frequently make a request that will try to access the cached value, find that it is null, and be forced to wait for it to repopulate."
From .NET 3.5, we have a new CacheItemUpdateCallback delegate for caching handling (http://msdn.microsoft.com/en-us/library/system.web.caching.cacheitemupdatecallback.aspx). Now we can periodically update the cache by the callback function. The new overloading Cache Insert method is:HttpRuntime.Cache.Insert(The callback function signature is:
stringKey,
objectValue,
cacheDependencies,
dateTimeAbsoluteExpiration,
timeSpanSlidingExpiration,
cacheUpdateCallback)
This new feature is very handy when we need to automatically refresh the cache in background.
public delegate void CacheItemUpdateCallback(
string key,
CacheItemUpdateReason reason,
out Object expensiveObject,
out CacheDependency dependency,
out DateTime absoluteExpiration,
out TimeSpan slidingExpiration
)