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

概述

Velocity是微軟推出的分佈式緩存解決方案,爲開發可擴展性,可用的,高性能的應用程提供支持,可以緩存各種類型的 數據,如CLR對象、XML、二進制數據等,並且支持集羣模式的緩存服務器。Velocity也將集成在.NET Framework 4.0中,本文將介紹Velocity中的配置模型、緩存複雜數據和創建分區、使用標籤以及ASP.NET SessionState提供者。

配置模型

在本文開始之前,先簡單介紹一下Velocity中的配置模型,主要包括三方面的配置,緩存集羣的配置,緩存宿主服務器配置以及應用程序的配置,如下圖所示:

Velocity_001

緩存集羣的配置,可以基於XML、SQL Server CE或者SQL Server數據庫來進行存儲,包括各個服務器以及所有的命名緩存、是否過期等配置,當我們使用Windows PowerShell管理工具進行配置時,將會修改該配置文件,如下代碼所示:

<?xml version="1.0" encoding="utf-8"?>
<
configuration>
<
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中的分區功能,它會在邏輯上把各個命名緩存再進行分區,這樣可以完全保持數據隔離,如下圖所示:

TerryLee_0216

圖中共有三個命名緩存,其中在緩存Catalog中又分區爲Sports和Arts。在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_002

同樣在檢索緩存數據時,仍然可以使用分區名進行檢索。

使用標籤

在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的支持,希望對大家有用。



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