分布式缓存: Velocity之集群应用

概述 数据库

Velocity是微软推出的分布式缓存解决方案,为开发可扩展性,可用的,高性能的应用程提供支持,能够缓存各类类型的数据,如CLR对象、XML、二进制数据等,而且支持集群模式的缓存服务器。Velocity也将集成在.NET Framework 4.0中,本文将介绍Velocity中的配置模型、缓存复杂数据和建立分区、使用标签以及ASP.NET SessionState提供者。 编程

配置模型 缓存

在本文开始以前,先简单介绍一下Velocity中的配置模型,主要包括三方面的配置,缓存集群的配置,缓存宿主服务器配置以及应用程序的配置,以下图所示: 服务器

缓存集群的配置,能够基于XMLSQL Server CE或者SQL Server数据库来进行存储,包括各个服务器以及全部的命名缓存、是否过时等配置,当咱们使用Windows PowerShell管理工具进行配置时,将会修改该配置文件,以下代码所示: session

<?xml version="1.0" encoding="utf-8"?> 分布式

<configuration> ide

<configSections> 工具

<section name="dcache" type="System.Data.Caching.DCacheSection, 性能

CacheBaseLibrary, Version=1.0.0.0, Culture=neutral, 测试

PublicKeyToken=89845dcd8080cc91" />

</configSections>

<dcache cluster="localhost" size="Small">

<caches>

<cache type="partitioned" consistency="strong" name="default">

<policy>

<eviction type="lru" />

<expiration defaultTTL="10" isExpirable="true" />

</policy>

</cache>

<cache type="partitioned" consistency="strong" name="other">

<policy>

<eviction type="lru" />

<expiration defaultTTL="10" isExpirable="true" />

</policy>

</cache>

</caches>

<hosts>

<host clusterPort="22234" hostId="1319514812" size="1024" quorumHost="true"

name="TERRYLEE-PC" cacheHostName="DistributedCacheService"

cachePort="22233" />

</hosts>

<advancedProperties>

<partitionStoreConnectionSettings providerName="System.Data.SqlServerCe.3.5"

connectionString="D:\CacheShare\ConfigStore.sdf" />

</advancedProperties>

</dcache>

</configuration>

在上一篇的示例中,并无使用应用程序配置文件,事实上使用配置文件是更好的编程实践,首先须要添加一个配置区:

<section name="dcacheClient"

type="System.Data.Caching.DCacheSection,

CacheBaseLibrary, Version=1.0.0.0,

Culture=neutral, PublicKeyToken=89845dcd8080cc91"/>

配置信息包括部署方式,是否启用本地缓存以及缓存宿主等,以下代码所示:

<dcacheClient>

<localCache isEnabled="true" sync="TTLBased" ttlValue="300" />

<hosts>

<host name="localhost" cachePort="22233"

cacheHostName="DistributedCacheService"/>

</hosts>

</dcacheClient>

如今Velocity CTP2对于应用程序使用配置的支持彷佛有些问题。缓存宿主的配置放在DistributedCache.exe.config文件中,能够在Velocity安装目录下找到。

缓存复杂数据类型

Velocity中,能够缓存任何类型的数据,如CLR对象、XML或者二进制数据等。如今看一个简单的示例,如何缓存复杂类型数据,定义一个以下的Customer类,注意要可以序列化:

[Serializable]

public class Customer

{

public String ID { get; set; }

 

public String FirstName { get; set; }

 

public String LastName { get; set; }

 

public int Age { get; set; }

 

public String Email { get; set; }

}

对应用程序作配置,参考本文的配置模型部分,使用方法与简单数据类型的基本一致,如添加缓存项,使用Customer主键做为缓存键,其中GetCurrentCache()方法的实现请参考上一篇文章:

Cache cache = GetCurrentCache();

Customer customer = new Customer()

{

ID = "C20081117002",

FirstName = "Terry",

LastName = "Lee",

Age = 25,

Email = "lhj_cauc[#AT#]163.com"

};

 

cache.Add(customer.ID, customer);

获取缓存项:

Cache cache = GetCurrentCache();

Customer customer = cache.Get("C20081117002") as Customer;

移除缓存项:

Cache cache = GetCurrentCache();

cache.Remove("C20081117002");

更新缓存中数据,能够有两种方法,一是直接使用缓存索引,若是确保缓存键存在:

Cache cache = GetCurrentCache();

Customer customer = new Customer()

{

ID = "C20081117002",

FirstName = "Huijui",

LastName = "Li",

Age = 26,

Email = "lhj_cauc[#AT#]163.com"

};

cache["C20081117002"] = customer;

另一种是使用Put方法,若是缓存键不存在,它将会新增到缓存中,不然会进行覆盖,以下代码所示:

