在项目中我们经常会用到数据缓存,也会在项目处理对缓存的维护,但是有些时间我们需要人为的来维护这些缓存,用下面的代码来实现:
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();}}