Windows Server AppFabric 最新的功能,就是 Caching 了。
天啊!Caching 不早就有了嗎?在 System.Web.Caching 有一票的 API,而 Enterprise Library 也早就有 Cache Application Block,為何微軟還要多此一舉地再加一個 AppFabric Caching 呢?
原來,System.Web.Caching 是最單純的 API 內建於 .NET Framework 中,但在使用上並不方便。而 Enterprise Library 的 Cache Application Block 將它方便地以設定的方式建立Cache,並以簡單的方式來存取 Cache。雖然如此,上述兩者仍然逃不出「只能在同一個 Process中存取」的命運。簡單的來說:「自己建的Cache 只能自己用」的限制。
而 AppFabric 的 Caching 功能,就完整地介紹了整個 Cache 的生命週期,包含 Cache 的管理、授權、統計、存取等方面。以下就簡單的介紹 Cache 基本的操作。
- 啟動 Cache Cluster
- 授權
- 建立 Cache
- 加入項目到 Cache
- 讀取 Cache
- 在 Cache 中移除項目
在這些實作之前,請記得做 安裝 AppFabric 步驟
啟動 Cache Cluster
AppFabric Caching 也是一個 Cluster (叢集)的概念,見Windows Server AppFabric Caching Physical Architecture Diagram。雖然我們只建立了一個 Cache Server,但在維運上仍然需要啟動 Cluster。
請在程式集中,以管理員權限執行 Caching Adminstration Windows PowerShell。

輸入下列指令,可以列出 PowerShell 中含有 cache 的指令
get-command *cache*
輸入下列指令,以啟動 Cluster
Start-CacheCluster
授權
在微軟新的設計中,安全性考量已經是週詳地被考慮過了,預設沒有人可以存取。因此我們需要授權給適當的帳號。如下面的指令:
Grant-CacheAllowedClientAccount domain\username
建立 Cache
我們可以建立多個 DataCache,每個DataCache 內可以有多個 CacheItem。我們必須先建立一個 DataChche 並指定其名稱為 emps。指令:New-Cache
PS C:\Windows\system32> New-Cache
cmdlet New-Cache at command pipeline position 1
Supply values for the following parameters:
CacheName: emps
加入項目到 Cache
接下來,就是程式的部份了。首先建立一個Console Application,並以瀏覽目錄的方式選到“%windir%\Sysnative\AppFabric” 這個目錄,加上參考
Microsoft.ApplicationServer.Caching.Client
Microsoft.ApplicationServer.Caching.Core
我建立個一個為 Employee 的類別。注意到為了將資料放到不同 Process 中,類別必須是 Serializable。
using System;
namespace AppFabric_CachingTest1
{
[Serializable]
class Employee
{
public string Name { get; set; }
public int Salary { get; set; }
}
}
接下來,建立一個 Employee 的 instance 並放到 cache 中。
private static void PutToCache()
{
var myCacheFactory = new DataCacheFactory();
var emps = myCacheFactory.GetCache("emps"); //取得 emps 的 DataCache
emps.Put("EmpCharles", new Employee() { Name = "Charles", Salary = 30000 });
Console.WriteLine("Put emp Charles to cache");
}
而 Cache Cluster 的位置,Port 等資訊,可以在 config 中設定即可。
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="dataCacheClient"
type="Microsoft.ApplicationServer.Caching.DataCacheClientSection,
Microsoft.ApplicationServer.Caching.Core, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35"
allowLocation="true"
allowDefinition="Everywhere"/>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<dataCacheClient>
<hosts>
<host name="localhost" cachePort="22233"/>
</hosts>
</dataCacheClient>
</configuration>
讀取 Cache
下面的程式相當簡單,與一般的處理 Cache 邏輯並沒有差別,就不廢言了。
private static void GetFromCache()
{
var myCacheFactory = new DataCacheFactory();
var emps = myCacheFactory.GetCache("emps");
var charles = (Employee)emps.Get("EmpCharles");
Console.WriteLine("Name:{0}, Salay:{1}", charles.Name, charles.Salary);
}
在 Cache 中移除項目
private static void RemoveFromCahce()
{
Console.WriteLine("remove cache");
var myCacheFactory = new DataCacheFactory();
var emps = myCacheFactory.GetCache("emps");
emps.Remove("EmpCharles");
var o = emps.Get("EmpCharles");
Console.WriteLine(" o is null ? {0}", o == null);
}
統計
好玩的是,AppFabric Caching 還有統計的功能。
Get-CacheStatistics
統計輸出如下
PS C:\Windows\system32> Get-CacheStatistics
cmdlet Get-CacheStatistics at command pipeline position 1
Supply values for the following parameters:
CacheName: emps
Size : 0
ItemCount : 0
RegionCount : 1
RequestCount : 14
MissCount : 5
當然,還有效能計數器可以看。

結論
在 Windows Server AppFabric 中,看起來好像複雜了很多。但事實上,真正複雜的是架構與管理的部份。在程式碼的部份來看,相對來說簡單很多,與其他的 Cache Framework 並沒有差別。
範例程式下載
參考
Introduction to Caching with Windows Server AppFabric (Beta 1)
Adding a Reference to AppFabric Cache Client-Cache assemblies
保哥的 Windows Server AppFabric Caching (Velocity) 心得筆記