CacheItemRemovedCallback 委托


定义在从 Cache 移除缓存项时通知应用程序的回调方法。Defines a callback method for notifying applications when a cached item is removed from the Cache.

public delegate void CacheItemRemovedCallback(System::String ^ key, System::Object ^ value, CacheItemRemovedReason reason);
public delegate void CacheItemRemovedCallback(string key, object value, CacheItemRemovedReason reason);
type CacheItemRemovedCallback = delegate of string * obj * CacheItemRemovedReason -> unit
Public Delegate Sub CacheItemRemovedCallback(key As String, value As Object, reason As CacheItemRemovedReason)



从缓存中移除的键。The key that is removed from the cache.


与从缓存中移除的键关联的 Object 项。The Object item associated with the key removed from the cache.


CacheItemRemovedReason 枚举指定的、从缓存移除项的原因。The reason the item was removed from the cache, as specified by the CacheItemRemovedReason enumeration.



下面的代码示例演示一个页面, 该页面向用户显示分配给缓存中某个项的值, 然后在从缓存中移除该项时发出通知。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. 它创建一个RemovedCallback方法, 该方法使用CacheItemRemovedCallback委托的签名, 以便在删除缓存项时通知用户, 并使用CacheItemRemovedReason枚举告诉用户删除该缓存项的原因。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. 此外, 它还使用Cache.Item[String]属性将对象添加到缓存并检索这些对象的值。In addition, it uses the Cache.Item[String] property to add objects to the cache and retrieve the value of those objects. 在方法中, 它Cache.Add使用方法将一项添加到缓存中。 AddItemToCacheIn the AddItemToCache method, it uses the Cache.Add method to add an item to the cache. 若要使用CacheItemRemovedCallback委托, 必须使用此方法Cache.Insert或方法将一个项添加到缓存, 以便在移除该项时 ASP.NET 可以自动执行正确的方法调用。To use the CacheItemRemovedCallback delegate, you must add an item to the cache with this method or the Cache.Insert method so that ASP.NET can automatically make the proper method calls when the item is removed. 自定义RemoveItemFromCache方法Cache.Remove使用方法从缓存显式删除项, 从而导致RemovedCallback调用方法。The custom RemoveItemFromCache method uses the Cache.Remove method to explicitly delete the item from the cache, causing the RemovedCallback method to be invoked.

 <Script runat=server language="C#">
// <snippet2>
    static bool itemRemoved = false;
    static CacheItemRemovedReason reason;
    CacheItemRemovedCallback onRemove = null;

    public void RemovedCallback(String k, Object v, CacheItemRemovedReason r){
      itemRemoved = true;
      reason = r;
// </snippet2>

// <snippet3>
    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);
// </snippet3>

// <snippet4>
    public void RemoveItemFromCache(Object sender, EventArgs e) {
        if(Cache["Key1"] != null)
// </snippet4>
  <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"/>
  <% if (itemRemoved) {
        Response.Write("RemovedCallback event raised.");
        Response.Write("Reason: <B>" + reason.ToString() + "</B>");
     else {
// <snippet5>
        Response.Write("Value of cache key: <B>" + Server.HtmlEncode(Cache["Key1"] as string) + "</B>");
// </snippet5>
<%@ Page Language="VB" %>

 <Script runat=server>
' <snippet2>
    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
' </snippet2>

' <snippet3>
    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
' </snippet3>

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

  <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"/>
If (itemRemoved) Then
    Response.Write("RemovedCallback event raised.")
    Response.Write("Reason: <B>" + reason.ToString() + "</B>")
' <snippet5>
    Response.Write("Value of cache key: <B>" + Server.HtmlEncode(CType(Cache("Key1"),String)) + "</B>")
' </snippet5>
End If



获取指示指定委托表示的方法的对象。Gets an object that represents the method represented by the specified delegate.