因为session基于本地cache,以前我们自己写分布式缓存,或者数据库存储,或者cookie加密存储,来保存用户状态信息,但较少的直接通过创建一个继承 SessionStateStoreProviderBase 类,来实现自定义会话状态存储提供程序。但有ASP.NET官方的福利,我们就不能放过。

  Microsoft.Web.RedisOutputCacheProvider,没错就是它,此库底层用的正是StackExchange来访问redis  当然,不止MS写了,
研究内部实现可参考其下    参考:
 

天色尚未晚 媳妇还未还,还有时间 干点正事吧...

Install-Package Microsoft.Web.RedisSessionStateProvider

webconfig出现一小坨,如果用的是Azure的Redis,那你需要填一下host和accesskey,否则也没啥好改的

<sessionState mode="Custom" customProvider="MySessionStateStore">
<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]
/>
-->
<add name="MySessionStateStore" type="Microsoft.Web.Redis.RedisSessionStateProvider" connectionString="127.0.0.1:6379,allowadmin=true" accessKey="" ssl="true" />
</providers>
</sessionState>

接着我们看看它能干啥?

//Action Index中,就两句,view直接输出Model
Session["UserAgent"] = Request.UserAgent;
ViewData.Model = "访问状态已分布式存储session"; //然后我们访问另一个About,然后直接输出Session:@Model
//不出意料的出现了:Session:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36
//当然你们是看不到,我反正看到了,但看不到也不是重点
ViewData.Model = Session["UserAgent"];

重点来了,有图有真相,分布式Session完毕

这么屌,是否能搞OutputCacheProvider?搞搞不要钱

1
Install-Package Microsoft.Web.RedisOutputCacheProvider

  依旧免费给的一大坨

<caching>
<outputCache defaultProvider="MyRedisOutputCache">
<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'. -->
<!-- 'databaseId' and 'applicationName' can be used with both options. -->
<!--
<add name="MyRedisOutputCache"
host = "127.0.0.1" [String]
port = "" [number]
accessKey = "" [String]
ssl = "false" [true|false]
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]
/>
-->
<add name="MyRedisOutputCache" type="Microsoft.Web.Redis.RedisOutputCacheProvider" connectionString="127.0.0.1:6379,allowadmin=true" accessKey="" ssl="true" />
</providers>
</outputCache>
</caching>
[OutputCache(Duration = , VaryByParam = "*")]
public ActionResult RedisCacheIndex()
{
ViewData.Model = DateTime.Now.ToString();
return View();
}

  当然访问此页面怎么刷新都是同一个时间,但我们要学会寻找重点

虽然真相就在眼前

原文地址:http://www.cnblogs.com/NotAnEmpty/p/5441127.html

