1. 开始

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

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

2. 代码

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

Install-Package Microsoft.Web.RedisSessionStateProvider

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

<sessionState mode="Custom" customProvider="MySessionStateStore" timeout="">
<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 = "" [number]
databaseId = "" [number]
applicationName = "" [String]
connectionTimeoutInMilliseconds = "" [number]
operationTimeoutInMilliseconds = "" [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 = ""
connectionTimeoutInMilliseconds = ""
operationTimeoutInMilliseconds = ""
databaseId = ""
/>
</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.

C# Azure 存储-分布式缓存Redis在session中的配置的更多相关文章

  1. C# Azure 存储-分布式缓存Redis的新建&配置&查看

    1. 介绍 Redis 是一款开源的,基于 BSD 许可的,高级键值 (key-value) 缓存 (cache) 和存储 (store) 系统.由于 Redis 的键包括 string,hash,l ...

  2. C# Azure 存储-分布式缓存Redis工具类 RedisHelper

    using System; using System.Collections.Generic; using Newtonsoft.Json; using StackExchange.Redis; na ...

  3. 分布式缓存Redis应用场景解析

    Redis的应用场景非常广泛.虽然Redis是一个key-value的内存数据库,但在实际场景中,Redis经常被作为缓存来使用,如面对数据高并发的读写.海量数据的读写等. 举个例子,A网站首页一天有 ...

  4. ASP.Net Core使用分布式缓存Redis从入门到实战演练

    一.课程介绍 人生苦短,我用.NET Core!缓存在很多情况下需要用到,合理利用缓存可以一方面可以提高程序的响应速度,同时可以减少对特定资源访问的压力.  所以经常要用到且不会频繁改变且被用户共享的 ...

  5. .NET分布式缓存Redis从入门到实战

    一.课程介绍 今天阿笨给大家带来一堂NOSQL的课程,本期的主角是Redis.希望大家学完本次分享课程后对redis有一个基本的了解和认识,并且熟悉和掌握 Redis在.NET中的使用. 本次分享课程 ...

  6. 第十章 企业项目开发--分布式缓存Redis(2)

    注意:本章代码是在上一章的基础上进行添加修改,上一章链接<第九章 企业项目开发--分布式缓存Redis(1)> 上一章说了ShardedJedisPool的创建过程,以及redis五种数据 ...

  7. 三点须知:当我们在开发过程中需要用到分布式缓存Redis的时候

    当我们在开发过程中需要用到分布式缓存Redis的时候,我们首先要明白缓存在系统中用来做什么? 1. 少量数据存储,高速读写访问.通过数据全部in-momery 的方式来保证高速访问,同时提供数据落地的 ...

  8. 一个技术汪的开源梦 —— 公共组件缓存之分布式缓存 Redis 实现篇

    Redis 安装 & 配置 本测试环境将在 CentOS 7 x64 上安装最新版本的 Redis. 1. 运行以下命令安装 Redis $ wget http://download.redi ...

  9. 企业项目开发--分布式缓存Redis

    第九章 企业项目开发--分布式缓存Redis(1) 注意:本章代码将会建立在上一章的代码基础上,上一章链接<第八章 企业项目开发--分布式缓存memcached> 1.为什么用Redis ...

随机推荐

  1. Git命令参考手册(文本版)

    git init # 初始化本地git仓库(创建新仓库) git config --global user.name "xxx" # 配置用户名 git config --glob ...

  2. shixi

    1. ./statsite -f statsite.conf &(后台): 2. ps ax|grep ** 3. bg fg 4.chmod +x start.sh 5.tcpdump ud ...

  3. [杂谈]冲NOIP一等奖。。

    唉不想多说了. 真是一段“传奇”的经历啊. 还是那句话..“是的我上次什么都没说就走了...”这次也一样. 我还是太单纯的以为我们是肯定能够参加北大的夏令营的..然而结果真是意料之外啊. 本来我以为我 ...

  4. Difference between WCF and Web API and WCF REST and Web Service

    The .Net framework has a number of technologies that allow you to create HTTP services such as Web S ...

  5. WINFORM 输出txt文件

    SaveFileDialog saveFile1 = new SaveFileDialog(); saveFile1.Filter = "文本文件(.txt)|*.txt"; sa ...

  6. Linux 系统查看物理内存使用率的命令脚本,以百分比形式输出。

    想监视系统内存?好像是没法直接得到现成的百分比的,自己取值计算一下吧 totalmem=`free -m | grep 'Mem' | awk '{print $3}'` usedmem=`free ...

  7. 关于AlertDialog.Builder(Context context)中所应传入的context

    错误报告: 10-20 14:34:46.565: E/AndroidRuntime(23098): FATAL EXCEPTION: main10-20 14:34:46.565: E/Androi ...

  8. 通用EF框架

    之前我老大去网上找了一个DAL里面操作数据库的通用类: public class DALHelper { public static List<T> Search<T>() w ...

  9. content内网,会显示内容,没有内容可地址存在就是这个情况

    漏洞地址:http://note.youdao.com/memory/?url=http://www.wooyun.org(如需登录,请注册登录) 正文预览的地方会读取URL地址的<meta n ...

  10. C#模拟http 发送post或get请求

    /// <summary> /// 模拟HTTP提交表单并获取返回数据 /// POST /// </summary> /// <param name="Url ...