星空网 > 软件开发 > ASP.net

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

  因为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直接输出ModelSession["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完毕

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

    这么屌,是否能搞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();}

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

     虽然真相就在眼前

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

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

 

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




原标题:web开发 那些年基于Redis的Provider库

关键词:Redis

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

到马来西亚货运专线:https://www.goluckyvip.com/tag/90420.html
马来西亚空运双清专线:https://www.goluckyvip.com/tag/90422.html
马来西亚小包专线:https://www.goluckyvip.com/tag/90423.html
专线到马来西亚:https://www.goluckyvip.com/tag/90424.html
专线到马来西亚的物流:https://www.goluckyvip.com/tag/90425.html
陆运马来西亚专线:https://www.goluckyvip.com/tag/90426.html
从园岭新村到大梅沙海滨总站坐什么车:https://www.vstour.cn/a/363191.html
七月份适合去日本旅游吗 7月份去日本哪里好玩:https://www.vstour.cn/a/363192.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流