Asp.Net分布式缓存Redis之Session框架

     尽管Session在某些方面,并不尽如人意,关于它的种种利弊,也都是各种云(人云亦云、众说纷纭)。可在面对一些项目的时候,我不得不采用它来存储一些信息,来实现某些功能。在某项项目里,我采用Redis缓存服务,实现自定义Session。

   在这里,我推荐两款基于Redis的Session框架,一种是Harbour.RedisSessionStateStore,但是它好像有段时间未更新了。我们可以在这里https://www.nuget.org/packages/Harbour.RedisSessionStateStore/

通过nuget工具来安装到工程里。

gitHub的下载地址:https://github.com/TheCloudlessSky/Harbour.RedisSessionStateStore

  另一种,是微软提供的

RedisSessionStateProvider 组件。安装方法如下图:


RedisSessionStateProvider的安装方式,非常简单。安装完成后,只需要在你的Web.config中,添加下配置


<sessionState mode="Custom"  cookieName="TiKuSession" customProvider="MySessionStateStore">
      <providers>
        <!-- Either use 'connectionString' and provide all parameters as string 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"
            port = "6379"
            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]
            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" host="127.0.0.1" port="6379" accessKey="" ssl="false" />
      </providers>
    </sessionState>
接下来,让我们一起来感受下。


Asp.Net分布式缓存Redis之Session框架_第1张图片

预览下:

Asp.Net分布式缓存Redis之Session框架_第2张图片

看下我们的Redis服务里存储了什么?

Asp.Net分布式缓存Redis之Session框架_第3张图片

好了,先写到这儿吧。若有想法,欢迎探讨。

你可能感兴趣的:(redis,session)