在asp.net 中实现维护数据缓存

本文介绍了一种在项目中管理和操作缓存的方法,包括如何将缓存信息绑定到DataGrid进行展示,以及如何删除指定的缓存项。
在项目中我们经常会用到数据缓存,也会在项目处理对缓存的维护,但是有些时间我们需要人为的
来维护这些缓存,用下面的代码来实现:

1、将缓存信息绑定到DataGrid上
  
   private void bindCache()
        {
            string str = this.TextBox1.Text.Trim();
            DataTable table = new DataTable();
            table.Columns.Add("CacheName", typeof(string));
            table.Columns.Add("CacheType", typeof(string));
            IDictionaryEnumerator enumerator = HttpRuntime.Cache.GetEnumerator();
            int num = 0;
            while (enumerator.MoveNext())
            {
                bool flag = true;
                if ((str != "") && (enumerator.Key.ToString().IndexOf(str) < 0))
                {
                    flag = false;
                }
                if (flag)
                {
                    num++;
                    DataRow row = table.NewRow();
                    row["CacheName"] = enumerator.Key;
                    row["CacheType"] = enumerator.Value.GetType();
                    table.Rows.Add(row);
                }
            }
            this.Label1.Text = num.ToString().Trim();
            this.DataGrid1.DataSource = table;
            this.DataGrid1.DataBind();
        }

2、清除指定的缓存

   private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            string text = e.Item.Cells[0].Text;
            if (base.Cache[text] != null)
            {
                base.Cache.Remove(text);
                this.bindCache();
            }
        }

转载于:https://www.cnblogs.com/bccu/archive/2008/01/03/1024670.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值