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 ...
随机推荐
- HTTrack 网站备份工具
HTTrack可以克隆指定网站-把整个网站下载到本地.可以用在离线浏览上,免费的噢! 强大的Httrack类似于搜索引擎的爬虫,也可以用来收集信息.记得之前写过篇http://www.cnblogs. ...
- 【Java EE 学习 81】【CXF框架】【CXF整合Spring】
一.CXF简介 CXF是Apache公司下的项目,CXF=Celtix+Xfire:它支持soap1.1.soap1.2,而且能够和spring进行快速无缝整合. 另外jax-ws是Sun公司发布的一 ...
- Windows平台下和跨平台的相关公共库
以下主要包含windows下公共库以及跨平台公共库: 1. google base库:google下chromium项目的跨平台公共库: 2. vc_common_src:即HP_SOCKET项目中的 ...
- JsonHelper developed by using Newtonsoft.Json.NET, Deserialize to <T> object , XmlToJson/JsonToXml, QuoteName by using JToken Path.
namespace TestConsoleApplication { using System; using System.Diagnostics; using System.Threading; u ...
- CSS 两列布局 之 左侧适应,右侧固定 3种方式
第一种:左侧用margin-right,右侧float:right CSS代码: html, body,ul,li #wrapper { width: 100%; height: 100%; padd ...
- CTSC是啥
洛谷看到一题的难度NOI/NOI+/CTSC 百度一下 CTSC (China Team Selection Competition)为国际信息学奥林匹克竞赛(International Olympi ...
- 【Hibernate框架】批量操作Batch总结
在我们做.net系统的时候,所做的最常见的批量操作就是批量导入.插入.更新.删除等等,以前我们怎么做呢?基本上有以下几种方式: 1.利用循环调用insert方法,一条条插入. public boole ...
- Element-ui,Mint-ui
style-loader css-loader style!css 饿了么团队开源一个基于vue 组件库 elementUI PC MintUI 移动端 官网: http://element.elem ...
- spring-data-redis 用法
- Mac锁屏
http://www.dbform.com/html/2006/192.html 应用程序-实用工具-钥匙锁-菜单栏中的钥匙串访问-偏好设置-选中“在菜单栏中显示钥匙串”