【问题标题】:Saving the response cache to the server in Net 5将响应缓存保存到 Net 5 中的服务器
【发布时间】:2022-01-06 12:45:10
【问题描述】:

与在 Net MVC 中一样,我想将响应缓存保存到服务器(称为 outputcache),但在 Net Core 或 Net 5 中没有这样的功能。我找不到替代方法。提前谢谢你。

【问题讨论】:

  • 你好@Blackerback,你可以参考this doc学习ASP.NET Core中的响应缓存。
  • 你好@Rena,这个和很多文档我都看了很多遍,但是当我按F5键的时候,所有的缓存数据都被删除了,我需要使用服务器端缓存结构来防止它被删除(OutputCache Location.Server 等...)。

标签: caching asp.net-core-mvc http-caching httpresponsecache responsecache


【解决方案1】:

您可以使用WebEssentials.AspNetCore.OutputCaching nuget 来满足您的要求。

按照以下步骤操作:

1.添加中间件:

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }
    public void ConfigureServices(IServiceCollection services)
    {
         services.AddOutputCaching();
         //other middleware...
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        app.UseOutputCaching();

        //other middleware...
    }
}

2.给动作添加OutputCache属性:

[OutputCache(Duration = 600)]
public IActionResult Index()
{ ... }

测试示例代码:

控制器:

[OutputCache(Duration = 600)]
public IActionResult Index()
{        
    return View(DateTime.Now);
}

查看:

@model DateTime

Time of request: @Model.ToString()

尝试请求页面并注意日期和时间保持不变,即使在重新加载页面时也是如此,直到缓存过期。

OutputCache 不仅包含Duration 选项,还包含其他选项,例如VaryByHeaderVaryByParam 等等...

更多详情可以参考github repo for WebEssentials.AspNetCore.OutputCaching

【讨论】:

猜你喜欢
  • 2013-12-07
  • 1970-01-01
  • 2019-09-26
  • 2013-12-10
  • 1970-01-01
  • 2023-02-14
  • 1970-01-01
  • 2011-10-09
  • 1970-01-01
相关资源
最近更新 更多