ASP.NET MVC Redis實現Session

來源:互聯網
上載者:User

標籤:iter   hub   connect   github   stat   nec   技術   ram   which   

 

1:Nuget安裝StackExchange.Redis

2:Nuget安裝RedisSessionStateProvider

在web.config

<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]          />        -->        <add name="MySessionStateStore" type="Microsoft.Web.Redis.RedisSessionStateProvider"              connectionString="127.0.0.1:6379,password=123456,ssl=false,abortConnect=false,connectTimeout=5000" host="" accessKey="" ssl="true" />      </providers>    </sessionState>

在代碼中使用session

 Session["maimai"] = "麥麥";

 Session["haha"] = "哈哈";

redis會產生兩個Hash key,如下

{/_jih5op0z3n0pcoo3amv32p3n}_Data

{/_jih5op0z3n0pcoo3amv32p3n}_Internal

Data儲存所有的session資料,Internal儲存session的到期時間,如下

 

jih5op0z3n0pcoo3amv32p3n 這一串就是我們的sessionId,如

 

再添加一個session測試一下,

 Session["haha"] = "哈哈";

 https://docs.microsoft.com/zh-cn/azure/redis-cache/cache-aspnet-session-state-provider

ASP.NET MVC Redis實現Session

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.