2216609207 发表于 2015-7-19 14:26:19

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(this IDatabase cache, string key)
{
return Deserialize(cache.StringGet(key));
}
public static object Get(this IDatabase cache, string key)
{
return Deserialize(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(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 会话状态提供程序。







  使用 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
  
页: [1]
查看完整版本: Azure Redis Cache