.NET Framework Class Library
CacheItemRemovedCallback Delegate

Defines a callback method for notifying applications when a cached item is removed from the Cache.

Namespace:  System.Web.Caching
Assembly:  System.Web (in System.Web.dll)
Syntax

Visual Basic (Declaration)
Public Delegate Sub CacheItemRemovedCallback ( _
    key As String, _
    value As Object, _
    reason As CacheItemRemovedReason _
)
Visual Basic (Usage)
Dim instance As New CacheItemRemovedCallback(AddressOf HandlerMethod)
C#
public delegate void CacheItemRemovedCallback(
    string key,
    Object value,
    CacheItemRemovedReason reason
)
Visual C++
public delegate void CacheItemRemovedCallback(
    String^ key, 
    Object^ value, 
    CacheItemRemovedReason reason
)
JScript
JScript does not support delegates.

Parameters

key
Type: System..::.String
The key that is removed from the cache.
value
Type: System..::.Object
The Object item associated with the key removed from the cache.
reason
Type: System.Web.Caching..::.CacheItemRemovedReason
The reason the item was removed from the cache, as specified by the CacheItemRemovedReason enumeration.
Examples

The following code example demonstrates a page that shows users the value assigned to an item in the cache and then notifies them when the item is removed from the cache. It creates a RemovedCallback method, which uses the signature of the CacheItemRemovedCallback delegate, to notify users when the cache item is removed and uses the CacheItemRemovedReason enumeration to tell them why it was removed. In addition, it uses the Cache..::.Item property to add objects to the cache and retrieve the value of those objects. In the AddItemToCache method, it uses the Cache..::.Add method to add an item to the cache. To use the CacheItemRemovedCallback delegate, you must add an item to the cache with this method or the Cache..::.Insert method so that that ASP.NET can automatically make the proper method calls when the item is removed. The custom RemoveItemFromCache method uses the Cache..::.Remove method to explicitly delete the item from the cache, causing the RemovedCallback method to be invoked.

Visual Basic
<%@ Page Language="VB" %>

<html>
 <Script runat=server>
    Shared itemRemoved As boolean = false
    Shared reason As CacheItemRemovedReason
    Dim onRemove As CacheItemRemovedCallback

    Public Sub RemovedCallback(k As String, v As Object, r As CacheItemRemovedReason)
      itemRemoved = true
      reason = r
    End Sub

    Public Sub AddItemToCache(sender As Object, e As EventArgs)
        itemRemoved = false

        onRemove = New CacheItemRemovedCallback(AddressOf Me.RemovedCallback)

        If (IsNothing(Cache("Key1"))) Then
          Cache.Add("Key1", "Value 1", Nothing, DateTime.Now.AddSeconds(60), Cache.NoSlidingExpiration, CacheItemPriority.High, onRemove)
        End If
    End Sub

    Public Sub RemoveItemFromCache(sender As Object, e As EventArgs)
        If (Not IsNothing(Cache("Key1"))) Then
          Cache.Remove("Key1")
        End If
    End Sub
 </Script>

 <body>
  <Form runat="server">
    <input type=submit OnServerClick="AddItemToCache" value="Add Item To Cache" runat="server"/>
    <input type=submit OnServerClick="RemoveItemFromCache" value="Remove Item From Cache" runat="server"/>
  </Form>
<%
If (itemRemoved) Then
    Response.Write("RemovedCallback event raised.")
    Response.Write("<BR>")
    Response.Write("Reason: <B>" + reason.ToString() + "</B>")
Else
    Response.Write("Value of cache key: <B>" + Server.HtmlEncode(CType(Cache("Key1"),String)) + "</B>")
End If
%>
 </body>
</html>
C#
<html>
 <Script runat=server language="C#">
    static bool itemRemoved = false;
    static CacheItemRemovedReason reason;
    CacheItemRemovedCallback onRemove = null;

    public void RemovedCallback(String k, Object v, CacheItemRemovedReason r){
      itemRemoved = true;
      reason = r;
    }

    public void AddItemToCache(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);
    }

    public void RemoveItemFromCache(Object sender, EventArgs e) {
        if(Cache["Key1"] != null)
          Cache.Remove("Key1");
    }
 </Script>
 <body>
  <Form runat="server">
   <input type=submit OnServerClick="AddItemToCache" value="Add Item To Cache" runat="server"/>
   <input type=submit OnServerClick="RemoveItemFromCache" value="Remove Item From Cache" runat="server"/>
  </Form>
  <% if (itemRemoved) {
        Response.Write("RemovedCallback event raised.");
        Response.Write("<BR>");
        Response.Write("Reason: <B>" + reason.ToString() + "</B>");
     }
     else {
        Response.Write("Value of cache key: <B>" + Server.HtmlEncode(Cache["Key1"] as string) + "</B>");
     }
  %>
 </body>
</html>
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
See Also

Reference

Tags :


Community Content

Tess1
Inappropriate use can lead to memory leaks and stackoverflow exceptions

When using a CacheItemRemovedCallback make sure that you make the callback method ("RemovedCallback" in the sample above) a static method.
If you use an instance method the whole page where the method is defined (including any controls on it or any other data associated with the page) will be stick around in memory for the period that the object is stored in cache. This is because the CacheItemRemovedCallback will need to call into the method of the page object once the cached object is removed from cache. With a static method however the page does not need to stay around so it can be garbage collected appropriately. See http://blogs.msdn.com/tess/archive/2006/08/11/695268.aspx for more info on this

Also take care if you refresh the item in the CacheItemRemovedCallback as this can lead to stackoverflow exceptions if done inappropriately. See http://blogs.msdn.com/tess/archive/2006/11/27/asp-net-crash-bad-cacheitemremovedcallback-part-ii.aspx for more information on this.

Tags :

Page view tracker