简体   繁体   English

将响应缓存保存到 Net 5 中的服务器

[英]Saving the response cache to the server in Net 5

As in Net MVC, I want to save the response cache to the server (it's called outputcache), but there is no such feature in Net Core or Net 5. I couldn't find an alternative method.与在 Net MVC 中一样,我想将响应缓存保存到服务器(称为 outputcache),但在 Net Core 或 Net 5 中没有这样的功能。我找不到替代方法。 Thank you in advance.先感谢您。

You can use WebEssentials.AspNetCore.OutputCaching nuget to achieve your requirement.您可以使用WebEssentials.AspNetCore.OutputCaching nuget 来满足您的要求。

Follow the steps below:请按照以下步骤操作:

1.Add the middleware: 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.Add OutputCache attribute to the action: 2.为动作添加OutputCache属性:

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

A sample code for testing:用于测试的示例代码:

Controller: Controller:

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

View:看法:

@model DateTime

Time of request: @Model.ToString()

Try requesting the page and notice that the date and time remains the same, even when reloading the page, until the cache has expired.尝试请求页面并注意日期和时间保持不变,即使在重新加载页面时也是如此,直到缓存过期。

OutputCache contains not least Duration option, but also other options, such as VaryByHeader , VaryByParam and so on... OutputCache 不仅包含Duration选项,还包含其他选项,例如VaryByHeaderVaryByParam等...

More details you can refer to the github repo for WebEssentials.AspNetCore.OutputCaching更多细节可以参考github repo for WebEssentials.AspNetCore.OutputCaching

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM