共用方式為


CacheItemRemovedCallback 代理人

定義

定義一個回呼 (Callback) 方法,以便在快取項目從 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)

參數

key
String

從快取中移除的索引鍵。

value
Object

與快取中移除的索引鍵相關聯的 Object 項目。

reason
CacheItemRemovedReason

項目從快取移除的理由,如 CacheItemRemovedReason 列舉型別 (Enumeration) 所指定的。

範例

下列程式碼範例示範一個頁面,其中顯示使用者指派給快取中專案的值,然後在從快取中移除專案時通知它們。 它會建立 RemovedCallback 方法,此方法會使用委派的 CacheItemRemovedCallback 簽章,在移除快取專案時通知使用者,並使用 CacheItemRemovedReason 列舉來告知他們移除它的原因。 此外,它會使用 Cache.Item[] 屬性將物件新增至快取,並擷取這些物件的值。 在 方法中 AddItemToCache Cache.Add ,它會使用 方法將專案新增至快取。 若要使用 CacheItemRemovedCallback 委派,您必須使用此方法或 Cache.Insert 方法將專案新增至快取,如此一來,ASP.NET 可以在移除專案時自動進行適當的方法呼叫。 自訂 RemoveItemFromCache 方法會 Cache.Remove 使用 方法來明確刪除快取中的專案,導致 RemovedCallback 叫用 方法。

<html>
 <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)
          Cache.Remove("Key1");
    }
// </snippet4>
 </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 {
// <snippet5>
        Response.Write("Value of cache key: <B>" + Server.HtmlEncode(Cache["Key1"] as string) + "</B>");
// </snippet5>
     }
  %>
 </body>
</html>
<%@ Page Language="VB" %>

<html>
 <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
          Cache.Remove("Key1")
        End If
    End Sub
' </snippet4>
 </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
' <snippet5>
    Response.Write("Value of cache key: <B>" + Server.HtmlEncode(CType(Cache("Key1"),String)) + "</B>")
' </snippet5>
End If
%>
 </body>
</html>

擴充方法

GetMethodInfo(Delegate)

取得表示特定委派所代表之方法的物件。

適用於

另請參閱