將于 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
文章列表