alun-chen

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

1. 开始

  对于分布式的缓存,平常的session的处理是一个用户对应一台分布式的机器,如果这台机器中途挂机或者不能处理这个用户session的情况发生,则此用户的session会丢失,会发生不可预知的错误。如下图:

image

 

  如果用Redis的分布式缓存,则能避免上面的情况。因为session是保存在Redis中,不会有丢失的情况,就算中途有服务器A挂掉。如下图:

image

 

 

2. 代码

1)在包管理器中,输入下面的包,安装

Install-Package Microsoft.Web.RedisSessionStateProvider

2)可以看到web.config中自动添加了以下代码:

<sessionState mode="Custom" customProvider="MySessionStateStore" timeout="30">
      <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="ceswebchat.redis.cache.chinacloudapi.cn" 
             accessKey="m1JuDEstaqA+xxxxxxxxxxx" 
             ssl="false"
             throwOnError = "true"
             retryTimeoutInMilliseconds = "5000"
             connectionTimeoutInMilliseconds = "5000"
             operationTimeoutInMilliseconds = "1000"
             databaseId = "0"
             />
      </providers>
</sessionState>

上面可以看到,输入你所需的信息就可以了。很简单

  • host – Redis Cache的host name/endpoint
  • port – 使用你的Redis Cache的SSL port或者非SSL port,这取决你的ssl设置的值
  • accessKey – Redis Cache的Keys
  • ssl – true (Cache/Client之间安全的通信); 否则设为 false
    • 默认禁用 non-SSL port,有关启用 non-SSL port,参阅 Access Ports
  • throwOnError – true (在事件出错时抛出异常),否则设为 false. 你可以在静态属性 Microsoft.Web.Redis.RedisSessionStateProvider.LastException检查异常信息, 默认为true。
  • retryTimeoutInMilliseconds – 在此时间间隔内重试操作(单位:毫秒)
  • databaseId – 指定一个数据库用来存储缓存输入数据。如果未指定,默认值为0。
  • applicationName – 缓存key存储在Redis中命名规则为:{<Application Name>_<Session ID>}_Data,如果多个应用程序共享相同的key,则这个参数是可选的。 如果不设置该参数将使用默认值。
  • connectionTimeoutInMilliseconds – 这个设置会覆盖connectTimeout参数设置。如果未设置,那么会使用默认的connectTimeout值5000,详情请参考 StackExchange.Redis configuration model.
  • operationTimeoutInMilliseconds – 这个设置会覆盖syncTimeout参数设置。如果未设置,那么会使用默认的syncTimeout值1000,详情请参考 StackExchange.Redis configuration model.
posted on 2016-08-22 17:47  alun-chen  阅读(993)  评论(0编辑  收藏  举报