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

Microsoft.Web.RedisOutputCacheProvider,没错就是它,此库底层用的正是StackExchange来访问redis

当然,不止MS写了,这个也是可以滴:https://github.com/alex-simonov/RedisAspNetProviders

redis戳此入门:http://www.cnblogs.com/NotAnEmpty/p/5441127.html

研究内部实现可参考其下

参考:https://github.com/Azure/aspnet-redis-providers

https://msdn.microsoft.com/zh-cn/library/ms178587(VS.80).aspx

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

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?搞搞不要钱

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 = 60, VaryByParam = "*")]
public ActionResult RedisCacheIndex()
{
ViewData.Model = DateTime.Now.ToString();
return View();
}

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

虽然真相就在眼前

欢迎转载,来爬我啊:http://www.cnblogs.com/NotAnEmpty/p/5441127.html

web开发 那些年基于Redis的Provider库的更多相关文章

  1. ASP.NET基于Redis的Provider库

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

  2. 向 Web 开发人员推荐35款 JavaScript 图形图表库

    图表是数据图形化的表示,通过形象的图表来展示数据,比如条形图,折线图,饼图等等.可视化图表可以帮助开发者更容易理解复杂的数据,提高生产的效率和 Web 应用和项目的可靠性. 在这篇文章中,我们收集了3 ...

  3. 超全的web开发工具和资源

    首页 新闻 产品 地图 动态 城市 帮助 论坛 关于 登录 注册 · 不忘初心,继续前进,环境云V2接口正式上线 · 环境云测点地图全新改版 · 祝福各位环境云用户中秋快乐!   平台信息 培训互动 ...

  4. 干货100+ 最超全的web开发工具和资源大集合

    干货100+ 最超全的web开发工具和资源大集合   作为Web开发者,这是好的时代,也是坏的时代.Web开发技术也在不断变化.虽然很令人兴奋,但是这也意味着Web开发人员需要要积极主动的学习新技术和 ...

  5. Python做web开发,推荐几个能立马上手的小项目

    Python这门优美的语言是非常适合web开发的,基于Python的Django框架简单便捷且很强大. 那么作为新手该如何上手这门语言?一切不敲代码的学编程手段都是扯淡,今天就推荐一些适合新手练手的P ...

  6. Redis的Python实践,以及四中常用应用场景详解——学习董伟明老师的《Python Web开发实践》

    首先,简单介绍:Redis是一个基于内存的键值对存储系统,常用作数据库.缓存和消息代理. 支持:字符串,字典,列表,集合,有序集合,位图(bitmaps),地理位置,HyperLogLog等多种数据结 ...

  7. Redis在WEB开发中的应用与实践

    Redis在WEB开发中的应用与实践 一.Redis概述: Redis是一个功能强大.性能高效的开源数据结构服务器,Redis最典型的应用是NoSQL.但事实上Redis除了作为NoSQL数据库使用之 ...

  8. BEGINNING SHAREPOINT&#174; 2013 DEVELOPMENT 第3章节--SharePoint 2013 开发者工具 SharePoint中基于Web开发

    BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第3章节--SharePoint 2013 开发者工具 SharePoint中基于Web开发         之前提到过, ...

  9. 《Flask Web开发——基于Python的Web应用开发实践》一字一句上机实践(上)

    目录 前言 第1章 安装 第2章 程序的基本结构 第3章 模板 第4章 Web表单 第5章 数据库 第6章 电子邮件 第7章 大型程序的结构   前言 学习Python也有一个半月时间了,学到现在感觉 ...

随机推荐

  1. IOS-SQLite3的封装

    IWStudent.h // // IWStudent.h // 02-SQLite的封装 // // Created by apple on 14-5-22. // Copyright (c) 20 ...

  2. mooseFS学习篇

    官方网站:http://www.moosefs.org/ About MooseFS MooseFS is a fault tolerant, network distributed file sys ...

  3. 记录下MD5加密遇到的坑

    错误的写法: public static String md5(String plainText) { byte[] secretBytes = null; try { secretBytes = M ...

  4. L129

    Iraq Sees Spike in Water-Borne IllnessesIraqi health officials say that a health crisis stemming fro ...

  5. VBox自动启动websrv 并开机启动所有的虚拟机

    由于项目需要,在Ubuntu 16.04的Server版本中安装了一个virtualbox,并利用phpvirtualbox来管理虚拟机 需要在宿主机启动之后将所有的虚拟机也启动,因此用以下方式进行处 ...

  6. windows 下多线程

    unsigned uiThread2ID; HANDLE handle = (HANDLE)_beginthreadex(NULL, , ThreadUploadFun, NULL, CREATE_S ...

  7. bulkcopy实现批量插入与更新

    public static void UpdateData<T>(List<T> list, string TabelName) { DataTable dt = new Da ...

  8. 使用jenkins并发布应用到tomcat

    jenkins的介绍及安装请自行百度,本文重点介绍如何使用jenkins,并自动发布web应用到tomcat中. 1 . 创建项目 打开jenkins --> 新建 --> 填写item名 ...

  9. input光标位置不居中问题

    文本输入框默认在谷歌,火狐浏览器中,光标是居中显示的.但在IE7中一开始会在顶部闪烁(输入文字后光标居中),加上行高就可以,值为文本框的高度. 注意要加*号,否则在谷歌浏览其中光标会在顶部闪烁. *l ...

  10. tab显示不同数据

    效果 核心代码 [js] [#escape x as (x)!?html]<!doctype html><html lang="zh-CN"><hea ...