简体   繁体   English

ASP.NET内核中响应缓存中间件的使用和好处

[英]The use and benefits of Response Caching Middleware in ASP.NET Core

I wanted to try caching mechanism in my .NET core API project.我想在我的 .NET 核心 API 项目中尝试缓存机制。

I followed this article to configure the middleware https://thecodeblogger.com/2021/06/06/middleware-for-response-caching-in-net-core-web-apis/我按照这篇文章配置中间件https://thecodeblogger.com/2021/06/06/middleware-for-response-caching-in-net-core-web-apis/

Startup.cs启动.cs

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers();


        // Response Caching Middleware
        services.AddResponseCaching(options =>
        {
            // Each response cannot be more than 1 KB 
            options.MaximumBodySize = 1024;

            // Case Sensitive Paths 
            // Responses to be returned only if case sensitive paths match
            options.UseCaseSensitivePaths = true;
        });
    }


public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseHttpsRedirection();

        app.UseRouting();

        // Response Caching Middleware
        app.UseResponseCaching();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }

The controller controller

   [HttpGet]
    [ResponseCache(NoStore = false, Duration = 600, Location = ResponseCacheLocation.Any)]
    public IEnumerable<WeatherForecast> Get()
    {
        var rng = new Random();
        return Enumerable.Range(1, 5).Select(index => new WeatherForecast
        {
            Date = DateTime.Now.AddDays(index),
            TemperatureC = rng.Next(-20, 55),
            Summary = Summaries[rng.Next(Summaries.Length)]
        })
        .ToArray();
    }

And I can see in the response this header我可以在响应中看到这个 header

Cache-Control : public,max-age=600

So I put a break point in the controller, whenever I call the api thorugh postman or browsers it hit the api. So I put a break point in the controller, whenever I call the api thorugh postman or browsers it hit the api.

I was assuming adding the header Cache-Control: public,max-age=600 will create a cache of the response in the client for 600 seconds and it won't hit the backend for each request.我假设添加 header Cache-Control: public,max-age=600将在客户端中创建 600 秒的响应缓存,并且不会针对每个请求访问后端。

If not, then what is the actual point of the Cache-Control如果不是,那么Cache-Control的实际意义是什么

Most browser will have in the request header Cache-Control: max-age=0 which will override the response header's Cache-Control, forcing the response to not be cached.大多数浏览器都会在请求 header Cache-Control: max-age=0中覆盖响应头的 Cache-Control,从而强制不缓存响应。

For the Chrome browser, to not pass this Cache-Control: max-age=0 header, open the Chrome console and type对于 Chrome 浏览器,要不通过此Cache-Control: max-age=0 header,请打开 Chrome 控制台并输入

location = "https://your.page.com"

In your application codebase, you can set the request header to null to remove the default Cache-Control: max-age=0 header.在您的应用程序代码库中,您可以将请求 header 设置为 null 以删除默认的Cache-Control: max-age=0 header。

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

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