Windows Azure AppFabric Caching入门简介

 

Windows Azure AppFabric Caching

入门简介

什么是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 相同的API来操作缓存。但是其中有一些例外,具体请参考这里

 

如何使用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 细节请浏览这里