将于 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. 第三十二篇:在SOUI2.0中像android一样使用资源

    SOUI2.0之前,在SOUI中使用资源通常是直接使用这个资源的name(一个字符串)来引用.使用字符串的好处在于字符串能够表达这个资源的意义,因此使用字符串也是现代UI引擎常用的方式. 尽管直接使用 ...

  2. C#: using JsonReader avoid Deserialize Json to dynamic

    namespace Test { using Microshaoft; using Test.Models; using Newtonsoft.Json; using System; using Sy ...

  3. XmlValidationHelper XSD、Schema(XmlSchemaSet)、XmlReader(XmlValidationSettings)、XmlDocument、XDocument Validate

    namespace Test { using Microshaoft; using System; using System.Xml; using System.Xml.Linq; class Pro ...

  4. oracle创建DBLINK

    1.查看Global_name参数 show parameter global_name;  该参数为true时,你在本地建立的DBLINK的名称必须和远程的Global_name一致才行. 2.查看 ...

  5. 多个jar包合并成一个jar包的办法

    步骤: 1.将多个JAR包使用压缩软件打开,并将全包名的类拷贝到一个临时目录地下. 2.cmd命令到该临时目录下,此时会有很多.class文件,其中需要带完整包路径 3.执行 jar -cvfM te ...

  6. 如何关闭eslint

    在vue-cli搭建webpack中,使用eslint进行代码规范化检查. 进行关闭,在根目录下有个.eslintignore直接将不想要检查的文件丢进去就可以了 也可以在重构的时候把它关闭掉

  7. C# DataGridView绑定数据源

    第一种: DataSet ds=new DataSet (); ]; 第二种: DataTable dt=new DataTable(); this.dataGridView1.DataSource= ...

  8. C#解析json文件的方法

    C# 解析 json JSON(全称为JavaScript Object Notation) 是一种轻量级的数据交换格式.它是基于JavaScript语法标准的一个子集. JSON采用完全独立于语言的 ...

  9. pycharm快捷键、配置virtualenv环境,配置django调试,配置远程调试

    pycharm安装和首次使用 http://blog.csdn.net/chenggong2dm/article/details/9365437 快捷键: 找文件.代码.引用相关 1.双击shift ...

  10. 『AngularJS』$location 服务

    项目中关于 $location的用法 简介 $location服务解析在浏览器地址栏中的URL(基于window.location)并且让URL在你的应用中可用.改变在地址栏中的URL会作用到$loc ...