自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

ASP.NET緩存數(shù)據(jù)技巧三則

開發(fā) 后端
ASP.NET使用緩存機(jī)制,將需要大量服務(wù)器資源來創(chuàng)建的對象存儲在內(nèi)存中。本文介紹在編寫ASP.NET應(yīng)用時可能會用到的三個ASP.NET緩存數(shù)據(jù)技巧。

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

由于緩存中所存儲的信息為易失信息,即該信息可能由 ASP.NET 移除,因此建議先確定該項(xiàng)是否在緩存中。如果不在,則應(yīng)將它重新添加到緩存中,然后檢索該項(xiàng)。

  1. string cachedString;  
  2. if (Cache["CacheItem"] != null)  
  3. {  
  4.     cachedString = (string)Cache["CacheItem"];  
  5. }  
  6. else 
  7. {  
  8.  
  9.      //緩存不存在時  
  10.     Cache.Insert("CacheItem""Hello, World.")  
  11.     cachedString = (string)Cache["CacheItem"];  
  12. }  

ASP.NET緩存數(shù)據(jù)技巧:刪除緩存項(xiàng)

由于以下任一原因,緩存中的數(shù)據(jù)可能會自動移除:緩存已滿、該項(xiàng)已過期、依賴項(xiàng)發(fā)生更改。注意:如果調(diào)用 Insert 方法,并向緩存中添加與現(xiàn)有項(xiàng)同名的項(xiàng),則將從緩存中刪除該舊項(xiàng)。顯示刪除緩存的值:

  1. Cache.Remove("MyCacheKey"); 

ASP.NET緩存數(shù)據(jù)技巧:刪除緩存項(xiàng)時通知應(yīng)用程序

從緩存中移除項(xiàng)時通知應(yīng)用程序,可能非常有用。例如,可能具有一個緩存的報告,創(chuàng)建該報告需花費(fèi)大量的時間進(jìn)行處理。當(dāng)該報告從緩存中移除時,希望重新生成該報告,并立即將其置于緩存中,以便下次請求該報告時,用戶不必等待對此報告進(jìn)行處理。

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

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

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

2)創(chuàng)建用于在從緩存中刪除項(xiàng)時處理通知的完整類ReportManager。

  1. using System;  
  2. using System.Web;  
  3. using System.Web.Caching;  
  4. public static class ReportManager  
  5. {  
  6.     private static bool _reportRemovedFromCache = false;  
  7.       
  8.     static ReportManager() { }  
  9.       
  10.     //從緩存中獲取項(xiàng)  
  11.     public static String GetReport()  
  12.     {  
  13.         lock (typeof(ReportManager))  
  14.         {  
  15.             if (HttpContext.Current.Cache["MyReport"] != null)  
  16.             {    //存在MyReport緩存項(xiàng),返回緩存值  
  17.                 return (string)HttpRuntime.Cache["MyReport"];  
  18.             }  
  19.             else 
  20.             {   //MyReport緩存項(xiàng)不存在,則創(chuàng)建MyReport緩存項(xiàng)  
  21.                 CacheReport();  
  22.                 return (string)HttpRuntime.Cache["MyReport"];  
  23.             }  
  24.         }  
  25.     }  
  26.  
  27.     //將項(xiàng)以 MyReport 的名稱添加到緩存中,并將該項(xiàng)設(shè)置為在添加到緩存中后一分鐘過期。  
  28.     //并且該方法注冊 ReportRemoveCallback 方法,以便在從緩存中刪除項(xiàng)時進(jìn)行調(diào)用。  
  29.     public static void CacheReport()  
  30.     {  
  31.         lock (typeof(ReportManager))  
  32.         {  
  33.             HttpContext.Current.Cache.Add("MyReport",  
  34.                 CreateReport(), null, DateTime.MaxValue,  
  35.                 new TimeSpan(0, 1, 0),   
  36.                 System.Web.Caching.CacheItemPriority.Default,  
  37.                 ReportRemovedCallback);  
  38.         }  
  39.     }  
  40.  
  41.     //創(chuàng)建報告,該報告時MyReport緩存項(xiàng)的值  
  42.     private static string CreateReport()  
  43.     {  
  44.         System.Text.StringBuilder myReport =   
  45.             new System.Text.StringBuilder();  
  46.         myReport.Append("Sales Report< br />");  
  47.         myReport.Append("2005 Q2 Figures< br />");  
  48.         myReport.Append("Sales NE Region - $2 million< br />");  
  49.         myReport.Append("Sales NW Region - $4.5 million< br />");  
  50.         myReport.Append("Report Generated: " + DateTime.Now.ToString()   
  51.             + "< br />");  
  52.         myReport.Append("Report Removed From Cache: " +   
  53.             _reportRemovedFromCache.ToString());  
  54.         return myReport.ToString();  
  55.     }  
  56.  
  57.     //當(dāng)從緩存中刪除項(xiàng)時調(diào)用該方法。  
  58.     public static void ReportRemovedCallback(String key, object value,   
  59.         CacheItemRemovedReason removedReason)  
  60.     {  
  61.         _reportRemovedFromCache = true;  
  62.         CacheReport();  
  63.     }  
  64. }  

不應(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)時處理回調(diào)的方法仍然存在,請使用該方法的靜態(tài)類。但是,靜態(tài)類的缺點(diǎn)是需要保證所有靜態(tài)方法都是線程安全的,所以使用lock關(guān)鍵字。

本文來自菩提屋:《緩存應(yīng)用程序數(shù)據(jù)(二)》

【編輯推薦】

  1. ASP.NET緩存數(shù)據(jù)添加方法一覽
  2. ASP.NET緩存機(jī)制基礎(chǔ)概念
  3. 再談ASP.NET緩存機(jī)制:開發(fā)效率與優(yōu)化的平衡
  4. .NET分布式緩存之Memcached執(zhí)行速度檢測
  5. 如何避免ASP.NET緩存占用系統(tǒng)資源
責(zé)任編輯:yangsai 來源: 菩提屋
相關(guān)推薦

2009-07-30 08:49:58

ASP.NET中usi

2009-08-03 18:47:12

ASP.NET數(shù)據(jù)緩存

2009-08-03 18:35:51

ASP.NET數(shù)據(jù)緩存

2009-07-24 11:24:33

ASP.NET中文亂碼

2009-07-31 10:23:44

緩存頁面ASP.NET緩存

2009-07-24 12:14:17

asp.net技巧

2009-07-31 09:57:47

ASP.NET數(shù)據(jù)庫緩

2009-08-04 15:22:33

ASP.NET緩存機(jī)制

2009-07-29 14:35:34

頁面輸出緩存ASP.NET

2009-07-31 10:33:54

ASP.NET頁面輸出

2009-07-29 10:35:51

ASP.NET緩存

2009-01-03 09:34:30

ASP.NET.NET性能優(yōu)化

2009-07-29 10:52:09

數(shù)據(jù)采集程序ASP.NET技巧

2009-05-11 13:48:00

ASP.NET 2.0緩存效率

2009-08-17 16:59:36

ASP.NET緩存機(jī)制

2009-07-29 15:34:13

2009-07-29 13:32:06

ASP.NET控件使用

2009-07-29 13:42:25

ASP.NET注釋

2009-08-17 17:19:00

ASP.NET緩存數(shù)據(jù)

2009-07-23 13:47:46

ASP.NET數(shù)據(jù)緩存
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號