asp.net輸出緩存的使用網(wǎng)上已經(jīng)有很多例子了,這里主要介紹下如何在后臺管理中移除緩存。
1.基于頁面緩存
對于頁面:default.aspx 如果頁面頂部添加:
<%@ outputcache duration=60 varybyparam=none %>
在后臺管理中要移除很簡單:
system.web.httpresponse.removeoutputcacheitem(page.resolveurl(default.aspx));
2.基于控件
對于控件webusercontrol.ascx 如果在頂部添加了
<%@ outputcache duration=60 varybyparam=none shared=true%>
在后臺管理中要實現(xiàn)的話有點麻煩,在博客園的博問請朋友們解答,查爾斯提供了一種解決方法。
實現(xiàn)如下:
(1)添加varybycustom項,值為cashgroupclass。
<%@ outputcache duration=60 varybyparam=none shared=true varybycustom=cashgroupclass %>
(2) 在global.asax 中重寫 getvarybycustomstring 方法,代碼如下:
代碼
public override string getvarybycustomstring(httpcontext context, string arg)
{
if (arg == cashgroupclass)
{
cache objcache = httpruntime.cache;
object _flag = objcache[cashgroupclass];
if (_flag == null)
{
_flag = datetime.now.ticks.tostring();
objcache.insert(cashgroupclass, _flag);
}
return _flag.tostring();
}
return base.getvarybycustomstring(context, arg);
}
(3)在后臺管理的移除頁面添加如下代碼:
cache objcache = httpruntime.cache;
if (objcache[cashgroupclass] != null)
{
objcache.remove(cashgroupclass);
}
當然,您也可以借助這個方法實現(xiàn)控件的緩存更新。對了,查爾斯貼的代碼中有使用datacache類,是個自己寫的類,可以參考datacache ,不過里面重載參數(shù)對不上。那就加一個吧。
代碼
public static void setcache(string cachekey, object objobject, datetime absoluteexpiration, timespan slidingexpiration)
{
httpruntime.cache.insert(cachekey, objobject, null, absoluteexpiration, slidingexpiration);
}
最后,感謝朋友們對我的幫助。
參考:(1):緩存應(yīng)用程序頁面和數(shù)據(jù)(一)
(2):asp.net緩存
(3):global.asax.cs中的getvarybycustomstring函數(shù)在什么地方調(diào)用
(4):datacache
更多信息請查看IT技術(shù)專欄