Azure Redis Cache

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   使用   os   io   

將於 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 快取用戶端來訪問緩衝。

    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 cachecache.StringSet("key1", "value");cache.StringSet("key2", 25);// Simple get of data types from the cachestring 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 cachecache.StringSet("key1", "value");cache.StringSet("key2", 25);// Simple get of data types from the cachestring key1 = cache.StringGet("key1");int key2 = (int)cache.StringGet("key2");

Azure Redis Cache可以使用 .NET 對象和基礎資料型別 (Elementary Data Type),但 .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/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.