AppFabric的开发相对还是很简单的,最常见的方法无非是声明一个缓存接口,然后由各种缓存实现.

具体的使用除了msdn:

http://msdn.microsoft.com/zh-cn/library/hh334305
这是实例包的下载地址:

http://www.microsoft.com/en-us/download/confirmation.aspx?id=19603

这篇博客介绍得不错.

使用微软分布式缓存服务Velocity(Windows Server AppFabric Caching Service)

概述

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

配置模型

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

缓存集群的配置,可以基于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中的分区功能,它会在逻辑上把各个命名缓存再进行分区,这样可以完全保持数据隔离,如下图所示:

图中共有三个命名缓存,其中在缓存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还允许对加入到缓存中的缓存项设置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.blog.51cto.com/342737/151964

AppFabric 使用相关推荐

  1. Windows Azure AppFabric概述

    公告    :本博客为微软云计算中文博客  的镜像博客.   部分文章因为博客兼容性问题  ,会影响阅读体验  .如遇此情况,请访问  原博客    . Windows Azure AppFabric ...

  2. Windows Server AppFabric Caching

    这套 AppFabric Caching 比我用过的 memcached 复杂多了,MSDN有一篇文章进行介绍Introduction to Caching with Windows Server A ...

  3. Windows Server AppFabric Beta 2 for For Vistual Studio 2010已经发布

    Windows Server AppFabric Beta 2 For Vistual Studio 2010/.NET Framework 4.0已经发布了,参看EndPonit上的博客文章http ...

  4. AppFabric Caching Admin Tool

    最近试用了一下MS的AppFabric的分布是缓存(Velocity),发现了一个不错的可视化配置工具挺有用,先介绍给大家,后续再详细介绍. http://mdcadmintool.codeplex. ...

  5. SharePoint2013安装组件时AppFabric时出现1603错误,解决方法:

    采用PowerShell命令批量下载必备组件: 下载完成后,采用批处理命令安装必备组件. 注:SPS2013安装必备组件及批处理下载地址: 需要将必备组件放在安装文件的PrerequisiteInst ...

  6. Windows Server AppFabric Caching支持大数据量的配置

    Memcache支持的数据量大小为1M,最新版本可以通过配置调整突破1M(参看http://www.cnblogs.com/shanyou/archive/2010/02/01/1661271.htm ...

  7. 使用AppFabric 承载WCF和WF服务-安装和使用

    AppFabric 承载服务步骤 对于包含WCF和WF的服务,如果想长期的运行和管理维护,AppFabric无疑是个很好的选择.具体步骤: 参考http://www.cnblogs.com/2018/ ...

  8. AppFabric客户端的一些配置(Microsoft.Web.DistributedCache)

    通过使用Microsoft.Web.DistributedCache可直接将AppFabric Cache用于Session与Cache存储.直接贴配置,很简单. 1.配置configSections ...

  9. SharePoint 2013必备组件离线包安装:AppFabric无法安装问题解决

    SharePoint 2013必备组件离线包安装:AppFabric无法安装问题解决 参考文章: (1)SharePoint 2013必备组件离线包安装:AppFabric无法安装问题解决 (2)ht ...

  10. Windows Azure AppFabric 入门教学(七):多播(Multicast)

    公告:本博客为微软云计算中文博客的镜像博客.部分文章因为博客兼容性问题,会影响阅读体验.如遇此情况,请访问原博客. 本文是Windows Azure AppFabric入门教学的第七篇文章.我们知道, ...

最新文章

  1. java handler类_java——Handler类
  2. Linux文字分段裁剪命令cut(转)
  3. C#中Lambda表达式类型Expression不接受lambda函数
  4. SMB MS17-010 利用(CVE-2017-0144 )
  5. win10开机时不显示锁屏壁纸
  6. linux 命令学习 —— 硬件外设管理(dmesg、lsusb)
  7. kafka/producer.lua:168: attempt to perform arithme
  8. 火山视窗类库静态编译一览表,以及所需求的vs版本
  9. 安装VirtualBox的虚拟机增强功能
  10. 8种经典的统计学悖论18种经典的哲学悖论
  11. 一小时看懂Ruby代码基本逻辑(自定义metasploit模块)
  12. Python--Turtle钟表
  13. python报错:Empty suite
  14. Android7工程模式,安卓手机进入各种工程模式快捷键小结
  15. 全国二级计算机考试准考证打印官网
  16. 电视连接WiFi中心服务器异常,网络电视登录失败怎么办?为什么电视连接wifi常常显示登陆失败?...
  17. 微信支付开发 认清微信支付v2和v3
  18. 《悲惨世界》--[法]雨果
  19. 《2023年金融科技趋势展望》发布,提出十大技术趋势
  20. python 拼音库_python有没有拼音库python进阶之socket详解

热门文章

  1. FasterRCNN调试笔记
  2. 如何实现高性能的在线 PDF 预览
  3. 计算机二级MS office之excel常用函数
  4. UTM坐标和WGS84坐标转换
  5. 抖音网红简易时钟代码
  6. 用python做一个抖音上很火的罗盘时钟
  7. 计算机编程游戏本还是商务本,游戏本和商务本哪个比较适合编程?
  8. NS2 学习笔记—— AODV协议分析
  9. 安装laravel8
  10. 初中计算机成绩评定方案,初中信息技术学科评价方案