溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

ASP.NET緩存數(shù)據(jù)技巧有哪些

發(fā)布時(shí)間:2021-12-06 15:44:51 來源:億速云 閱讀:135 作者:iii 欄目:編程語言

這篇文章主要講解了“ASP.NET緩存數(shù)據(jù)技巧有哪些”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“ASP.NET緩存數(shù)據(jù)技巧有哪些”吧!

ASP.NET緩存數(shù)據(jù)技巧:訪問緩存的值

由于緩存中所存儲(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)已過期、依賴項(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)告,并立即將其置于緩存中,以便下次請求該報(bào)告時(shí),用戶不必等待對此報(bào)告進(jìn)行處理。

ASP.NET 提供了CacheItemRemovedCallback 委托,在從緩存中移除項(xiàng)時(shí)能夠發(fā)出通知。還提供 CacheItemRemovedReason 枚舉,用于指定移除緩存項(xiàng)的原因。舉例:假設(shè)有一個(gè) ReportManager 對象,該對象具有兩種方法,即 GetReport 和 CacheReport。GetReport 報(bào)告方法檢查緩存以查看報(bào)告是否已緩存;如果沒有,該方法將重新生成報(bào)告并將其緩存。CacheReport 方法具有與 CacheItemRemovedCallback 委托相同的函數(shù)簽名;從緩存中移除報(bào)告時(shí),ASP.NET 會(huì)調(diào)用 CacheReport 方法,然后將報(bào)告重新添加到緩存中。

1)創(chuàng)建一個(gè) ASP.NET 網(wǎng)頁,該網(wǎng)頁將調(diào)用類中用于將項(xiàng)添加到緩存中的方法。

protected void Page_Load(object sender, EventArgs e)  {      this.Label1.Text = ReportManager.GetReport();  }

2)創(chuàng)建用于在從緩存中刪除項(xiàng)時(shí)處理通知的完整類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 的名稱添加到緩存中,并將該項(xiàng)設(shè)置為在添加到緩存中后一分鐘過期。      //并且該方法注冊 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 頁中實(shí)現(xiàn)回調(diào)處理程序,因?yàn)樵趶木彺嬷袆h除項(xiàng)之前該頁可能已被釋放,因此用于處理回調(diào)的方法將不可用,應(yīng)該在非ASP.NET的程序集中實(shí)現(xiàn)回調(diào)處理程序。為了確保從緩存中刪除項(xiàng)時(shí)處理回調(diào)的方法仍然存在,請使用該方法的靜態(tài)類。但是,靜態(tài)類的缺點(diǎn)是需要保證所有靜態(tài)方法都是線程安全的,所以使用lock關(guān)鍵字。

感謝各位的閱讀,以上就是“ASP.NET緩存數(shù)據(jù)技巧有哪些”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對ASP.NET緩存數(shù)據(jù)技巧有哪些這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI