概述
Velocity是微軟推出的分布式緩衝解決方案,為開發可擴充性,可用的,高效能的應用程提供支援,可以緩衝各種類型的資料,如CLR對象、XML、位元據等,並且支援叢集模式的快取服務器。Velocity也將整合在.NET Framework 4.0中,本文將介紹Velocity中的悲觀鎖定,快取項目版本、日誌記錄、用戶端緩衝以及路由表等知識。
悲觀鎖定
在Velocity提供了一套悲觀鎖定模型,即在某個快取項目資料處理過程中,資料將處於鎖定狀態,來自於其它用戶端應用程式將無法對該快取項目進行處理。提供悲觀鎖定的方法主要三個,如下代碼所示:
GetAndLock():擷取快取項目並對資料加鎖;
PutAndUnlock():更新加鎖的資料並釋放鎖;
Unlock():釋放鎖定。
先來看GetAndLock()方法,在擷取快取項目時並加鎖,此時如果其它用戶端試圖擷取該資料並加鎖(即調用GetAndLock方法)將會失敗,而不會阻塞;但用戶端如果只想擷取資料(即調用Get方法),則會返回相應資料,可以用圖1形象的來表示:
圖 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表示:
圖 2
用戶端緩衝
在Velocity中還支援用戶端緩衝,如果啟用了用戶端緩衝後,在從緩衝叢集中取回資料時,將會放在用戶端緩衝中,這樣下次取資料時將會直接從用戶端緩衝中取出,能夠極大的提高效率,有點像是緩衝的緩衝。當叢集中的資料發生變化時,Velocity將會使用事件通知機制通知用戶端緩衝重新整理資料,3所示:
圖 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所示:
圖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所示:
圖 5
總結
本文介紹了Velocity中的悲觀鎖定,快取項目版本、日誌記錄、用戶端緩衝以及路由表等知識,希望對大家有用。至此,關於微軟的分布式快取服務Velocity就用短短的三篇文章介紹到這裡,期待在.NET Framework 4.0中Velocity能夠為我們帶來更多的驚喜。
相關文章:
1. 使用微軟分布式快取服務Velocity Part 1
2. 使用微軟分布式快取服務Velocity Part 2