Cache cache = GetCurrentCache();

Customer customer = new Customer()

{

ID = "C20081117002",

FirstName = "Huijui",

LastName = "Li",

Age = 26,

Email = "lhj_cauc[#AT#]163.com"

};

cache.Put(customer.ID, customer);

使用分区

在实际部署中,常常会出现多个应用程序共享同一个缓存集群,这不可避免的会出现缓存键冲突,如上面的示例中使用CustomerID做为缓存键,此时可使用Velocity中的分区功能,它会在逻辑上把各个命名缓存再进行分区,这样能够彻底保持数据隔离,以下图所示:

图中共有三个命名缓存,其中在缓存Catalog中又分区为SportsArts。在Velocity中对于分区的操做提供了以下三个方法,能够用于建立分区,删除分区以及清空分区中全部的对象:

public void ClearRegion(string region);

public bool CreateRegion(string region, bool evictable);

public bool RemoveRegion(string region);

以下代码所示,建立了一个名为"Customers"的分区,在调用Add方法时能够指定数据将会缓存到哪一个分区:

Cache cache = GetCurrentCache();

string regionName = "Customers";

cache.CreateRegion(regionName, false);

 

Customer customer = new Customer()

{

ID = "C20081117003",

FirstName = "Terry",

LastName = "Lee",

Age = 25,

Email = "lhj_cauc[#AT#]163.com"

};

 

cache.Add(regionName, customer.ID, customer);

可使用Get-CacheRegion命令在Windows PowerShell中来查看一下当前缓存集群中全部的分区信息,以下图所示:

一样在检索缓存数据时,仍然可使用分区名进行检索。

使用标签

Velocity还容许对加入到缓存中的缓存项设置Tag,能够是一个或者多个,使用了Tag,就能够从多个方面对缓存项进行描述,这样在检索数据时,就能够根据Tag来一次检索多个缓存项。为缓存项设置Tag,以下代码所示:

Cache cache = GetCurrentCache();

string regionName = "Customers";

 

Customer customer1 = new Customer()

{

ID = "C20081117004",

FirstName = "Terry",

LastName = "Lee",

Age = 25,

Email = "lhj_cauc[#AT#]163.com"

};

Customer customer2 = new Customer()

{

ID = "C20081117005",

FirstName = "Terry",

LastName = "Lee",

Age = 25,

Email = "lhj_cauc[#AT#]163.com"

};

 

Tag tag1 = new Tag("Beijing");

Tag tag2 = new Tag("Tianjin");

cache.Add(regionName, customer1.ID, customer1, new Tag[] { tag1, tag2 });

cache.Add(regionName, customer2.ID, customer2, new Tag[] { tag2 });

这样就能够对设置了Tag的缓存项进行检索,根据实际需求选择使用以下三个方法之一:

GetAllMatchingTags(string region, Tag[] tags)

GetAnyMatchingTag(string region, Tag[] tags)

GetByTag(string region, Tag tag)

第一个检索匹配全部Tag的数据,第二个检索匹配全部Tag中的任意一个便可,最后只使用一个Tag,以下代码所示:

string regionName = "Customers";

Tag[] tags = new Tag[] { new Tag("Beijing"),

new Tag("Tianjin")};

 

List<KeyValuePair<string, object>> result

= cache.GetAllMatchingTags(regionName, tags);

使用Tag功能对于检索缓存项提供了极大的灵活性,对于任何一个数据,均可以使用多个Tag从不少方面去描述它。

ASP.NET SessionState提供者

Velocity还提供了对于ASP.NET SessionState提供者的支持,能够经过配置把Session信息缓存到缓存集群中,添加Velocity配置区:

<section name="dcacheClient"

type="System.Data.Caching.DCacheSection,

CacheBaseLibrary, Version=1.0.0.0,

Culture=neutral, PublicKeyToken=89845dcd8080cc91"/>

配置缓存客户端信息:

<dcacheClient>

<localCache isEnabled="true" sync="TTLBased" ttlValue="300" />

<hosts>

<host name="localhost" cachePort="22233"

cacheHostName="DistributedCacheService"/>

</hosts>

</dcacheClient>

配置SessionState信息:

<sessionState mode="Custom" customProvider="Velocity">

<providers>

<add name="Velocity"

type="System.Data.Caching.SessionStoreProvider,ClientLibrary"

cacheName="default"/>

</providers>

</sessionState>

须要指定使用哪一个命名缓存,可是该功能彷佛到目前还存在问题,没法测试经过L

总结

本文简单介绍了Velocity的配置模型,以及如何缓存复杂数据类型,对命名缓存分区,为缓存项设置Tag,以及对于ASP.NET SessionState的支持,但愿对你们有用。

相关文章
相关标签/搜索