5月,我们的纪念
Blog经过一次大的修整后 几个页面都采用了Cache机制,并设定了相应的过期时间 这样会加快页面的载入,减少等待的时间 但同时也有一个弊端:无法正确获取最新的记录 如发布一篇日志后,可能不会立即在首页显示出来,必须等缓存过期后, 才会再从数据库查询一次,此时才会看到最新的记录. 有时可能需要立即更新,这里就必须手工清除一下Cache Cache类有一个Remove方法,但该方法需要提供一个CacheKey,但整个网站的CacheKey我们是无法得知的 只能经过遍历 [code] 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 += "缓存名[" + CacheEnum.Key+"]
" ; } this.Label1.Text = "当前网站总缓存数:" + HttpRuntime.Cache.Count + "
"+str; } [/code]
Categories:   学以致用
Tags:  
Actions:   Comments (4) |

Comments

1#

September 25 2006 , 12:09

首的样式怎么没有了

不惊鸟 | Top

2#

September 25 2006 , 12:45

幻想曲

可能在读取Cookie时有点问题

幻想曲 | Top

3#

December 28 2006 , 14:00

客人

像一个论坛的帖子列表,该不该使用缓存呢?
如果帖子很多,很多页,是不是每页都缓存了?清除方法可能要使用你这种方法才能清除完。
但是我想能否有更好的办法呢?
例如缓存键值为:string cachekey="TopicList" + boardid.ToString() + page.ToString();
后面的page就可能有很多页,怎么清除完这个cachekey?
有其它可以清除部分缓存的办法吗?

客人 | Top

4#

December 28 2006 , 16:33

幻想曲

帖子列表数据变化比较大,用缓存不会起到作用。即使缓存了,当有新数据时,你也得手工Remove一下。
清除某一个缓存可以用
HttpContext.Current.Cache.Remove(CacheKey);

幻想曲 | Top

Add comment



(Will show your Gravatar icon)  



  Country flag


[b][/b] - [i][/i] - [u][/u]- [quote][/quote]

:-/ ^_^ :d :o :kiss: :) :p :se: [yeah] :( :love: :han: :up: :cry: :zzz: o_o


申请链接请看这里