Azure - 儲存Session到Redis Cache

24 August 2014 — Written by Sky Chang
#ASP.NET MVC#Azure#Redis

上一篇,我們快速地看到了Redis怎麼樣在Azure建立與使用,那同樣的,在Azure上,多個Instance最需要解決的還是Session問題;我們以前看了很多種解決方式,例如官方不建議的Table方式,或是早期的存放到SQL Database、貴貴的Azure Cache,或是拿Cloud Service的Work Role當作Cache。

而這篇文章,我們就來看看,要怎樣使用超高速的Redis Cache當作Session的存放位置。

首先,我們要在NuGet裝RedisSessionStateProvider,所以我們要在搜尋方塊打"RedisSessionStateProvider”。( 目前這個Lib是屬於發行前版本,所以要切換到發行前版本進行搜尋才搜尋的到 )

image

完成之後,官方是寫說會將Lib載入,並且自動修改Web.config,但實際上,我沒有阿QQ..好啦,這個月人品持續有問題,那我們也只好自己加入了…下面是官方給的範例,我們就用這範例去做調整吧。

基本上,只有retryTimeoutInMilliseconds要注意一下,因為是以毫秒為單位計算。( RetryTimeoutInMilliseconds是當有斷線,連不到的情況下,可以重試 )

<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>

那我們修改玩的Web.Config會長得像這樣,記得accessKey要填入Azure上Redis Cache的Key。

<?xml version="1.0" encoding="utf-8"?>
<!--
  如需如何設定 ASP.NET 應用程式的詳細資訊,請瀏覽
  http://go.microsoft.com/fwlink/?LinkId=301880
  -->
<configuration>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0"/>
    <add key="webpages:Enabled" value="false"/>
    <add key="ClientValidationEnabled" value="true"/>
    <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5"/>
    <httpRuntime targetFramework="4.5"/>

    <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="skyrediscache.redis.cache.windows.net"
        accessKey="..." ssl="true" />
      </providers>
    </sessionState>

  </system.web>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed"/>
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-1.5.2.14234" newVersion="1.5.2.14234"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-5.2.0.0" newVersion="5.2.0.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

最後,我們可以寫一個Session進去看看。

public ActionResult SessionTest()
{
    Session["Test"] = "Hello Redis";

    return View();
}

基本上如果無報錯的話,就代表成功了喔!!

參考資料

Sky & Study4.TW