Detail view cache (response cache) of ASP. Net core 2.1 +

Time:2020-2-5

Response cache razor pages are not supported in asp.net core 2.0. This feature will support asp.net core version 2.1.

In the old version of MVC, there is a feature that can cache the view (outputcache), which can keep the request of the same parameter. In n period of time, it can read directly from the cache of MVC without going to view logic.

[outputcache (duration = 20)] // set the expiration time to 20 seconds 
  public ActionResult ExampleCacheAction() 
  { 
    Var time = datetime. Now. ToString ("HH, mm, DD, yyyy, mm, mm, min, SS, s)"; 
    ViewBag.time= time; 
    return View(); 
  }

In ASP. Net core 2.1, the official document states that response caching can reduce the number of requests from clients or agents to web servers. Response caching can also reduce the amount of work that web server executors do to generate responses. Response caching is controlled by headers that specify how you want clients, agents, and middleware to cache responses.

In ASP. Net core 2.1, there is no outputcache, instead of responsecache. The responsecache must take a parameter: duration unit is second, and the minimum setting is one second

[ResponseCache(Duration = 5)]
    public IActionResult About()
    {

      Viewbag. Time = datetime. Now. ToString ("yyyy mm / DD / HH, mm, mm, SS, s)";

      return View();
    }

Then the browser requests the view

Max-age=5 appears in the Cache-Control of the response header of the browser, which is explained by the Http protocol

The client will not accept a response with a retention time greater than the specified number of seconds. Example:max-age=60(60 seconds),max-age=2592000(1 month)

If caching is disabled in the browser, responsecache has no effect

Vary filtering

[ResponseCache(VaryByHeader = "User-Agent", Duration = 5)]
    public IActionResult About()
    {

      Viewbag. Time = datetime. Now. ToString ("yyyy mm / DD / HH, mm, mm, SS, s)";

      return View();
    }

The function of vary in the HTTP response header is to tell the cache server or CDN that I am still the same browser’s request, and you can cache it for me. If you change the browser to request, then the value of vary must be empty, then the cache server will think you are a new request, and it will read the latest data to the browser

Reference: http://www.w3.org/protocols/rfc2616/rfc2616-sec14.html

Disable caching (nostore and location. None)

In http: no store, request and response information should not be stored in the disk system of the other party;

[ResponseCache(Location = ResponseCacheLocation.None, NoStore = true)]
    public IActionResult About()
    {

      Viewbag. Time = datetime. Now. ToString ("yyyy mm / DD / HH, mm, mm, SS, s)";

      return View();
    }

Responsecachelocation.none is to set a no cache property in cache control so that the browser does not cache the current URL
In a normal project, there must be many controllers for cache profiles, but it’s impossible for every controller to have the same cache strategy. At this time, we need a cache configuration to deal with this problem flexibly. When MVC services are injected, we can inject our cache strategy into option


services.AddMvc(option=> {
        option.CacheProfiles.Add("test1", new CacheProfile()
        {
          Duration = 5
        });
        option.CacheProfiles.Add("test2", new CacheProfile()
        {
          Location = ResponseCacheLocation.None,
          NoStore = true
        });
      });

Then we can use the name of configuration policy directly

[ResponseCache(CacheProfileName = "test1")]
    public IActionResult About()
    {

      Viewbag. Time = datetime. Now. ToString ("yyyy mm / DD / HH, mm, mm, SS, s)";

      return View();
    }

In this way, we can configure it as before, and the code looks fresh

Summary: for response cache, my personal understanding is: MVC returns the HTTP response header to let the browser in how much time, when performing the refresh operation, do not request the server, directly read from the cache…