ASP.NET基于Redis的Provider库的更多相关文章

  1. web开发 那些年基于Redis的Provider库

    因为session基于本地cache,以前我们自己写分布式缓存,或者数据库存储,或者cookie加密存储,来保存用户状态信息,但较少的直接通过创建一个继承 SessionStateStoreProvi ...

  2. asp.net core mvc基于Redis实现分布式锁,C# WebApi接口防止高并发重复请求,分布式锁的接口幂等性实现

    使用背景:在使用app或者pc网页时,可能由于网络原因,api接口可能被前端调用一个接口重复2次的情况,但是请求内容是一样的.这样在同一个短暂的时间内,就会有两个相同请求,而程序只希望处理第一个请求, ...

  3. ASP.NET Core WebApi基于Redis实现Token接口安全认证

    一.课程介绍 明人不说暗话,跟着阿笨一起玩WebApi!开发提供数据的WebApi服务,最重要的是数据的安全性.那么对于我们来说,如何确保数据的安全将会是需要思考的问题.在ASP.NET WebSer ...

  4. .Net分布式架构(二):基于Redis的Session共享

    一:Session简介 Session是什么呢?简单来说就是服务器给客户端的一个编号.当一台web服务器运行时,可能有若干个用户浏览正在运正在这台服务器上的网站.当每个用户首次与这台web服务器建立连 ...

  5. 基于redis分布式缓存实现(新浪微博案例)

    第一:Redis 是什么? Redis是基于内存.可持久化的日志型.Key-Value数据库 高性能存储系统,并提供多种语言的API. 第二:出现背景 数据结构(Data Structure)需求越来 ...

  6. 项目分布式部署那些事(1):ONS消息队列、基于Redis的Session共享,开源共享

    因业务发展需要现在的系统不足以支撑现在的用户量,于是我们在一周之前着手项目的性能优化与分布式部署的相关动作. 概况 现在的系统是基于RabbitHub(一套开源的开发时框架)和Rabbit.WeiXi ...

  7. 基于redis分布式缓存实现

    Redis的复制功能是完全建立在之前我们讨论过的基 于内存快照的持久化策略基础上的,也就是说无论你的持久化策略选择的是什么,只要用到了Redis的复制功能,就一定会有内存快照发生,那么首先要注意你 的 ...

  8. 分布式锁 基于Redis

    分布式锁的实现(基于Redis) 参考:http://www.jb51.net/article/75439.htm http://www.linuxidc.com/Linux/2015-01/1118 ...

  9. 基于Redis缓存的Session共享(附源码)

    基于Redis缓存的Session共享(附源码) 在上一篇文章中我们研究了Redis的安装及一些基本的缓存操作,今天我们就利用Redis缓存实现一个Session共享,基于.NET平台的Seesion ...

随机推荐

  1. Python 文件修改

    # 需求: 把好人换成sb # 必须: # 1. 先从文件中读取内容 # 2. 把要修改的内容进行修改 # 3. 把修改好的内容写人一个新文件 # 4. 删除掉原来的文件 # 5. 把新文件重命名成原 ...

  2. WEB接口测试之Jmeter接口测试自动化 (四)

    Jmeter是压力测试.接口测试工具,Ant是基于Java的构建工具,具有跨平台的作用,jenkins是持续集成工具.将这三者结合起来可以搭建一套webservice接口测试的持续构建环境.   1. ...

  3. matlab sparse()

    一.sparse()的稀疏矩阵简单运用 1.>> A = [0, 0, 0, 0; 0, 0, 1, 0; 0, 0, 0, 0; 0, 1, 0, 2]; >> sparse ...

  4. WinRAR的自解压模式 - imsoft.cnblogs

    一个 SFX (SelF-eXtracting)自解压文件是压缩文件的一种,它结合了可执行文件模块,一种用以运行从压缩文件解压文件的模块.这样的压缩文件不需要外部程序来解压自解压文件的内容,它自己便可 ...

  5. 《DSP using MATLAB》Problem 4.5

    1. 2. 3. 5.不会

  6. Apache和Nginx的Rewrite规则对比

    一.Apache的rewrite 1.Rewrite规则简介: Rewirte主要的功能就是实现URL的跳转,它的正则表达式是基于Perl语言.可基于服务器级的(httpd.conf)和目录级的(.h ...

  7. 树莓派挂载和卸载U盘或移动硬盘

    通常我们在 /mnt 或 /media 目录下新建一个目录udisk作为挂载点 sudo mkdir /mnt/udisk 1.手动挂载: 挂载命令: sudo mount -o uid=pi,gid ...

  8. 六、物理数据模型(PDM逆向工程)

      物理数据模型PDM 物理数据模型(Physical Data Model,PDM):在数据库的逻辑结构设计好之后,就需要完成其物理设计,PDM就是为实现这一目的而设计的. 物理数据模型是以常用的D ...

  9. nyoj 单调递增最长子序列

    单调递增最长子序列 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 求一个字符串的最长递增子序列的长度如:dabdbf最长递增子序列就是abdf,长度为4   输入 ...

  10. 【转】每天一个linux命令(11):nl命令

    原文网址:http://www.cnblogs.com/peida/archive/2012/11/01/2749048.html nl命令在linux系统中用来计算文件中行号.nl 可以将输出的文件 ...