Introduction
CacheItemRemovedCallback is a delegate that provides notification to a page when an item has been removed.
Let's see how we can write code for CacheItemRemovedCallback.
Step 1: First of all we will create a new ASP.NET application.
Step 2: Now will add two buttons in the Default.aspx page. See the following image. One button will save the cache and the other will remove an item from the Cache.
Step 3: Now will write code inside the code-behind page. If you see the following highlighted code then you will understand how it will work.
In the first line of highlighted code we declare a callback delegate. In the next highlighted line we are initializing the callback delegate. It will be called when our cache time expires. In the following code we use 60 seconds:
static bool itemRemoved = false;
static CacheItemRemovedReason reason;
CacheItemRemovedCallback onRemove = null;
public void RemovedCallback(String k, Object v, CacheItemRemovedReason r)
{
itemRemoved = true;
reason = r;
}
protected void addCache_Click(object sender, EventArgs e)
{
itemRemoved = false;
onRemove = new CacheItemRemovedCallback(this.RemovedCallback);
if (Cache["Key1"] == null)
Cache.Add("Key1", "Value 1", null, DateTime.Now.AddSeconds(60), Cache.NoSlidingExpiration, CacheItemPriority.High, onRemove);
Label1.Text = "Cache has been set.";
}
protected void removeCache_Click(object sender, EventArgs e)
{
if (Cache["Key1"] != null)
Cache.Remove("Key1");
Label1.Text = "Cache has been removed.";
}
Step 4: You can only understand this concept while debugging the application; you will see how RemovedCallback will be called.
Happy Coding.

Amit PatelPosted Jul 3, 2012, 11:04 AM
Thanks Ed..
EdPosted Jul 3, 2012, 10:17 AM
This slight code change will show that the callback is being called without being in debug mode. static string removedMessage = string.Empty; public void RemovedCallback(String k, Object v, CacheItemRemovedReason r) { removedMessage = string.Format("Cache has been removed: {0}",k ); } protected void btnAddToCache_Click(object sender, EventArgs e) { ... Same as above } protected void btnRemoveFromCache_Click(object sender, EventArgs e) { if (Cache["Key1"] != null) Cache.Remove("Key1"); lblStatus.Text = removedMessage; }
Amit PatelPosted Jul 3, 2012, 2:49 AM
@Nitin, Defines a callback method for notifying applications when a cached item is removed from the Cache.
Amit PatelPosted Jul 3, 2012, 2:48 AM
@Gaurav, This enumeration is used by the CacheItemRemovedCallback delegate to notify your ASP.NET applications when and why an object was removed from the Cache.
nitin bhardwajPosted Jul 3, 2012, 1:27 AM
what is the mean of cacheitemremovedcallback?
Gaurav GuptaPosted Jul 3, 2012, 12:52 AM
Hi, What is CacheItemRemoveReason parameter in the method?
Gaurav GuptaPosted Jul 3, 2012, 12:52 AM
Hi, What is CacheItemRemoveReason parameter in the method?