1<script language="C#" runat="server">
  2    //声明是什么原因造成的缓存移除的变量---注意类型
  3    static CacheItemRemovedReason reason;
  4    string[] tempArray ="北京""上海""广州""成都""深圳" };
  5    //实现增加按钮的事件处理程序
  6    protected void AddItemToCache(object sender, EventArgs e)
  7    {
  8        //如果缓存里的对象为空
  9        if (Cache["tempArray"== null)
 10        
 11            //则创建缓存对象
 12            //注意这个增加方法的最后一个参数是一个委托,里面的方法是在下面定义的
 13            //方法定义没有返回值,方法定义为: private void ItemRemoved(String Key,
 14            // object value,CacheItemRemovedReason RemovedReason)
 15            //以上这个方法就可以看出Add方法的委托的定义为:
 16            //Deledete void CacheItemRemovedCallback(String Key,object value,
 17            //CacheItemRemovedReason RemovedReason) 
 18            //所有程序在调用Add方法的时候也会自动出发委托里面的ItemRemoved方法
 19            //即下面我自己写的方法
 20            Cache.Add("tempArray", tempArray, null, DateTime.MaxValue, TimeSpan.Zero
 21                , CacheItemPriority.Default, new CacheItemRemovedCallback(ItemRemoved));
 22            lbMessage.Text+=">>>>已经将字符串数组增加到缓存中.<br>";
 23         
 24        }

 25           else
 26            {
 27                lbMessage.Text+= ">>>>缓存中已经存在字符串数组了.<br>";
 28            }

 29            DisPlayCacheInfo();
 30    }

 31    //实现检索按钮的事件处理程序
 32    private void GetItemFromCache(object sender, EventArgs e)
 33    {
 34        if (Cache["tempArray"== null)
 35        {
 36            lbMessage.Text += ">>>>未索引到缓存,因为tempArray中缓存已被移除<br>";
 37        }

 38        else
 39        {
 40            lbMessage.Text += ">>>>以索引到缓存数组<br>";
 41        }

 42        DisPlayCacheInfo();
 43    }

 44    //实现删除按钮的事件处理程序
 45    private void RemovedItemFromCache(object sender,EventArgs e)
 46    {
 47        if (Cache["tempArray"== null)
 48        {
 49            lbMessage.Text += ">>>>未索引到缓存,因为tempArray中缓存已被移除<br>";
 50        }

 51        else
 52        {
 53            Cache.Remove("tempArray");
 54            lbMessage.Text += "已经调用CacheItemRemovedCallback委托方法,缓存移除原因是:" +
 55                reason.ToString() + "<br>";
 56            lbMessage.Text += ">>>>已经删除数组中的数组缓存<br>";
 57        }

 58        DisPlayCacheInfo();
 59    }

 60    //CacheItemRemovedCallback的方法ItemRemoved定义
 61    private void ItemRemoved(string Key,object value,CacheItemRemovedReason RemovedReason)
 62    {
 63        reason = RemovedReason;
 64    }

 65    private void DisPlayCacheInfo()
 66    {
 67        string CacheCount = Cache.Count.ToString();
 68        lbCacheInfo.Text = "共包括" + CacheCount + "个缓存对象<br>";
 69        IDictionaryEnumerator CacheEnum = Cache.GetEnumerator();
 70        while (CacheEnum.MoveNext())
 71        {
 72            lbCacheInfo.Text += "索引到的Key为:" + CacheEnum.Key.ToString() +
 73                "---" + "索引到的Value为:" + CacheEnum.Value.ToString();
 74        }

 75    }

 76</script>
 77
 78<html xmlns="http://www.w3.org/1999/xhtml" >
 79<head runat="server">
 80    <title>无标题页</title>
 81</head>
 82<body>
 83    <form id="form1" runat="server">
 84    <div style="text-align: center">
 85    <fieldset style="width: 496px; height: 456px">
 86    <legend>应用程序数据缓存的增删查</legend>
 87        <br />
 88        <div style="text-align:left; color:red">字符串数组内容如下:</div>
 89        <div style="text-align:center">北京,上海,广州,成都,深圳</div>
 90        <hr style="color:Navy" />
 91        <div>
 92         <center>
 93            <asp:Button ID="btAdd" Text="增加" runat="server" OnClick="AddItemToCache" />
 94            <asp:Button ID="btGet" Text="检索" runat="server" OnClick="GetItemFromCache" />
 95            <asp:Button ID="btDel" Text="移除" runat="server" OnClick="RemovedItemFromCache" />
 96         </center>
 97            <center>
 98                &nbsp;</center>
 99            <center>
100            <div>
101            <asp:Label ID="lbCacheInfo" runat="server" ForeColor="RosyBrown" Width="100%" Height="80px"></asp:Label>
102            </div>
103            </center>
104        </div>
105        <hr style="color:Navy" />
106        <asp:Label ID="lbMessage" runat="server" ForeColor="Blue" Height="50%" Width="100%"></asp:Label>
107    </fieldset>
108    </div>
109    </form>
110</body>
111</html>
112
posted on 2006-12-26 21:49  小角色  阅读(235)  评论(0)    收藏  举报