Click here to Skip to main content
15,867,771 members
Articles / Web Development / ASP.NET

CacheItemRemovedCallback Example in ASP.NET

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
12 Oct 2010CPOL2 min read 25.7K   5  
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead Imfinity India Pte Ltd, Noida (Excelsoft Company)
India India
http://www.imfinity.com/

Comments and Discussions

 
-- There are no messages in this forum --