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 包含了兩組核心類別:
如果你曾經用過 ASP.NET Cache 物件,因為兩者的相似性非常高,所以要上手使用 MemoryCache 那可是非常容易,以下是 MemoryCache 的使用範例
( 以 Windows Form 做範例 ):
view
plaincopy
to clipboardprint?
- private void btnGet_Click(object sender, EventArgs e)
- {
- //Obtain a reference to the default MemoryCache instance.
- //Note that you can create multiple MemoryCache(s) inside
- //of a single application.
- ObjectCache cache = MemoryCache.Default;
-
- //In this example the cache is storing the contents of a file string
- fileContents = cache["filecontents"] as string;
-
- //If the file contents are not currently in the cache, then
- //the contents are read from disk and placed in the cache.
- if (fileContents == null)
- {
- //A CacheItemPolicy object holds all the pieces of cache
- //dependency and cache expiration metadata related to a single
- //cache entry.
- CacheItemPolicy policy = new CacheItemPolicy();
-
- //Build up the information necessary to create a file dependency.
- //In this case we just need the file path of the file on disk.
- List filePaths = new List();
- filePaths.Add("c:\\data.txt");
-
- //In the new cache API, dependencies are called "change monitors".
- //For this example we want the cache entry to be automatically expired
- //if the contents on disk change. A HostFileChangeMonitor provides
- //this functionality.
- policy.ChangeMonitors.Add(new HostFileChangeMonitor(filePaths));
-
- //Fetch the file's contents
- fileContents = File.ReadAllText("c:\\data.txt");
-
- //And then store the file's contents in the cache
- cache.Set("filecontents", fileContents, policy);
-
- }
- MessageBox.Show(fileContents);
- }
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 機制載入自訂的快取機制 (例如: Velocity, Memcached, ScaleOut,
… ) 來加強網站的延展性 (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命名空間。
[size=1em][size=1em][size=1em]1 | using
System.Runtime.Caching; |
接著我們遵循著一定的開發Flow就可以了。首先記得要初始ObjectCache,這邊多半是直接取得內建的MemoryCache實體,然後我們要帶入Content的Key,試圖從ObjectCache取得快取的內容,如果有快取的內容就直接回傳,若無則我們需從介質中取得內容,設定CacheItemPolicy,將Content的Key、Content、以及CacheItemPolicy設定給ObjectCachem。
這邊實際的示範個完整的範例
[size=1em][size=1em][size=1em]