Azure Redis Cache
将于 2014 年 9 月 1 日停止Azure Shared Cache服务,因此你需要在该日期前迁移到 Azure Redis Cache。Azure Redis Cache包含以下两个层级的产品。
- 基本版 – 单节点,多规格。
- 标准版 – 主/从双节点,多规格。标准层产品将具有 99.9% 的 SLA。
- 具体文档参看 http://azure.microsoft.com/zh-cn/documentation/articles/cache-dotnet-how-to-use-azure-redis-cache/
使用 StackExchange.Redis NuGet 程序包配置缓存客户端
以 Visual Studio 开发的 .NET 应用程序可以使用 StackExchange.Redis 缓存客户端来访问缓存。
- 要使用 StackExchange.Redis NuGet 程序包以 Visual Studio 配置客户端应用程序,请在“解决方案资源管理器”中右键单击项目,然后选择“管理 NuGet 程序包”。
- 在“联机搜索”文本框中输入 StackExchange.Redis,然后从结果中选择它并单击“安装”。
- NuGet 程序包便会下载并为客户端应用程序添加所需的程序集引用,以使用 StackExchange.Redis 缓存客户端访问 Azure Redis Cache
使用 ConnectionMultiplexer 类连接缓存
在 Azure Redis Cache中,缓存连接由 ConnectionMultiplexer
类进行管理。要连接 Azure Redis Cache 实例,请调用静态 ConnectionMultiplexer.Connect
方法并传递到终结点和密钥中,如下列中所示。
ConnectionMultiplexer connection = ConnectionMultiplexer.Connect("yhdcache0.redis.cache.windows.net,ssl=true,password=...");
ConnectionMultiplexer
被设计成在整个客户端应用程序中共享和重复使用,因此不需要在每次执行操作时都加以创建。如果创建实例后需要在每次调用缓存时都进行连接,性能会有所下降。
一种在应用程序中共享 ConnectionMultiplexer
实例的方法是使用一个静态属性来返回已连接的实例,如下列中所示。这样,一旦 ConnectionMultiplexer
断开连接,便可以初始化新的连接实例。
private static ConnectionMultiplexer connection;
private static ConnectionMultiplexer Connection
{
get
{
if(connection == null || !connection.IsConnected)
{
connection = ConnectionMultiplexer.Connect("yhdcache0.redis.cache.windows.net,ssl=true,password=...");
}
return connection;
}
}
如果不想通过 SSL 保护缓存/客户端通信,则只需要传递到终结点和密钥中,或者设置ssl=false
。有关高级连接配置选项的详细信息,请参阅 StackExchange.Redis 配置模型.建立连接后,通过调用ConnectionMultiplexer.GetDatabase
方法返回对 Redis Cache 数据库的引用。从GetDatabase
方法返回的对象是一个轻量级直通对象,不需要进行存储。
IDatabase cache = Connection.GetDatabase(); // Perform cache operations using the cache object...
// Simple put of integral data types into the cache
cache.StringSet("key1", "value");
cache.StringSet("key2", 25); // Simple get of data types from the cache
string key1 = cache.StringGet("key1");
int key2 = (int)cache.StringGet("key2");
StackExchange.Redis 客户端使用 RedisKey
和 RedisValue
类型在缓存中访问和存储项目。这些类型映射到大多数基本语言类型(包括 string
),通常不直接使用。Redis Strings
是最基本的 Redis 值类型,可以包含多种数据类型(包括经过序列化的二进制流)。虽然你可能不会直接使用这种类型,但会使用名称中包含 String
的方法。其中,最为常见的是 StringSet
和 StringGet
方法。
// Simple put of integral data types into the cache
cache.StringSet("key1", "value");
cache.StringSet("key2", 25); // Simple get of data types from the cache
string key1 = cache.StringGet("key1");
int key2 = (int)cache.StringGet("key2");
Azure Redis Cache可以使用 .NET 对象和基本数据类型,但 .NET 对象只有在经过序列化之后才能进行缓存。这属于应用程序开发人员的职责。这样,开发人员便可以灵活选择序列化程序。在下列示例中,缓存对象之前,使用了 StackExchange.Redis.IDatabase
类型的扩展类和 BinaryFormatter 来简化这些对象的序列化。
public static class SampleStackExchangeRedisExtensions
{
public static T Get<T>(this IDatabase cache, string key)
{
return Deserialize<T>(cache.StringGet(key));
} public static object Get(this IDatabase cache, string key)
{
return Deserialize<object>(cache.StringGet(key));
} public static void Set(this IDatabase cache, string key, object value)
{
cache.StringSet(key, Serialize(value));
} static byte[] Serialize(object o)
{
if(o == null)
{
return null;
} BinaryFormatter binaryFormatter = new BinaryFormatter();
using (MemoryStream memoryStream = new MemoryStream())
{
binaryFormatter.Serialize(memoryStream, o);
byte[] objectDataAsStream = memoryStream.ToArray();
return objectDataAsStream;
}
} static T Deserialize<T>(byte[] stream)
{
if(stream == null)
{
return default(T);
} BinaryFormatter binaryFormatter = new BinaryFormatter();
using (MemoryStream memoryStream = new MemoryStream(stream))
{
T result = (T)binaryFormatter.Deserialize(memoryStream);
return result;
}
}
}
RedisValue
类型可以直接使用字节数组,因此,调用 Get
帮助程序方法时,它会将对象序列化为字节流,然后再缓存该对象。检索项目时,项目会重新序列化为对象,然后返回给调用程序。
ASP.NET 会话状态的应用程序
- 要使用 Redis Cache 会话状态 NuGet 程序包在 Visual Studio 中配置客户端应用程序,请在“解决方案资源管理器”中右键单击项目,然后选择“管理 NuGet 程序包”。
- 在“联机搜索”文本框中输入 Redis Cache Session State,然后从结果中选择它并单击“安装”。
NuGet 程序包会下载并添加所需的程序集引用,并将以下部分添加到包含 ASP.NET 应用程序所需配置的 web.config 文件中,以便使用 Redis Cache 会话状态提供程序。
<sessionState mode="Custom" customProvider="MySessionStateStore">
<providers>
<!--
<add name="MySessionStateStore"
host = "127.0.0.1" [String]
port = "" [number]
accessKey = "" [String]
ssl = "false" [true|false]
throwOnError = "true" [true|false]
retryTimeoutInMilliseconds = "0" [number]
/>
-->
<add name="MySessionStateStore" type="Microsoft.Web.Redis.RedisSessionStateProvider" host="127.0.0.1" accessKey="" ssl="false" />
</providers>
</sessionState>
使用 Azure 管理门户预览缓存边栏选项卡中的值配置这些属性,然后根据需要配置其他值。
host
– 指定缓存终结点。port
– 使用你的非 SSL 端口或 SSL 端口,具体取决于ssl
设置。accessKey
– 使用缓存的主密钥或辅助密钥。ssl
– 如果要通过 SSL 保护缓存/客户端通信,则设为 true,否则设为 false。请务必指定正确的port
。throwOnError
– 如果希望在发生故障时抛出异常,则设为 true;如果希望按照retryTimeoutInMilliseconds
指定的重试时间间隔重试操作,则设为 false。retryTimeoutInMilliseconds
– 如果将throwOnError
设为 false,则系统会按照此时间间隔(以毫秒为单位)重试操作。
MVC movie app with Azure Redis Cache in 15 minutes
Redis缓存,Azure灾难恢复,标签,SQLDB弹性比例,文档数据库
Spring mvc Data Redis—Pub/Sub(附Web项目源码)
https://github.com/cargomedia/socket-redis
Azure Redis Cache的更多相关文章
- Azure Redis Cache (1) 入门
<Windows Azure Platform 系列文章目录> Microsoft Azure Redis Cache基于流行的开源Redis Cache. 1.功能 Redis 是一种高 ...
- Azure Redis Cache (2) 创建和使用Azure Redis Cache
<Windows Azure Platform 系列文章目录> 本文介绍的是国内由世纪互联运维的Azure China. 注意: 截至今日2015年10月7日,国内由世纪互联运维的Azur ...
- Azure Redis Cache (3) 创建和使用P级别的Redis Cache
<Windows Azure Platform 系列文章目录> 在笔者之前的文档里面已经说明了,Azure Redis Cache分为三个不同的级别: - 基本,Basic,不包含SLA ...
- Azure Redis Cache (4) 配置和管理Redis Cache
<Windows Azure Platform 系列文章目录> 我们在创建完Azure Redis Cache后,经常需要切换Redis Cache的服务级别,这里我简单介绍一下使用Azu ...
- Azure Redis Cache作为ASP.NET 缓存输出提供程序
前一篇文章<Azure Redis Cache作为ASP.NET Session状态提供程序 >我们已经知道如何将ASP.NET应用程序Session存储在Redis Cache中,这里我 ...
- Azure Redis Cache作为ASP.NET Session状态提供程序
从上一篇博客<使用Azure Redis Cache>我们已经可以创建并使用Redis Cache为我们服务了. 作为Web开发者,我们都知道Session状态默认是保存在内存中的,它的优 ...
- 使用Azure Redis Cache
通过上一篇博客<Redis Cache 简介>我们已经简单了解了Azure Redis Cache,这里就不过多赘述了. 1.创建Redis Cache 创建Redis Cache之前,我 ...
- 利用Azure Redis Cache构建百万量级缓存读写
Redis是一个非常流行的基于内存的,低延迟,高吞吐量的key/value数据存储,被广泛用于数据库缓存,session的管理,热数据高速访问,甚至作为数据库方式提高应用程序可扩展性,吞吐量,和实施处 ...
- Azure Redis Cache (3) 在Windows 环境下使用Redis Benchmark
<Windows Azure Platform 系列文章目录> 熟悉Redis环境的读者都知道,我们可以在Linux环境里,使用Redis Benchmark,测试Redis的性能. ht ...
随机推荐
- yii2 关联查询,分页设置
1.MODEL 如关联user table public function getUser(){ return$this->hasOne(User::className(),['i ...
- 用markdown简化书写
引言 markdown是什么, 谁在使用markdown, 以及为什么我应该/不应该使用它? 我能从这篇文章中了解到什么, 怎么开始使用? markdown有两个含义: 一套标记语法(类比html,w ...
- Orchard教程索引页
Orchard官方教程(译)索引 链接标注 原文 则表示未译,其他带有中文标题的表示译文内容. 入门 安装Orchard--Installing Orchard 通过zip包手动安装Orchard-- ...
- 【转载】关于treeview的多层显示的科学用法!
http://blogs.msdn.com/b/mikehillberg/archive/2009/10/30/treeview-and-hierarchicaldatatemplate-step-b ...
- StartCom 申请 SSL 证书及 Nginx HTTPS 支持配置全攻略
来源:https://www.williamyao.com/index.php/archives/1397/ 前言 最近收到 StartCom 的邮件,数字证书即将过期,想到去年在 StartSSL ...
- Nginx %00空字节执行php漏洞
Nginx如下版本:0.5.*, 0.6.*, 0.7 <= 0.7.65, 0.8 <= 0.8.37在使用PHP-FastCGI执行php的时候,URL里面在遇到%00空字节时与Fas ...
- python学习 2数学公式
递归 def fact(n): if n <= 1: return 1 else: return n * fact(n - 1) 斐波那契数列: 第0项是0,第1项是1,从第2项开始,每一项都等 ...
- C语言与java 20155317 王新玮第二次
20155317 王新玮第二次写作感想 你有什么技能比大多数人(超过90%以上)更好? 刚刚看到这个题目,我的首先想到的是会一些中医,懂得中医的理论框架知识,懂得大部分的中医脉象,能够解决日常生活 ...
- SQL Server 里的递归查询
http://www.360doc.com/content/13/0607/11/8463843_291221684.shtml
- ie8中遇到的兼容问题以及解决方案
一.CSS3 1.可以通过在css中引入pie.htc,处理兼容问题(可处理的属性) -webkit-box-shadow: 0 1px 5px #ff2826; -webkit-border-rad ...