zoukankan      html  css  js  c++  java
  • 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直接输出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

  • 相关阅读:
    毕业半年,码过5个城市
    Java实现office文档与pdf文档的在线预览功能
    挺过最艰难的2018,我终将长大
    dubbo源码分析(一)-从xml到我们认识的Java对象
    沉淀一年,我想推荐这些书给你
    Java动态代理(一)
    Java工程师成神之路思维导图
    Java
    阿里巴巴Java开发手册思维导图
    Mysql的排他锁和共享锁
  • 原文地址:https://www.cnblogs.com/NotAnEmpty/p/5459977.html
Copyright © 2011-2022 走看看