ASP.NET 4 快取 API 有两种:Cache 与 ObjectCache(MemoryCache)

 

ASP.NET 從最早期的版本就實做了一套好用的快取機制(System.Web.Caching.Cache),一直以來任何非
ASP.NET 的應用程式 (例如 WinForm, WPF, Console, WinService, …) 若要使用快取機制都必須將System.Web.dll 參考進專案才能使用,但從
.NET 4.0 開始出現了另一個擴充性更強的快取機制,稱為Object Caching (物件快取) 機制,未來這兩套快取機制將各司其職、相輔相成。

 

從 .NET 4.0 開始,.Net Framework 參考了 ASP.NET 內建的快取機制,並明確實做了另一個獨立存在的快取組件:System.Runtime.Caching.dll,此組件非常的小,我用 NDepend 工具分析了一下此組件,總共
IL 指令集僅有 9,844 個而已,而 System.Web.dll 的 IL 指令集總共有 496,395 個之多,就算只計算System.Web.Caching 命名空間的 IL 也有 14,760 個,還是比這次新增的 System.Runtime.Caching.dll來的大。

備註:原本的 System.Web.Caching.Cache 類別依然存在,功能與之前一樣,還是可以正常使用。

這個新的 System.Runtime.Caching.dll 組件在 System.Runtime.Caching 命名空間 包含了一組全新的 Cache API,而此 API 包含了兩組核心類別:

  • 透過一組抽象類別可讓你自行實做任意物件快取機制,例如: 實做分散式快取機制。
  • 透過此抽象類別實做的記憶體快取機制類別System.Runtime.Caching.MemoryCache
    類別

如果你曾經用過 ASP.NET Cache 物件,因為兩者的相似性非常高,所以要上手使用 MemoryCache 那可是非常容易,以下是 MemoryCache 的使用範例
( 以 Windows Form 做範例 ):

view
plain
copy
to clipboard
print?

  1. private void btnGet_Click(object sender, EventArgs e)   
  2. {   
  3.   //Obtain a reference to the default MemoryCache instance.   
  4.   //Note that you can create multiple MemoryCache(s) inside   
  5.   //of a single application.   
  6.   ObjectCache cache = MemoryCache.Default;   
  7.     
  8.   //In this example the cache is storing the contents of a file string   
  9.   fileContents = cache["filecontents"] as string;  
  10.     
  11.   //If the file contents are not currently in the cache, then   
  12.   //the contents are read from disk and placed in the cache.   
  13.   if (fileContents == null)   
  14.   {  
  15.     //A CacheItemPolicy object holds all the pieces of cache   
  16.     //dependency and cache expiration metadata related to a single   
  17.     //cache entry.   
  18.     CacheItemPolicy policy = new CacheItemPolicy();   
  19.       
  20.     //Build up the information necessary to create a file dependency.   
  21.     //In this case we just need the file path of the file on disk.   
  22.     List filePaths = new List();   
  23.     filePaths.Add("c:\\data.txt");   
  24.       
  25.     //In the new cache API, dependencies are called "change monitors".   
  26.     //For this example we want the cache entry to be automatically expired   
  27.     //if the contents on disk change. A HostFileChangeMonitor provides   
  28.     //this functionality.   
  29.     policy.ChangeMonitors.Add(new HostFileChangeMonitor(filePaths));   
  30.       
  31.     //Fetch the file's contents   
  32.     fileContents = File.ReadAllText("c:\\data.txt");   
  33.       
  34.     //And then store the file's contents in the cache   
  35.     cache.Set("filecontents", fileContents, policy);   
  36.       
  37.   }   
  38.   MessageBox.Show(fileContents);   
  39. }  

System.Web.Caching 中內建的 Cache 與 System.Runtime.Caching 的 MemoryCache 有幾點需注意:

  • System.Runtime.Caching.dll 組件下的 MemoryCache 類別與 System.Web.dll 組件下的 Cache類別完全沒有相依性,兩者是完全獨立的組件,任何
    ASP.NET 應用程式
    都不應該載入System.Web.dll 組件來實做快取。
  • ASP.NET 的 Cache 與 MemoryCache 雖然都是記憶體快取(In-memory
    cache),但在 ASP.NET 中只能使用一份 Cache 物件,而在 MemoryCache 可在
    AppDomain 中建立多份快取物件。

像我們之前在開發大型網站時,由於 ASP.NET 快取資料只能儲存在記憶體中,這對於大型網站來說非常不實用,甚至於”不能用”,有了 ObjectCache 機制這才出現了一絲生機,這時你就可以在
ASP.NET 中透過 ObjectCache 機制載入自訂的快取機制 (例如: VelocityMemcachedScaleOut,
… ) 來加強網站的延展性 (Scalability),相信此功能對擁有大量快取需求的開發人員來說絕對是一大福音。

.NET 4.0新增了一個System.Runtime.Caching命名空間,該命名空間主要提供了一個可擴充的資料快取框架,提供開發者使用與實作快取的功能。

在以往我們可能得自行將資料Load到記憶體中,在背景去監控是否變動或是過期需要更新,甚至是自行撰寫Cache Pool去做控管,甚至是套用LRU演算法去釋放,避免快取佔用過多的記憶體。在.NET 4.0以後,再也不用那麼麻煩了。

框架中比較重要的組成元件有ObjectCache、CacheItemPolicy、ChangeMonitor。

ObjectCache為記憶體快取框架中的主要類別,簡單的說就是一個快取池。提供存取快取所需方法和屬性,支援加入、擷取與更新快取資料,主要職責為建立管理快取的元素、指定快取回收與到期的資訊、觸發變更事件。快取框架中所提供的MemoryCache類別即為ObjectCache抽象類別的實作,可將從快取介質所讀出的資料快取到記憶體中,有點類似於ASP.NET中的Cache類別。

 

CacheItemPolicy的主要職責為告知ObjectCache內的內容何時會過期,我們可設定絕對的或是相對應的過期時間,也可與ChangeMonitor搭配使用。

在快取框架的使用上我們需先將System.Runtime.Caching.dll組件加入參考,該框架不支援Client Profile的.NET Framework,若看不到請切回.NET Framework 4。

 

並加入System.Runtime.Caching命名空間。

1using
System.Runtime.Caching;

接著我們遵循著一定的開發Flow就可以了。首先記得要初始ObjectCache,這邊多半是直接取得內建的MemoryCache實體,然後我們要帶入Content的Key,試圖從ObjectCache取得快取的內容,如果有快取的內容就直接回傳,若無則我們需從介質中取得內容,設定CacheItemPolicy,將Content的Key、Content、以及CacheItemPolicy設定給ObjectCachem。

 

這邊實際的示範個完整的範例

01using System;

转载于:https://my.oschina.net/dyc1122/blog/1014948

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值