配置好快取叢集時,在windows services 中啟動名為:Microsoft project code named “Velocity”的服務,該服務預設不會自動啟動
1. 將Volocity的程式集copy到開發機器上。程式集位於Velocity的安裝目錄。程式集如下:
CacheBaseLibrary.dll,
ClientLibrary.dll,
FabricCommon.dll,
CASBase.dll.
2.在VS的工程中添加以上程式集的引用
3.配置快取用戶端的xml設定檔一般為.config檔案,或直接在代碼裡指定配置
選擇用Routing Cleing或Simple Client,選擇Routing Client會獲得最佳的效能。
Demo:
下面是一個Routing Client的配置樣本。樣本中未啟用本機快取,但使用了兩個緩衝主機:CacheServer1和CacheServer2
<?xml version="1.0" encoding="utf-8" ?><configuration> <!--configSections must be the FIRST element --> <configSections> <!-- required to read the <dataCacheClient> element --> <section name="dataCacheClient" type="Microsoft.Data.Caching.DataCacheClientSection, CacheBaseLibrary" allowLocation="true" allowDefinition="Everywhere"/> <!-- required to read the <fabric> element, when present --> <section name="fabric" type="System.Data.Fabric.Common.ConfigFile, FabricCommon" allowLocation="true" allowDefinition="Everywhere"/> </configSections> <!-- routing client--> <dataCacheClient deployment="routing"> <!-- (optional) specify local cache <localCache isEnabled="true" sync="TTLBased" objectCount="100000" ttlValue="300" /> --> <!--(optional) specify cache notifications poll interval <clientNotification pollInterval="300" /> --> <!-- cache host(s) --> <hosts> <host name="CacheServer1" cachePort="22233" cacheHostName="DistributedCacheService"/> <host name="CacheServer2" cachePort="22233" cacheHostName="DistributedCacheService"/> </hosts> </dataCacheClient></configuration>
下面代碼示範如何在代碼中指定配置
//declare array for cache host(s)DataCacheServerEndpoint[] servers = new DataCacheServerEndpoint[1];//specify cache host(s)servers[0] = new DataCacheServerEndpoint("CacheServer2", 22233, "DistributedCacheService");//specify cache client configurationDataCacheFactory mycacheFactory = new DataCacheFactory(servers, true, false);//get cache client for cache "NamedCache1"DataCache myDefaultCache = mycacheFactory.GetCache("NamedCache1");