2017-01-27

使用 Redis 當做 ASP.NET MVC 的 Session State Server

Redis 是一套 Open Source ,Memory base 的 Key-Value 儲存資料庫,由於效能優異而受到重視。今天來看 Redis 可以怎麼用來當做 ASP.NET MVC 的 Session State Server.


安裝 Redis

  1. 下載 Redis on Winodws

    Release of Redis on Windows from Github

  2. 直接安裝

  • 預設使用 6379 port
  • 加入防火牆例外清單

    1DEFAULT

  1. 設定使用記憶體上限
  • 依實際使用況狀來設定 Redis 的記憶體使用上限

    2MEMORY

專案安裝 Microsoft.Web.RedisSessionStateProvider

以下安裝方式擇一即可

  1. NuGet GUI

    • 專案右鍵 --> Manage NuGet Packages...

      3rightclick

    • 搜尋 Microsoft.Web.RedisSessionStateProvider

      4search

  2. Package Manager Console

    • Visual Studio 主選單 Tools --> NuGet Package Manager --> Package Manager Console

      5console

    • 安裝 Install-Package Microsoft.Web.RedisSessionStateProvider

      6consoleinstall

使用設定

Microsoft.Web.RedisSessionStateProvider

  • 安裝完成後會直接在專案上加入參考,也會在 Web.config 加上設定區塊跟範例
    <system.web> 
    <sessionState mode="Custom" customProvider="MySessionStateStore">
         <providers>
           <!-- For more details check https://github.com/Azure/aspnet-redis-providers/wiki -->
           <!-- Either use 'connectionString' OR 'settingsClassName' and 'settingsMethodName' OR use 'host','port','accessKey','ssl','connectionTimeoutInMilliseconds' and 'operationTimeoutInMilliseconds'. -->
           <!-- 'throwOnError','retryTimeoutInMilliseconds','databaseId' and 'applicationName' can be used with both options. -->
           <!--
             <add name="MySessionStateStore" 
               host = "127.0.0.1" [String]
               port = "" [number]
               accessKey = "" [String]
               ssl = "false" [true|false]
               throwOnError = "true" [true|false]
               retryTimeoutInMilliseconds = "5000" [number]
               databaseId = "0" [number]
               applicationName = "" [String]
               connectionTimeoutInMilliseconds = "5000" [number]
               operationTimeoutInMilliseconds = "1000" [number]
               connectionString = "<Valid StackExchange.Redis connection string>" [String]
               settingsClassName = "<Assembly qualified class name that contains settings method specified below. Which basically return 'connectionString' value>" [String]
               settingsMethodName = "<Settings method should be defined in settingsClass. It should be public, static, does not take any parameters and should have a return type of 'String', which is basically 'connectionString' value.>" [String]
               loggingClassName = "<Assembly qualified class name that contains logging method specified below>" [String]
               loggingMethodName = "<Logging method should be defined in loggingClass. It should be public, static, does not take any parameters and should have a return type of System.IO.TextWriter.>" [String]
               redisSerializerType = "<Assembly qualified class name that implements Microsoft.Web.Redis.ISerializer>" [String]
             />
           -->
           <add name="MySessionStateStore" type="Microsoft.Web.Redis.RedisSessionStateProvider" host="" accessKey="" ssl="false" />
         </providers>
       </sessionState>
    </system.web>
    
    • nuget 安裝後會直接在 web.config 加上連線設定 MySessionStateStore預設啟用 ssl, 如果 redis server 沒有設定啟用 ssl 就會出現下列錯誤,請記得將 ssl 改為 false (上面範例我已調整不使用 ssl)

錯誤訊息:No connection is available to service this operation: EVAL

7sslerror

Redis 連線設定

屬性 預設值 說明
host - Redis Server 的 IP 或是 Server Name
port - Redis Server 服務的 port
accessKey - Redis 啟用授權時 的 Redis Server 密碼
ssl false 啟用 SSL 連線至 Redis Server 與否
throwOnError true 事件失敗時擲出例外
retryTimeoutInMilliseconds 0 失敗時以指定的毫秒數重試,0表不重試
databaseId 0 指定 database
applicationName - key 以 {<Application Name>_<Session ID>}_Data 格式儲存,可讓不同 application 用相同的 key
connectionTimeoutInMilliseconds 5000 連線逾時毫秒數
operationTimeoutInMilliseconds 1000 同步逾時毫秒數

確認 Redis 資料

  • 先加一個 session 資料

    9addsession

  • 使用 redis-cli.exe

    預設路徑 C:\Program Files\Redis\redis-cli.exe

  • 連線至 Redis

    "C:\Program Files\Redis\redis-cli.exe" -h 127.0.0.1

  • 列出所有 key

    KEYS *

  • 檢查特定 key 內容

    • HGETALL key

    8result

參考資料

  1. Announcing ASP.NET Session State Provider for Redis Preview Release
  2. Redis 官網
  3. Release of Redis on Windows from Github
  4. Redis On Windows - Part Two
  5. Azure Redis 快取的 ASP.NET 工作階段狀態提供者

沒有留言:

張貼留言