【问题标题】:ASP.NET Output Cache Provider for Redis doesn't storeRedis 的 ASP.NET 输出缓存提供程序不存储
【发布时间】:2019-08-03 17:12:06
【问题描述】:

我不确定如何解决它,但我正在尝试通过 Redis 输出缓存提供程序实现 ASP.NET 输出缓存。

我们设置了 Redis 服务器(非天蓝色),我可以存储缓存以供一般使用。 但是,当我尝试设置 ASP.NET 输出缓存时,它似乎没有保存任何缓存!

我已经通过 Nuget 安装了 Microsoft.Web.RedisOutputCacheProvider。 Web.Config 设置如下:

<caching>
      <outputCache defaultProvider="MyRedisOutputCache">
        <providers>
          <add name="MyRedisOutputCache" type="Microsoft.Web.Redis.RedisOutputCacheProvider" host="ServerName" port="6464" accessKey="PasswordToRedis" />
        </providers>
      </outputCache>
</caching>

MVC 控制器设置了 OutputCache 属性:

[OutputCache(Duration = 3600, VaryByParam = "*", Location = OutputCacheLocation.ServerAndClient)]
        public JsonResult GetLookupData()

当我检查 Redis 时,我没有看到任何 OutputCache 被存储。

我错过了什么吗?有没有办法调试为什么它没有在缓存中存储任何东西?

【问题讨论】:

  • 检查一下docs.microsoft.com/en-us/azure/azure-cache-for-redis/…,缓存 PING 命令是否在您的 Web 应用程序中工作?
  • 我应该提到,应用程序或缓存服务器都不在 Azure 上。没有。除此之外,我还可以使用代码中的普通缓存方法来利用 Redis 缓存。但是,如果我选择使用 OutputCacheProvider 进行输出缓存,我不会存储任何内容。
  • 您可以尝试按照这篇文章更改您的 web.config 吗? github.com/moonpyk/mvcdonutcaching/issues/35
  • 我已经尝试过使用额外的 type 参数,但它仍然无法正常工作。没有错误。

标签: asp.net-mvc redis outputcache


【解决方案1】:

好吧,这真的很傻。

当你通过 Nuget 安装 RedisOutputCacheProvider 时,你会在你的 app/web.config 中得到这个小文档:

<!-- 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]
            redisSerializerType = "<Assembly qualified class name that implements Microsoft.Web.Redis.ISerializer>" [String]
          />

它表示“ssl”的默认值为 false。 但是,通读代码本身,它实际上默认为 true

所以明确地将 ssl 设置为 false 已修复它。

编辑

哦,我不得不将 RedisOutputCacheProvider 降级到 1.7.5。

3.0.1 根本不适合我。

【讨论】:

    【解决方案2】:

    我在没有 SSL 设置的本地主机上工作。在生产中我需要它,但在我的它是连接字符串的一部分(由我的 Redis 托管服务提供)。

    它不起作用并且您不得不将 RedisOutputCacheProvider 降级到 1.7.5 的原因是因为您使用的是 Exchange.Redis.Strongname.dll(版本 1.2.6)

    根据这个问题,Redis 不再需要强名称,因为基础版本现在是强名称。 https://github.com/Azure/aspnet-redis-providers/issues/107

    所以要使用 RedisOutputCacheProvider v3.0.144,您需要卸载 Exchange.Redis.Strongname.dll(版本 1.2.6)并通过 Nuget 安装 Exchange.Redis.dll(版本 2.0.601)

    【讨论】:

      猜你喜欢
      • 2017-09-28
      • 1970-01-01
      • 2023-03-08
      • 2012-12-08
      • 2019-10-16
      • 2014-12-24
      • 2014-07-16
      • 2014-05-01
      • 2013-07-08
      相关资源
      最近更新 更多