微软分布式缓存AppFabric(Velocity)-开发篇(一)开发环境准备
配置好缓存群集时,在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 configuration DataCacheFactory mycacheFactory = new DataCacheFactory(servers, true, false); //get cache client for cache "NamedCache1" DataCache myDefaultCache = mycacheFactory.GetCache("NamedCache1");
曾经年少多少事 而今皆付谈笑中!