什么是 Windows Azure AppFabric Caching

Windows Azure AppFabric Caching 是一个专为 Windows Azure 应用 ( 当然也可以用于常规应用 ) 设计的分布式内存缓冲,它是 Windows Azure AppFabric 内所提供的服务之一。 它能带来如下好处:

1. 极大的提高了 Windows Azure 应用的数据访问速度

2. 以云服务的方式提供,简便,用户无需管理和安装

3. 基于 Windows Server AppFabric Caching 功能,提供了用户熟知的编程模式。


缓存通过暂时地存储来自其他后端资源的信息,提高应用的性能表现,对于诸如 ASP.NET 等应用来说是一项不可或缺的特性。

Windows Server AppFabric Caching的差别

Windows Azure AppFabric LABS 版本 SDK 支持 Windows Server AppFabric caching 所有特性的一个子集,具体差别如下:

通知 Notifications

Windows Azure AppFabric caching 不支持通知机制,这意味着您不能使用通知来使本地缓存失效。 Windows Azure AppFabric 中,本地缓存只能使用基于超时的失效策略

过期和逐出 Expiration and Eviction

缓存默认的过期时间是 10 分钟。和 Windows Server AppFabric 不同,我们无法改变该默认值。我们只能手动以编程的方法为每一个加入缓存中的项目设置过期时间。

Windows Azure AppFabric caching 会有内存逐出情况的发生。在内存使用的压力下,缓存中项目被逐出内存的情况时有发生。所以应用的设计应该随时考虑到项目可能会缺失,并需要重新载入。

高可用性 High Availability

Windows Azure AppFabric caching 并不支持高可用性。

API支持 API Support

多数情况下,您可以使用与 Windows Server AppFabric 相同的 AP I 来操作缓存。但是其中有一些例外,具体请参考 这里

如何使用 Windows Azure AppFabric Caching

下载 SDK:

您可以从 这里 下载到 Windows Azure AppFabric SDK V2.0 CTP ,同样的在该页面您可以下载到一些示例代码。

管理您的 LABS账户:

Windows Azure AppFabric Caching需要您拥有一个 AppFabric LABS账号。

· 浏览 https://portal.appfabriclabs.com/ ,输入您的 live ID账号以登陆。

· 点击 create project,输入 project name,点击 ok

· 创建完成后,点击刚才所建项目,点击 Add Service Namespace,输入 Service Namespace,点击 create

· 创建完成后,在 Service Namespace列表页面,在 Manage栏目下,选择 Cache

· 页面跳转后,您能看到 Cache的账户信息,包括服务 URL,端口,以及验证令牌。

使用 Cache

我们可以通过配置文件或者代码来使用 Windows Azure AppFabric Caching

我们首先来演示一下代码如何工作的:

// 声明 cache host 的数组

DataCacheServerEndpoint[] servers = new DataCacheServerEndpoint[1];

servers[0] = new DataCacheServerEndpoint("[your service url here]" , 22233);

// 配置 DataCacheSecurity

string strAuthToken = "[your token here]" ;

DataCacheSecurity factorySecurity = new DataCacheSecurity(strAuthToken);

// 配置 DataCacheFactory

DataCacheFactoryConfiguration factoryConfig = new DataCacheFactoryConfiguration();

factoryConfig.Servers = servers;

factoryConfig.SecurityProperties = factorySecurity;

factoryConfig.IsRouting = false ;

// 创建一个 DataCacheFactory 对象

DataCacheFactory cacheFactory = new DataCacheFactory(factoryConfig);

// 为默认的缓存返回一个客户端 Get a cache client for the default cache.

DataCache defaultCache = cacheFactory.GetDefaultCache();

// 增加并随后从默认缓存中取回一个测试对象

defaultCache.Add("testkey" , "testobject" );

string strObject = (string )defaultCache.Get("testkey" );

我们看到代码是非常简单的,主要步骤是进行一些配置的设置(服务 URL, 端口,验证令牌等),通过 DataCacheFactory 工厂方法返回一个最终能够操作 Cache DataCache 对象。通过该对象可以向缓存中添加项目,取回项目,进行缓存项目版本的提升等等。

接下来我们看看,如何通过配置文件省略用代码进行配置的步骤。

代码配置文件如下:

相应代码:

// 无参构造函数读会取配置文件

DataCacheFactory cacheFactory = new DataCacheFactory();

DataCache cache = cacheFactory.GetDefaultCache();

// 增加并随后从默认缓存中取回一个测试对象

defaultCache.Add("testkey" , "testobject" );

string strObject = (string )defaultCache.Get("testkey" );

只要可以被序列化的对象,且大小小于缓存设置的最大传输量,都可以被放入缓存中。

我们可以看到上述两种方法都是等效的,您可以根据实际需要来决定使用哪种方法。 Windows Azure AppFabric Caching提供了非常简洁 ,明了的 API ,用户可以快速入门。同时,作为云服务提供的该特性,已经为用户减免了大量管理和维护工作。 Windows Azure AppFabric Caching还能为 ASP.NET程序保存会话状态 ,您甚至无需编写代码,只需增加配置文件便能利用如此方便的特性。

更多详细关于 Windows Azure AppFabric Caching细节 请浏览 这里