使用微軟分佈式緩存服務Velocity Part 3——博客園 Terrylee

概述

Velocity是微軟推出的分佈式緩存解決方案,爲開發可擴展性,可用的,高性能的應用程提供支持,可以緩存各種類型的 數據,如CLR對象、XML、二進制數據等,並且支持集羣模式的緩存服務器。Velocity也將集成在.NET Framework 4.0中,本文將介紹Velocity中的悲觀鎖定,緩存項版本、日誌記錄、客戶端緩存以及路由表等知識。

悲觀鎖定

在Velocity提供了一套悲觀鎖定模型,即在某個緩存項數據處理過程中,數據將處於鎖定狀態,來自於其它客戶端應用程序將無法對該緩存項進行處理。提供悲觀鎖定的方法主要三個,如下代碼所示:

GetAndLock():獲取緩存項並對數據加鎖;

PutAndUnlock():更新加鎖的數據並釋放鎖;

Unlock():釋放鎖定。

先來看GetAndLock()方法,在獲取緩存項時並加鎖,此時如果其它客戶端試圖獲取該數據並加鎖(即調用GetAndLock方法)將會失敗,而不會阻塞;但客戶端如果只想獲取數據(即調用Get方法),則會返回相應數據,可以用圖1形象的來表示:

Velocity_003

圖 1

可以看到,ClientA獲取數據成功並加鎖;ClientB再次想獲取數據並加鎖時,將會失敗;ClientC能夠獲取數據。

使用GetAndLock()方式可以指定鎖過期時間,並且會有輸出參數LockHandle,該參數將會在PutAndUnlock()方法或Unlock()中來釋放鎖,如下代碼所示:

Cache cache = GetCurrentCache();
LockHandle handle = new LockHandle();
Customer item = (Customer)cache.GetAndLock("C20081117005",
new TimeSpan(0, 30, 0), out handle);

Customer customer = new Customer()
{
ID = "C20081117005",
FirstName = "Terry",
LastName = "Lee",
Age = 25,
Email = "lhj_cauc[#AT#]163.com"
};
cache.PutAndUnlock(customer.ID, customer, handle, null);

日誌記錄

Velocity中同樣提供了日誌記錄的功能,我們可以在應用程序配置文件中進行設置,它支持基於控制檯、基於文件以及Windows事件跟蹤三種方式的記錄,在配置文件中首先添加配置區:

<section name="fabric" type="System.Data.Fabric.Common.ConfigFile, FabricCommon"
allowLocation="true" allowDefinition="Everywhere"/>

然後可以進行配置,如設置日誌記錄級別等:

<fabric>
<
section name="logging" path="">
<
collection name="sinks" collectionType="list">
<
customType className="System.Data.Fabric.Common.EventLogger,FabricCommon"
sinkName="System.Data.Fabric.Common.ConsoleSink,FabricCommon"
sinkParam="" defaultLevel="-1"/>
<
customType className="System.Data.Fabric.Common.EventLogger,FabricCommon"
sinkName="System.Data.Fabric.Common.FileEventSink,FabricCommon"
sinkParam="CacheClientLog" defaultLevel="1"/>
<
customType className="System.Data.Fabric.Common.EventLogger,FabricCommon"
sinkName="System.Data.Caching.ETWSink, CacheBaseLibrary"
sinkParam="" defaultLevel="-1" />
</
collection>
</
section>
</
fabric>

同樣也可以在代碼中設置,調用CacheFactory的兩個靜態方法CreateLogSinks和DisableLogSinks,如下代碼所示:

private Cache GetCurrentCache()
{
List<LogSink> sinklist = new List<LogSink>(2);
LogSink fileBasedSink = new LogSink(SinkType.FILE,
TraceLevel.Warning, "DCache/dd-hh-mm");
LogSink consoleBasedSink = new LogSink(SinkType.CONSOLE,
TraceLevel.Warning);
sinklist.Add(fileBasedSink);
sinklist.Add(consoleBasedSink);
// 啓用
CacheFactory.CreateLogSinks(sinklist);

// 禁用
CacheFactory.DisableLogSinks();

Cache dCache;
ServerEndPoint[] servers = new ServerEndPoint[1];
servers[0] = new ServerEndPoint("localhost", 22233, "DistributedCacheService");
bool routingClient = true;
bool localCache = false;

var factory = new CacheFactory(servers, routingClient, localCache);
dCache = factory.GetCache("default");

return dCache;
}

緩存項版本

