.NET provides a callback method when a cached item is being removed from memory. Knowing when your cached item is removed is a good way to help manage caching performance. Below we'll take a look at how to implement the callback function that notifies us when a cached object is removed. We'll then display which cached object was removed along with the reason.
First add a label: lblInfo and a button: btnRemove to your web form.
Now move to the code-behind. First import the System.Web.Caching namespace.
In your Page_Load add the following:
Protected Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
Dim evtRemove As CacheItemRemovedCallback
evtRemove = New CacheItemRemovedCallback(AddressOf RemoveCallback)
' Add our new cached object, notice the onRemove callback
Cache.Add("CacheMe", _
"1 + 2 = 3", _
Nothing, _
Caching.Cache.NoAbsoluteExpiration, _
System.TimeSpan.FromSeconds(5), _
Caching.CacheItemPriority.Default, _
evtRemove)
End If
End Sub
What we do there is create the CacheItemRemovedCallback which points to our RemoveCallBack method (yet to be created) which will handle an event when the cached item is removed. We then add our cache object as normal but we pass our CacheItemRemovedCallback object. We set the expiration for 5 seconds for the sake of testing, assuming you'll be playing around with the code.
Next create the RemoveCallback method:
Protected Sub RemoveCallback(ByVal key As String, ByVal value As Object, ByVal reason As System.Web.Caching.CacheItemRemovedReason)
Dim nfo as String = "Cache item: [" & key & "] <br />value: [" & value.ToString() & "] <br />was [" & reason.ToString() & "]"
lblInfo.Text = nfo.ToString()
End Sub
The RemoveCallback returns a key (the name of the cached object), a value (the value of the cached object), and a reason (DependencyChanged, Expired, Removed, UnderUsed). Then we create a string to show what and why the cached object was removed by using the above values respectively.
Finally add the button that removes the cached object:
Protected Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
' Remove the cached item
Cache.Remove("CacheMe")
End Sub
The button's execution causes the cached item to be removed which in turn fires our callback function that we created above (RemoveCallback).
That's all there is to it. Now compile and run your app and play with the button. Also try letting the cache expire and refresh the page to see the different reasons for removing the cached object.
Update (5/29/2007 9:45AM): Some great advice from McGurk from the comments section of this post. I'm going to post the comments here because it's important to remember.
Whenever talking about cache and the CacheItemRemovedCallback it is always wise to remind people that you should NEVER re-add the item removed in this callback. Doing so can cause your application to bring down the worker process. Often this is because the item is being removed due to memory pressure, and re-adding the item will result in the cache immediately removing the item, which will result in the callback being called again. You can see the nasty loop you can get into.
For critical items set your priority on the item to an appropriate level. Use a pattern where you attempt to retrieve an item from the cache, and if it is not present retrieve it from storage (file, database, etc.), re-insert it in the cache and then return it to callers.