• ASP.NET基于Redis的Provider库


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

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

         虽然真相就在眼前

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

  • 相关阅读:
    剑气之争,聊聊算法岗位的门户之见!
    80%学生的困惑,学完C/C++之后学什么?
    算法工程师日常,训练的模型翻车了怎么办?
    迭代器设计模式,帮你大幅提升Python性能
    十年编程经验总结,三点技巧帮你提升代码能力!
    CenterNet:Corner-Center三元关键点,检测性能全面提升 | ICCV 2019
    CornerNet:经典keypoint-based方法,通过定位角点进行目标检测 | ECCV2018
    阿里面试:MySQL如何设计索引更高效?
    大厂是怎么进行SQL调优的?
    程序人生|从网瘾少年到微软、BAT、字节offer收割机逆袭之路
  • 原文地址:https://www.cnblogs.com/lenmom/p/8513307.html
Copyright © 2020-2023  润新知