新聞中心
ASP.NET緩存數(shù)據(jù)技巧:訪問(wèn)緩存的值

站在用戶(hù)的角度思考問(wèn)題,與客戶(hù)深入溝通,找到阜康網(wǎng)站設(shè)計(jì)與阜康網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶(hù)體驗(yàn)好的作品,建站類(lèi)型包括:成都網(wǎng)站建設(shè)、做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名申請(qǐng)、虛擬主機(jī)、企業(yè)郵箱。業(yè)務(wù)覆蓋阜康地區(qū)。
由于緩存中所存儲(chǔ)的信息為易失信息,即該信息可能由 ASP.NET 移除,因此建議先確定該項(xiàng)是否在緩存中。如果不在,則應(yīng)將它重新添加到緩存中,然后檢索該項(xiàng)。
- string cachedString;
- if (Cache["CacheItem"] != null)
- {
- cachedString = (string)Cache["CacheItem"];
- }
- else
- {
- //緩存不存在時(shí)
- Cache.Insert("CacheItem", "Hello, World.")
- cachedString = (string)Cache["CacheItem"];
- }
ASP.NET緩存數(shù)據(jù)技巧:刪除緩存項(xiàng)
由于以下任一原因,緩存中的數(shù)據(jù)可能會(huì)自動(dòng)移除:緩存已滿、該項(xiàng)已過(guò)期、依賴(lài)項(xiàng)發(fā)生更改。注意:如果調(diào)用 Insert 方法,并向緩存中添加與現(xiàn)有項(xiàng)同名的項(xiàng),則將從緩存中刪除該舊項(xiàng)。顯示刪除緩存的值:
- Cache.Remove("MyCacheKey");
ASP.NET緩存數(shù)據(jù)技巧:刪除緩存項(xiàng)時(shí)通知應(yīng)用程序
從緩存中移除項(xiàng)時(shí)通知應(yīng)用程序,可能非常有用。例如,可能具有一個(gè)緩存的報(bào)告,創(chuàng)建該報(bào)告需花費(fèi)大量的時(shí)間進(jìn)行處理。當(dāng)該報(bào)告從緩存中移除時(shí),希望重新生成該報(bào)告,并立即將其置于緩存中,以便下次請(qǐng)求該報(bào)告時(shí),用戶(hù)不必等待對(duì)此報(bào)告進(jìn)行處理。
ASP.NET 提供了CacheItemRemovedCallback 委托,在從緩存中移除項(xiàng)時(shí)能夠發(fā)出通知。還提供 CacheItemRemovedReason 枚舉,用于指定移除緩存項(xiàng)的原因。舉例:假設(shè)有一個(gè) ReportManager 對(duì)象,該對(duì)象具有兩種方法,即 GetReport 和 CacheReport。GetReport 報(bào)告方法檢查緩存以查看報(bào)告是否已緩存;如果沒(méi)有,該方法將重新生成報(bào)告并將其緩存。CacheReport 方法具有與 CacheItemRemovedCallback 委托相同的函數(shù)簽名;從緩存中移除報(bào)告時(shí),ASP.NET 會(huì)調(diào)用 CacheReport 方法,然后將報(bào)告重新添加到緩存中。
1)創(chuàng)建一個(gè) ASP.NET 網(wǎng)頁(yè),該網(wǎng)頁(yè)將調(diào)用類(lèi)中用于將項(xiàng)添加到緩存中的方法。
- protected void Page_Load(object sender, EventArgs e)
- {
- this.Label1.Text = ReportManager.GetReport();
- }
2)創(chuàng)建用于在從緩存中刪除項(xiàng)時(shí)處理通知的完整類(lèi)ReportManager。
- using System;
- using System.Web;
- using System.Web.Caching;
- public static class ReportManager
- {
- private static bool _reportRemovedFromCache = false;
- static ReportManager() { }
- //從緩存中獲取項(xiàng)
- public static String GetReport()
- {
- lock (typeof(ReportManager))
- {
- if (HttpContext.Current.Cache["MyReport"] != null)
- { //存在MyReport緩存項(xiàng),返回緩存值
- return (string)HttpRuntime.Cache["MyReport"];
- }
- else
- { //MyReport緩存項(xiàng)不存在,則創(chuàng)建MyReport緩存項(xiàng)
- CacheReport();
- return (string)HttpRuntime.Cache["MyReport"];
- }
- }
- }
- //將項(xiàng)以 MyReport 的名稱(chēng)添加到緩存中,并將該項(xiàng)設(shè)置為在添加到緩存中后一分鐘過(guò)期。
- //并且該方法注冊(cè) ReportRemoveCallback 方法,以便在從緩存中刪除項(xiàng)時(shí)進(jìn)行調(diào)用。
- public static void CacheReport()
- {
- lock (typeof(ReportManager))
- {
- HttpContext.Current.Cache.Add("MyReport",
- CreateReport(), null, DateTime.MaxValue,
- new TimeSpan(0, 1, 0),
- System.Web.Caching.CacheItemPriority.Default,
- ReportRemovedCallback);
- }
- }
- //創(chuàng)建報(bào)告,該報(bào)告時(shí)MyReport緩存項(xiàng)的值
- private static string CreateReport()
- {
- System.Text.StringBuilder myReport =
- new System.Text.StringBuilder();
- myReport.Append("Sales Report< br />");
- myReport.Append("2005 Q2 Figures< br />");
- myReport.Append("Sales NE Region - $2 million< br />");
- myReport.Append("Sales NW Region - $4.5 million< br />");
- myReport.Append("Report Generated: " + DateTime.Now.ToString()
- + "< br />");
- myReport.Append("Report Removed From Cache: " +
- _reportRemovedFromCache.ToString());
- return myReport.ToString();
- }
- //當(dāng)從緩存中刪除項(xiàng)時(shí)調(diào)用該方法。
- public static void ReportRemovedCallback(String key, object value,
- CacheItemRemovedReason removedReason)
- {
- _reportRemovedFromCache = true;
- CacheReport();
- }
- }
不應(yīng)在 ASP.NET 頁(yè)中實(shí)現(xiàn)回調(diào)處理程序,因?yàn)樵趶木彺嬷袆h除項(xiàng)之前該頁(yè)可能已被釋放,因此用于處理回調(diào)的方法將不可用,應(yīng)該在非ASP.NET的程序集中實(shí)現(xiàn)回調(diào)處理程序。為了確保從緩存中刪除項(xiàng)時(shí)處理回調(diào)的方法仍然存在,請(qǐng)使用該方法的靜態(tài)類(lèi)。但是,靜態(tài)類(lèi)的缺點(diǎn)是需要保證所有靜態(tài)方法都是線程安全的,所以使用lock關(guān)鍵字。
本文來(lái)自菩提屋:《緩存應(yīng)用程序數(shù)據(jù)(二)》
【編輯推薦】
- ASP.NET緩存數(shù)據(jù)添加方法一覽
- ASP.NET緩存機(jī)制基礎(chǔ)概念
- 再談ASP.NET緩存機(jī)制:開(kāi)發(fā)效率與優(yōu)化的平衡
- .NET分布式緩存之Memcached執(zhí)行速度檢測(cè)
- 如何避免ASP.NET緩存占用系統(tǒng)資源
本文標(biāo)題:ASP.NET緩存數(shù)據(jù)技巧三則
網(wǎng)站鏈接:http://m.fisionsoft.com.cn/article/djpjepg.html


咨詢(xún)
建站咨詢(xún)
