Creating Auto cache Refresh in ASP.NET

ASP.NET cache CacheItemRemovedCallback delegate may be used

quote from: http://www.developersdex.com/gurus/code/653.asp
The call back function which call every time cache item expired can be used to create a mechanism that will update the cache item data every time interval. Using such mechanism we can cache data that changed in fixed time intervals.
To update the cache item data we need to retrieve the item data from its storage and insert the item again into the cache to reflect the new changes. All this work is done in the callback function placed in the asax file. The problem is that in order to update the cache we need to use the application object and while the call back function call we cant use the Context object to get the session due to the fact that the call back not called from any request.
To work around this problem we can declare Class variable that will hold the cache. We will set this variable to the application process while the Application_onStart event occurred. Now after the cache points to the application change we can use it to change the current cache items.

static bool itemRemoved = false;static CacheItemRemovedReason reason;CacheItemRemovedCallback onRemove = null;
onRemove = new CacheItemRemovedCallback(this.RemovedCallback);
if (Cache[“Key1”] == null) {
Cache.Add(“Key1”, “Caching”, null, DateTime.Now.AddSeconds(60),
TimeSpan.Zero, CacheItemPriority.High, onRemove);
}

in RemovedCallback method we may refresh our cache and use Application object to insert the objectto the cache again.