CacheItemRemovedCallback Delegate
链接地址:http://msdn.microsoft.com/en-us/library/system.web.caching.cacheitemremovedcallback.aspx
Defines a callback method for notifying applications when a cached item is removed from the Cache.
1<html>
2 <Script runat=server language="C#">
3 static bool itemRemoved = false;
4 static CacheItemRemovedReason reason;
5 CacheItemRemovedCallback onRemove = null;
6
7 public void RemovedCallback(String k, Object v, CacheItemRemovedReason r){
8 itemRemoved = true;
9 reason = r;
10 }
11
12 public void AddItemToCache(Object sender, EventArgs e) {
13 itemRemoved = false;
14
15 onRemove = new CacheItemRemovedCallback(this.RemovedCallback);
16
17 if (Cache["Key1"] == null)
18 Cache.Add("Key1", "Value 1", null, DateTime.Now.AddSeconds(60), Cache.NoSlidingExpiration, CacheItemPriority.High, onRemove);
19 }
20
21 public void RemoveItemFromCache(Object sender, EventArgs e) {
22 if(Cache["Key1"] != null)
23 Cache.Remove("Key1");
24 }
25 </Script>
26 <body>
27 <Form runat="server">
28 <input type=submit OnServerClick="AddItemToCache" value="Add Item To Cache" runat="server"/>
29 <input type=submit OnServerClick="RemoveItemFromCache" value="Remove Item From Cache" runat="server"/>
30 </Form>
31 <% if (itemRemoved) {
32 Response.Write("RemovedCallback event raised.");
33 Response.Write("<BR>");
34 Response.Write("Reason: <B>" + reason.ToString() + "</B>");
35 }
36 else {
37 Response.Write("Value of cache key: <B>" + Server.HtmlEncode(Cache["Key1"] as string) + "</B>");
38 }
39 %>
40 </body>
41</html>
2 <Script runat=server language="C#">
3 static bool itemRemoved = false;
4 static CacheItemRemovedReason reason;
5 CacheItemRemovedCallback onRemove = null;
6
7 public void RemovedCallback(String k, Object v, CacheItemRemovedReason r){
8 itemRemoved = true;
9 reason = r;
10 }
11
12 public void AddItemToCache(Object sender, EventArgs e) {
13 itemRemoved = false;
14
15 onRemove = new CacheItemRemovedCallback(this.RemovedCallback);
16
17 if (Cache["Key1"] == null)
18 Cache.Add("Key1", "Value 1", null, DateTime.Now.AddSeconds(60), Cache.NoSlidingExpiration, CacheItemPriority.High, onRemove);
19 }
20
21 public void RemoveItemFromCache(Object sender, EventArgs e) {
22 if(Cache["Key1"] != null)
23 Cache.Remove("Key1");
24 }
25 </Script>
26 <body>
27 <Form runat="server">
28 <input type=submit OnServerClick="AddItemToCache" value="Add Item To Cache" runat="server"/>
29 <input type=submit OnServerClick="RemoveItemFromCache" value="Remove Item From Cache" runat="server"/>
30 </Form>
31 <% if (itemRemoved) {
32 Response.Write("RemovedCallback event raised.");
33 Response.Write("<BR>");
34 Response.Write("Reason: <B>" + reason.ToString() + "</B>");
35 }
36 else {
37 Response.Write("Value of cache key: <B>" + Server.HtmlEncode(Cache["Key1"] as string) + "</B>");
38 }
39 %>
40 </body>
41</html>