所有用到页面缓存的aspx页面修改以下cs,让它继承一个自定义基类(例如:PageCacheBase);
PageCacheBase 的 Page_Load
加入以下代码:
string cacheKey = Request.Url.ToString();
Cache[cacheKey] = new object();
Response.AddCacheItemDependency(cacheKey);
dv.Sort = "CacheName";
this.DataGrid1.DataSource =dt;
this.DataGrid1.DataBind();
string cacheKey = e.Item.Cells[0].Text;
if(Cache[cacheKey]!=null)
{
Cache.Remove(cacheKey);
bindCache();
}
PageCacheBase 的 Page_Load
加入以下代码:
string cacheKey = Request.Url.ToString();
Cache[cacheKey] = new object();
Response.AddCacheItemDependency(cacheKey);
这样就能在应用程序(整个网站)Cache里遍历缓存项(包括这些aspx页面的缓存依赖项)了,我把他们绑定道DataGrid:
private void bindCache()
{
DataTable dt = new DataTable();
dt.Columns.Add("CacheName",typeof(string));
dt.Columns.Add("CacheType",typeof(string));
IDictionaryEnumerator CacheEnum =
HttpRuntime.Cache.GetEnumerator();
while (CacheEnum.MoveNext())
{
DataRow dr = dt.NewRow();
dr["CacheName"] = CacheEnum.Key;
dr["CacheType"] = CacheEnum.Value.GetType();
dt.Rows.Add(dr);
}
DataView dv = dt.DefaultView; dv.Sort = "CacheName";
this.DataGrid1.DataSource =dt;
this.DataGrid1.DataBind();
}
删除就简单了: string cacheKey = e.Item.Cells[0].Text;
if(Cache[cacheKey]!=null)
{
Cache.Remove(cacheKey);
bindCache();
}