zoukankan      html  css  js  c++  java
  • 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,如图

    转自:http://www.tuicool.com/articles/UbYJbyq

  • 相关阅读:
    快速入门系列--MVC--05行为
    2015链家网面试记录
    快速入门系列--MVC--04模型
    django 添加动态表格的方法
    git clone
    postgresql数据库实用操作
    django 实战
    Android手机分辨率基础知识(DPI,DIP计算)
    Android中xml设置Animation动画效果详解
    Unable to execute dex: Multiple dex files define Lcom/gl
  • 原文地址:https://www.cnblogs.com/hf-0712/p/5518792.html
Copyright © 2011-2022 走看看