列出当前站点所有Cache并清除

in Develop

Blog经过一次大的修整后
几个页面都采用了Cache机制,并设定了相应的过期时间
这样会加快页面的载入,减少等待的时间

但同时也有一个弊端:无法正确获取最新的记录
如发布一篇日志后,可能不会立即在首页显示出来,必须等缓存过期后,
才会再从数据库查询一次,此时才会看到最新的记录.

有时可能需要立即更新,这里就必须手工清除一下Cache
Cache类有一个Remove方法,但该方法需要提供一个CacheKey,但整个网站的CacheKey我们是无法得知的
只能经过遍历

protected void RemoveAllCache()
{

System.Web.Caching.Cache _cache = HttpRuntime.Cache;
IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
ArrayList al = new ArrayList();
while (CacheEnum.MoveNext())
{
al.Add(CacheEnum.Key);
}

foreach (string key in al)
{
_cache.Remove(key);
}
show();
}
//显示所有缓存
void show()
{
string str = "";
IDictionaryEnumerator CacheEnum = HttpRuntime.Cache.GetEnumerator();

while (CacheEnum.MoveNext())
{
str += "缓存名<b>[" + CacheEnum.Key+"]</b><br />" ;
}
this.Label1.Text = "当前网站总缓存数:" + HttpRuntime.Cache.Count + "<br />"+str;
}

2 Comments

2 Comments

  1. 首的样式怎么没有了

  2. 可能在读取Cookie时有点问题

Leave a Reply

Using Gravatars in the comments - get your own and be recognized!

XHTML: These are some of the tags you can use: <a href=""> <b> <blockquote> <code> <em> <i> <strike> <strong>