什么是 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细节 请浏览 这里 。
相关推荐
第3 章到第7 章,通过完整的示例项目系统地介绍windows azure(计算服务、存储服务和部署调试)、sql azure 和windows azure appfabric。最后一章介绍了windows azure平台的高级应用,包括更加深入的功能点、如何在...
《Windows Azure从入门到精通》针对微软的云计算平台windows azure,循序渐进地介绍了如何构建和管理云端的可扩展应用,一次一个知识点,同时辅之以适当的练习,可帮助读者轻松掌握基本的编程技能,掌握windows ...
Windows Azure使用入门 第二课:建立自己的网站.pdf
windows azure sdk 从入门到精通
Windows Azure从入门到精通含源代码,接受微软的云服务平台Windows Azure帮助包含源代码
Queue存储服务与应用程序问通信、云上的关系数据库:SQL Azure、云上的访问控制技术:AppFabric Access Control、云服务的集散中心:AppFabric Service Bus、Windows Azure与其他平台的互操作性、云计算与Windows ...
Windows Azure从入门到精通【高清】
Windows Azure使用入门 第一课:创建虚拟机.pdf
Windows Azure快速入门——为什么选择Windows Azure
Windows Azure入门教学系列
Teach yourself how to build and host scalable applications in the ...Connect several cloud-based applications with Windows Azure AppFabric Design a multitiered solution that can scale to meet user demand
.Azure从入门到精通。
Windows Azure快速入门——认识云计算
第3章到第7章,通过完整的示例项目系统地介绍Windows Azure(计算服务、存储服务和部署调试)、SQL Azure 和Windows Azure AppFabric。最后一章介绍了Windows Azure平台的高级应用,包括更加深入的功能点、如何在项目...
Windows Azure
Windows Azure使用入门 第五课:用户认证上下打通.pdf
这是一个关于在Windows Azure上使用Cache的ppt,对比了Cache Service、Redis、MemCache等等。
简要描述Windows Azure最新的功能、特色和主要的服务
Windows Azure入门指南,帮助读者了解Windows Azure使用开发
This caching services uses features that are not available in Windows Azure Cache, so you cannot substitute Azure cache for the distributed cache service. One other thing that’s important to ...