Redis学习笔记~StackExchange.Redis实现分布式Session

回到目录

对于多WEB的环境现在已经是必须的了,很难想像一台WEB服务器面对百万并发的响应,所以,我们需要多台WEB服务器集群合作,来缓解这种高并发,高吞吐的场景,而对于多WEB的场景又会有个问题出现,即session存储的问题,如一个用户登陆后,把一个状态信息存储到当前WEB服务器的session里,而你请求其它页面时,很可能就被路由到另一台服务器了,这时,session也就丢了,而对于这种情况,有人把redis这个存储中间件想了起来,对它进行了封装,就有了今天基于redis的session共享机制。

下面说下安装方法

1 使用nuget安装redis缓存 StackExchange.Redis
2 使用nuget安装RedisSession服务  RedisSessionStateProvider
3 从nuget添加RedisSession之后,它会在你的config文件中写入以下内容,主要是对session进行持久化设置的
   

<sessionState mode="Custom" customProvider="MySessionStateStore" timeout="30">
      <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" [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]
            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" accessKey="" ssl="false" />
      </providers>
    </sessionState>


4 下载是新版本的redis服务端,可以是windows版的,我用的是2.6.13,低版本的redis会出现Eval命令无法识别的问题
5 处理完成,可以测试你的session了,默认过期时间为1200秒

注意,上面sessionState里的timeout就是设置session超时的,它同样使用于redis的存储,下面是存在redis里的session,如图

 

感谢我VIP群的哥们提出这个问题,并对session超时的设置进行了解决。

回到目录

 

posted @ 2016-04-08 16:35  张占岭  阅读(3464)  评论(3编辑  收藏  举报