【问题标题】:Configuring System.Runtime.Caching in appSettings.json在 appSettings.json 中配置 System.Runtime.Caching
【发布时间】:2019-01-07 12:04:04
【问题描述】:

我正在尝试在完整的 .NET Framework 4.7 上运行的 Asp.NET Core 2.1 应用程序中配置 System.Runtime.Caching.MemoryCache

在另一个非 Asp.NET Core 应用程序中,我在 Web.config 中进行了配置,如下所示:

<system.runtime.caching>
  <memoryCache>
    <namedCaches>
      <add name="default" cacheMemoryLimitMegabytes="0" physicalMemoryLimitPercentage="0" pollingInterval="00:02:00" />
    </namedCaches>
  </memoryCache>
</system.runtime.caching>

但是,在 Asp.NET Core 2.1 应用程序中,我只有 appsetting.json。在 Asp.NET Core 2.1 中如何配置System.Runtime.Caching.MemoryCache

【问题讨论】:

    标签: c# asp.net asp.net-core appsettings memorycache


    【解决方案1】:

    我建议您查看Cache in-memory in ASP.NET Core,因为它似乎准确地描述了您的需求。

    更新

    您应该尝试使用AddMemoryCache 重载,它接受用于配置缓存的 lambda/方法(请参阅MemoryCacheOptions),它可能看起来像:

    services.AddMemoryCache(options => 
    { 
        options.SizeLimit = 1024; // Can be hardcoded or read from a custom configuration value
    })
    

    更新 2

    如果您想将旧包与 ASP.Net Core 一起使用,您将无法使用 appSettings.json 来配置它,因为 .Net Core' s 使用不同的配置堆栈。

    但是,除了 appsetings.json 文件之外,仅删除 web.config 文件可能会起作用。如果不是这样,您将需要进行一些繁重的自定义,以便在加载缓存机制之前以某种方式使用新配置堆栈中的值加载旧配置堆栈,这绝非易事。

    希望对你有帮助!

    【讨论】:

    • 应用程序中使用 System.Runtime.Caching 的部分是与我们套件中的其他非核心应用程序共享的模块,因此我无法轻易将其重写为特定于核心的东西。跨度>
    • System.Runtime.Caching 不是特定于核心的,您需要的唯一特定于核心的东西应该在您的 Startup 类中,无论如何它都是特定于核心的。
    • 对不起,可能是我误会了。该链接描述了使用 System.Runtime.Caching 的替代方法,但我仍想使用 System.Runtime.Caching,因为有问题的应用程序不是唯一依赖于代码的应用程序。我只想从 appsettings.json 配置它。
    • 目标是Microsoft.Extensions.Caching,而不是System.Runtime.Caching
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-29
    • 2017-11-23
    • 2017-04-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多