Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

CacheItemRemovedCallback Example in ASP.NET

0.00/5 (No votes)
12 Oct 2010 1  
Exploring the CacheItemRemovedCallback delegate provided by ASP.NET

Notify When an Item is Removed from Cache in ASP.NET

While adding or inserting an item into cache object, we add dependency object as well to ensure the cache is automatically invalidated if any change is detected in that dependent object like file, for example. Then, we again read or update that item from the original source to make sure the item is still fresh with data. This is one of the main reasons that really appeal the use of ASP.NET caching feature where one can decide about dependencies and expiry time policy. There are other properties also that can be used in combination to set the scope of cached object within the time frame and location. Also see HttpCacheability.

But today, we will explore the CacheItemRemovedCallback delegate provided by ASP.NET. It is used to notify the application about cache removal or deletion with some reason. CacheItemRemovedReason enumeration is used as a parameter in callback method to tell the appropriate reason of removal.

Let’s take an example to know more about the CacheItemRemovedCallback delegate.

C#
protected void Page_Load(object sender, EventArgs e)   
{    
// Fetch item list from cache    
ArrayList cacheditems = CachedItemList();    
}

private static CacheItemRemovedCallback OnCachedItemRemoved = null;   
private ArrayList CachedItemList()    
{    
//    
OnCachedItemRemoved = new CacheItemRemovedCallback(this.CachedItemRemovedCallback);    
ArrayList cacheditems = HttpContext.Current.Cache.Get("CACHE_KEY") as ArrayList;    
// Found in cache    
if (cacheditems != null)    
{    
return cacheditems;    
}    
else    
{    
// Not found in cache    
cacheditems = ItemList();    
HttpContext.Current.Cache.Insert("CACHE_KEY", cacheditems, 
	new System.Web.Caching.CacheDependency(Server.MapPath
	("~//CacheDependentFile.txt")), Cache.NoAbsoluteExpiration, 
	Cache.NoSlidingExpiration, CacheItemPriority.Default, OnCachedItemRemoved);    
return cacheditems;    
}    
}

private void CachedItemRemovedCallback(string key, Object val, 
	CacheItemRemovedReason reason)   
{    
//    
if (reason == CacheItemRemovedReason.DependencyChanged)    
{    
// Log the cache key name, reason and time details    
// when the cached object was removed from the cache    
}    
}

private ArrayList ItemList()   
{    
//    
ArrayList lst = new ArrayList();    
lst.Add("First Item");    
lst.Add("Second Item");    
lst.Add("Third Item");    
lst.Add("Fourth Item");    
lst.Add("Fifth Item");    
return lst;    
}

Let’s see this call back method carefully:

C#
private void CachedItemRemovedCallback(string key, Object val, 
				CacheItemRemovedReason reason)   
{    
//    
}

The first parameter specifies the cache key name that we used to store an item (an ArrayList collection values). The second parameter is the object that we stored in the cache. The third parameter is an enumeration which has enum values like Removed, Expired, Underused, and DependencyChanged.

In the above example, if any change is made in CacheDependentFile.txt file, the callback method is automatically fired, and the reason captured will be DependencyChanged. Try and see.

Important Point: When using a CacheItemRemovedCallback, make sure that you make the callback method ("OnCachedItemRemoved" in the sample above) a static method.

This feature can be used in many cases like logging the reason why any cached item was removed from the cache, and many others depending upon the scenario.


Posted in .NET Technologies, ASP.NET, C#/VB.NET, CodeProject, Dot Net Tips Tagged: .NET 3.5, ASP.NET, C#, Caching

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here