在Velocity中提供了一種基於版本的更新功能,當使用GetCacheItem()方法時將返回一個緩存項,並攜帶有版本信息,當每次對緩存項做更新時,在內部都會對它的版本增加。如下面的示例,有兩個客戶應用程序,它們同時獲取了同一個緩存項:

ClientA

CacheItem item = cache.GetCacheItem("Customers", "C2008");

ClientB

CacheItem item = cache.GetCacheItem("Customers", "C2008");

並且同時對緩存項做修改:

ClientA

((Customer)item.CacheObject).FirstName = "Huijun";

ClientB

((Customer)item.CacheObject).FirstName = "Terry";

如果ClientA首先提交更改,在提交更改時攜帶版本信息,由於版本信息與內部的版本一致,所以提交成功:

ClientA

cache.Put("Customers", "C2008", item.CacheObject, item.Version);

此時內部版本將會增加,現在ClientB如果再提交更改,將會失敗,因爲版本無法匹配,如圖2表示:

Velocity_004

圖 2

客戶端緩存

在Velocity中還支持客戶端緩存,如果啓用了客戶端緩存後,在從緩存集羣中取回數據時,將會放在客戶端緩存中,這樣下次取數據時將會直接從客 戶端緩存中取出,能夠極大的提高效率,有點像是緩存的緩存。當集羣中的數據發生變化時,Velocity將會使用事件通知機制通知客戶端緩存刷新數據,如 圖3所示:

Velocity_005

圖 3

要啓用客戶端緩存,一是使用配置文件,設置IsEnabled屬性爲True,如下代碼所示:

<dcacheClient deployment="routing">
<
localCache isEnabled="true" sync="TTLBased" ttlValue="300" />
<
hosts>
<
host name="localhost" cachePort="22233"
cacheHostName="DistributedCacheService"/>
</
hosts>
</
dcacheClient>

直接指定啓用客戶端緩存即可,另外也可以在創建CacheFactory時指定,如下代碼所示:

Cache dCache;
ServerEndPoint[] servers = new ServerEndPoint[1];
servers[0] = new ServerEndPoint("localhost", 22233, "DistributedCacheService");
bool routingClient = true;
bool localCache = false;

var factory = new CacheFactory(servers, routingClient, localCache);
dCache = factory.GetCache("default");

return dCache;

路由客戶端

Velocity中在緩存客戶端,提供了一種路由客戶端Routing Client,它能夠提供比簡單客戶端Simple Client更好的性能,在Routing Client中會有一個路由表Routing Table,它用來跟蹤緩存對象,它是全局緩存中的分區映射的一個子集,同時分發緩存操作(Put、Get等)到確定的緩存宿主。路由客戶端使用此路由表 來優化性能,因爲該表可以跟蹤緩存對象,所以當有請求到緩存宿主時,可以進行物理上的定位。如圖4所示:

Velocity_007

圖4

是否在應用程序中啓用路由客戶端,可以由開發者來確定,如在配置中啓用路由客戶端,這裏可以通過指示deployment來設定是路由客戶端(routing)還是簡單客戶端(simple):

<dcacheClient deployment="routing">
<
localCache isEnabled="true" sync="TTLBased" ttlValue="300" />
<
hosts>
<
host name="localhost" cachePort="22233"
cacheHostName="DistributedCacheService"/>
</
hosts>
</
dcacheClient>

另外還可以通過代碼來設置,如下面的代碼,在創建CacheFactory時指定構造函數參數:

Cache dCache;
ServerEndPoint[] servers = new ServerEndPoint[1];
servers[0] = new ServerEndPoint("localhost", 22233, "DistributedCacheService");
bool routingClient = true;
bool localCache = false;

var factory = new CacheFactory(servers, routingClient, localCache);
dCache = factory.GetCache("default");

return dCache;

Velocity組成

最後我們再看一幅圖,來了解一下Velocity的組成部分,可以看到它可以分爲客戶端緩存、服務端緩存以及管理工具三部分,如圖5所示:

Velocity_006

圖 5

總結

本文介紹了Velocity中的悲觀鎖定,緩存項版本、日誌記錄、客戶端緩存以及路由表等知識,希望對大家有用。至此,關於微軟的分佈式緩存服務 Velocity就用短短的三篇文章介紹到這裏,期待在.NET Framework 4.0中Velocity能夠爲我們帶來更多的驚喜。


本文作者:TerryLee
本文出處:http://terrylee.cnblogs.com
發佈了22 篇原創文章 · 獲贊 4 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章