当前位置导航:炫浪网>>网络学院>>网页制作>>ASP.NET教程

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

欢迎进入.NET社区论坛,与200万技术人员互动交流 >>进入  在项目中我们经常会用到数据缓存,也会在项目处理对缓存的维护,但是有些时间我们需要人为的来维护这些缓存,用下面的代码来实现:
 
  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();
}
}
  
相关内容
赞助商链接