CacheItemRemovedCallback 委托
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
定义在从 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
从缓存中移除的键。
- reason
- CacheItemRemovedReason
CacheItemRemovedReason 枚举指定的、从缓存移除项的原因。
示例
下面的代码示例演示了一个页面,它向用户显示分配给缓存中某个项的值,然后在从缓存中删除该项时通知他们。 它创建一个 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) |
获取指示指定委托表示的方法的对象。 |