将于 2014 年 9 月 1 日停止Azure Shared Cache服务,因此你需要在该日期前迁移到 Azure Redis Cache。Azure Redis Cache包含以下两个层级的产品。

使用 StackExchange.Redis NuGet 程序包配置缓存客户端

以 Visual Studio 开发的 .NET 应用程序可以使用 StackExchange.Redis 缓存客户端来访问缓存。

    1. 要使用 StackExchange.Redis NuGet 程序包以 Visual Studio 配置客户端应用程序,请在“解决方案资源管理器”中右键单击项目,然后选择“管理 NuGet 程序包”。
    2. 在“联机搜索”文本框中输入 StackExchange.Redis,然后从结果中选择它并单击“安装”。
    3. 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 客户端使用 RedisKeyRedisValue 类型在缓存中访问和存储项目。这些类型映射到大多数基本语言类型(包括 string),通常不直接使用。Redis Strings 是最基本的 Redis 值类型,可以包含多种数据类型(包括经过序列化的二进制流)。虽然你可能不会直接使用这种类型,但会使用名称中包含 String 的方法。其中,最为常见的是 StringSetStringGet 方法。

// 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 会话状态的应用程序

  1. 要使用 Redis Cache 会话状态 NuGet 程序包在 Visual Studio 中配置客户端应用程序,请在“解决方案资源管理器”中右键单击项目,然后选择“管理 NuGet 程序包”。
  2. 在“联机搜索”文本框中输入 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

http://wacel.codeplex.com/

Redis到底该如何利用?

Redis缓存,Azure灾难恢复,标签,SQLDB弹性比例,文档数据库

Redis编程实践【pub/sub】

Spring mvc Data Redis—Pub/Sub(附Web项目源码)

https://github.com/cargomedia/socket-redis

Azure Redis Cache的更多相关文章

  1. Azure Redis Cache (1) 入门

    <Windows Azure Platform 系列文章目录> Microsoft Azure Redis Cache基于流行的开源Redis Cache. 1.功能 Redis 是一种高 ...

  2. Azure Redis Cache (2) 创建和使用Azure Redis Cache

    <Windows Azure Platform 系列文章目录> 本文介绍的是国内由世纪互联运维的Azure China. 注意: 截至今日2015年10月7日,国内由世纪互联运维的Azur ...

  3. Azure Redis Cache (3) 创建和使用P级别的Redis Cache

    <Windows Azure Platform 系列文章目录> 在笔者之前的文档里面已经说明了,Azure Redis Cache分为三个不同的级别: - 基本,Basic,不包含SLA ...

  4. Azure Redis Cache (4) 配置和管理Redis Cache

    <Windows Azure Platform 系列文章目录> 我们在创建完Azure Redis Cache后,经常需要切换Redis Cache的服务级别,这里我简单介绍一下使用Azu ...

  5. Azure Redis Cache作为ASP.NET 缓存输出提供程序

    前一篇文章<Azure Redis Cache作为ASP.NET Session状态提供程序 >我们已经知道如何将ASP.NET应用程序Session存储在Redis Cache中,这里我 ...

  6. Azure Redis Cache作为ASP.NET Session状态提供程序

    从上一篇博客<使用Azure Redis Cache>我们已经可以创建并使用Redis Cache为我们服务了. 作为Web开发者,我们都知道Session状态默认是保存在内存中的,它的优 ...

  7. 使用Azure Redis Cache

    通过上一篇博客<Redis Cache 简介>我们已经简单了解了Azure Redis Cache,这里就不过多赘述了. 1.创建Redis Cache 创建Redis Cache之前,我 ...

  8. 利用Azure Redis Cache构建百万量级缓存读写

    Redis是一个非常流行的基于内存的,低延迟,高吞吐量的key/value数据存储,被广泛用于数据库缓存,session的管理,热数据高速访问,甚至作为数据库方式提高应用程序可扩展性,吞吐量,和实施处 ...

  9. Azure Redis Cache (3) 在Windows 环境下使用Redis Benchmark

    <Windows Azure Platform 系列文章目录> 熟悉Redis环境的读者都知道,我们可以在Linux环境里,使用Redis Benchmark,测试Redis的性能. ht ...

随机推荐

  1. C#重启IIS指定网站和指定应用程序池

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  2. PHP项目实现手机端和PC端的页面切换

    目前访问页面的要切换成手机端和PC端,原理是通过对设备作出判断,显示不同的功能和页面. 如果手机端和PC端的功能结构不相同,一般会写两套系统,一套适用于PC端,一套适用于手机端. 如果功能相同,则只需 ...

  3. net-force.nl/programming writeup

    从 wechall.net 到 net-force.nl 网站,发现网站的内容不错,里面也有不同类型的挑战题目:Javascript / Java Applets / Cryptography / E ...

  4. TFS Express backup and restore

    When we setup source control server, we should always make a backup and restore plan for it. This ar ...

  5. 查看文本[Linux]

    查看文本 不分屏查看 cat (默认标准输入到标准输出) -n(行号) 连接...并显示 -E(每行行尾打印$) 翻屏:shift+pageUp/pageDown tac reverse cat 分屏 ...

  6. Cocos2d-x 版本小游戏 《是男人就下100层》 项目开源

    这个是很久就开始动手写的一个小游戏了,直到最近才把它收尾了,拖拖拉拉的毛病总是很难改啊. 项目是基于 cocos2d-x v2.2 版本 ,目前只编译到了 Win8 平台上,并且已经上传到了商店,支持 ...

  7. jquery轮播图详解,40行代码即可简单解决。

    我在两个月以前没有接触过html,css,jquery,javascript.今天我却在这里分享一篇技术贴,可能在技术大牛面前我的文章漏洞百出,也请斧正. 可以看出来,无论是div+css布局还是jq ...

  8. Vue - 事件绑定

    1.内联方式: A:将事件处理器绑定到一个方法中,以下所有事件都以click事件作为案例 注意:内联方式下事件处理器只能绑定一个方法,要是想要绑定多个方法,依旧还是使用js中的addEventList ...

  9. 一个快速排序(分类)及使用类似思想实现选择问题[c++实现]

    一.快速排序(快速分类)算法: 问题描述:给定线性集中n个元素和一个整数k,1<=k<=n,要求找出这n个元素中第k小的元素. 思想:选取数组A中的某个元素 t=A[s],然后将其他元素重 ...

  10. (转) 注意啦,笔记本是无线的,虚拟机上网方式莫用NAT,好难整。

    有线网络 在有线网络的条件下,vmware的安装非常简单,上网方式几乎不用怎么设置(默认 NAT模式) 如果默认情况下不能上网,则按以下步骤尝试: ************************** ...