asp.net 輸出緩存移除的實例代碼
來源:易賢網(wǎng) 閱讀:674 次 日期:2014-11-12 14:54:18
溫馨提示:易賢網(wǎng)小編為您整理了“asp.net 輸出緩存移除的實例代碼”,方便廣大網(wǎng)友查閱!

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ù)專欄

更多信息請查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機網(wǎng)站地址:asp.net 輸出緩存移除的實例代碼

2025國考·省考課程試聽報名

  • 報班類型
  • 姓名
  • 手機號
  • 驗證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)