输出缓存/响应缓存VaryByParam

问题描述 投票:0回答:7

ResponseCache
在某种程度上是
OutputCache
的替代品;但是,我想进行服务器端缓存以及每个参数输入。

根据一些答案herehere,我应该使用

IMemoryCache
IDistributedCache
来执行此操作。我对参数不同的控制器上的缓存特别感兴趣,以前在 ASP.NET 4 中使用
OutputCache
VaryByParam
完成,如下所示:

[OutputCache(CacheProfile = "Medium", VaryByParam = "id", Location = OutputCacheLocation.Server)]
public ActionResult Index(long id) 
{ 
    ///...
}

我将如何在 ASP.NET Core 中复制它?

c# asp.net-web-api asp.net-caching asp.net-core-1.0
7个回答
11
投票

如果要通过控制器中所有请求中的所有请求查询参数来更改缓存:

[ResponseCache(Duration = 20, VaryByQueryKeys = new[] { "*" })]
public class ActiveSectionController : ControllerBase
{
   //...
}

10
投票

首先确保您使用的是 ASP.NET Core 1.1 或更高版本。

然后在您的控制器方法上使用与此类似的代码:

[ResponseCache(Duration = 300, VaryByQueryKeys = new string[] { "date_ref" } )]
public IActionResult Quality(DateTime date_ref)

来源:https://learn.microsoft.com/en-us/aspnet/core/performance/caching/middleware


5
投票

Asp.Net Core 7 添加了 Output Caching 中间件。这正是您想要的,即服务器缓存。不要将其与您所指的旧 .Net Framework OutputCacheAttribute 混淆。

OutputCache 和 ResponseCache 的主要区别:

  • OutputCache 仅用于服务器缓存(它甚至没有 Location 属性)
  • ResponseCache 主要用于浏览器缓存并使用 HTTP 缓存标头

您可以将它们用于同一个端点。


3
投票

在asp.net core中使用它

[ResponseCache(CacheProfileName = "TelegraphCache", VaryByQueryKeys = new[] { "id" })]

1
投票

对于寻找答案的人...毕竟有 IMemoryCache 但不像以前那么漂亮

ActionFilterAttribute
但具有更多灵活性。
长话短说(对于.Net core 2.1,主要由微软文档+我的理解):
1- 将
services.AddMemoryCache();
服务添加到
ConfigureServices
文件中的
Startup.cs

2- 将服务注入您的控制器:

public class HomeController : Controller
{
  private IMemoryCache _cache;

  public HomeController(IMemoryCache memoryCache)
  {
      _cache = memoryCache;
  }

3-任意(为了防止拼写错误)声明一个包含一堆键名称的静态类:

public static class CacheKeys
{
  public static string SomeKey { get { return "someKey"; } }
  public static string AnotherKey { get { return "anotherKey"; } }
  ... list could be goes on based on your needs ...

我更喜欢声明一个

enum

public enum CacheKeys { someKey, anotherKey, ...}

3- 在 actionMethods 中使用它,如下所示:
对于获取缓存值:
_cache.TryGetValue(CacheKeys.SomeKey, out someValue)

或者重置值如果
TryGetValue
如果失败:

_cache.Set(CacheKeys.SomeKey, 
           newCachableValue, 
           new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromSeconds(60)));  

结束。


1
投票

我遇到了同样的问题,以下都没有产生预期的唯一参数值缓存。始终返回初始结果:

VaryByQueryKeys = new[] { "*" }

VaryByQueryKeys = new[] { "searchparam" }

给予

public async Task<IActionResult> Search([FromQuery] string searchparam)

解决方案是在其前面加上

$
,即
VaryByQueryKeys = new[] { "$searchparam" }


0
投票
在我看来,.NET7 和 .NET8 的 Microsoft

文档 并不那么清楚,但我为最小 API 找到了它:

var builder = WebApplication.CreateBuilder(args); builder.Services.AddOutputCache(); // add this builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); var app = builder.Build(); if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } app.UseHttpsRedirection(); app.UseOutputCache(); // add this app.MapGet("/products/{id}", async (string id) => { return Results.Ok("someresult"); }).CacheOutput(c => c.VaryByValue((context) => new KeyValuePair<string, string>( "id", context.Request.RouteValues["id"].ToString()))); app.Run();
    
© www.soinside.com 2019 - 2024. All rights reserved.