使用微软分布式缓存服务Velocity(Windows Server AppFabric Caching Service)
概述
配置模型
<?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>
缓存复杂数据类型
[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; }
}
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;
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);
使用分区
public void ClearRegion(string region);
public bool CreateRegion(string region, bool evictable);
public bool RemoveRegion(string region);
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);
使用标签
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 });
GetAllMatchingTags(string region, Tag[] tags)
GetAnyMatchingTag(string region, Tag[] tags)
GetByTag(string region, Tag tag)
string regionName = "Customers";
Tag[] tags = new Tag[] { new Tag("Beijing"),
new Tag("Tianjin")};
List<KeyValuePair<string, object>> result
= cache.GetAllMatchingTags(regionName, tags);
ASP.NET SessionState提供者
<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 mode="Custom" customProvider="Velocity">
<providers>
<add name="Velocity"
type="System.Data.Caching.SessionStoreProvider,ClientLibrary"
cacheName="default"/>
</providers>
</sessionState>
总结
本文出自 “TerryLee技术专栏” 博客,请务必保留此出处http://terrylee.blog.51cto.com/342737/151964
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。