将于 2014 年 9 月 1 日中止Azure Shared Cache服务,所以你须要在该日期前迁移到 Azure Redis Cache。Azure Redis Cache包含如下两个层级的产品。html
使用 StackExchange.Redis NuGet 程序包配置缓存客户端git
以 Visual Studio 开发的 .NET 应用程序可使用 StackExchange.Redis 缓存客户端来访问缓存。github
使用 ConnectionMultiplexer 类链接缓存web
在 Azure Redis Cache中,缓存链接由 ConnectionMultiplexer
类进行管理。要链接 Azure Redis Cache 实例,请调用静态 ConnectionMultiplexer.Connect
方法并传递到终结点和密钥中,以下列中所示。redis
ConnectionMultiplexer connection = ConnectionMultiplexer.Connect("yhdcache0.redis.cache.windows.net,ssl=true,password=...");
ConnectionMultiplexer
被设计成在整个客户端应用程序中共享和重复使用,所以不须要在每次执行操做时都加以建立。若是建立实例后须要在每次调用缓存时都进行链接,性能会有所降低。sql
一种在应用程序中共享 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 保护缓存/客户端通讯,则只须要传递到终结点和密钥中,或者设置 。有关高级链接配置选项的详细信息,请参阅 StackExchange.Redis 配置模型.创建链接后,经过调用 方法返回对 Redis Cache 数据库的引用。从 方法返回的对象是一个轻量级直通对象,不须要进行存储。ssl=falseConnectionMultiplexer.GetDatabaseGetDatabase
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 来简化这些对象的序列化。windows
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 会话状态的应用程序
<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弹性比例,文档数据库