Detailed introduction of Asp. Net Core 2.1 + View cache (Response cache), asp. netcore

Source: Internet
Author: User

Detailed introduction of Asp. Net Core 2.1 + View cache (Response cache), asp. netcore

The response cache Razor page and ASP. NET core 2.0 are not supported. This feature supports ASP. NET core version 2.1.

In the old version of MVC, there is an OutputCache feature that can cache the view, which can keep requests with the same parameter for a period of time, read directly from the mvc cache without going through the view logic.

[OutputCache (Duration = 20)] // sets the expiration time to 20 seconds. public ActionResult ExampleCacheAction () {var time = DateTime. now. toString ("MM dd, yyyy, HH, mm, ss seconds"); ViewBag. time = time; return View ();}

In Asp. Net core 2.1, the official document said: the response cache can reduce the number of requests sent by the client or proxy to the web server. The response cache can also reduce the workload of web servers to execute programs to generate responses. The response cache is determined by the header, specifying how you want the client, proxy, and cache response middleware to control.

In Asp. Net Core 2.1, if no OutputCache exists and the value is changed to ResponseCache, The ResponseCache must contain a parameter: The Duration unit is seconds, with at least one second set.

[ResponseCache (Duration = 5)] public IActionResult About () {ViewBag. time = DateTime. now. toString ("MM dd, yyyy, HH, mm, ss seconds"); return View ();}

Then the browser requests this view.

Max-age = 5 appears in the Cache-Control of the browser's response header.

The client will not accept responses whose retention time is 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 will not have any effect

Vary Filter

[ResponseCache (VaryByHeader = "User-Agent", Duration = 5)] public IActionResult About () {ViewBag. time = DateTime. now. toString ("MM dd, yyyy, HH, mm, ss seconds"); return View ();}

The role of vary in the Http response header is to tell the cache server or CDN that I still request from the same browser. You can just cache it for me. If you change the browser to request it, the value of vary must be blank, so the cache server will regard you as a new request and read the latest data to the browser.

References: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

Disable cache (NoStore and Location. None)

In Http: no-store, the 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 ("MM dd, yyyy, HH, mm, ss seconds"); return View ();}

ResponseCacheLocation. None is to set a no-Cache attribute in cache-Control so that the browser does not Cache the current URL.
Cache configuration (CacheProfiles) in a normal project, there must be many controllers, but it is impossible for each controller to have the same cache policy. At this time, we need a cache configuration to flexibly cope with this problem. When mvc service injection, we can inject it into our Cache Policy in 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 directly use the Configuration Policy Name When using it.

[ResponseCache (CacheProfileName = "test1")] public IActionResult About () {ViewBag. time = DateTime. now. toString ("MM dd, yyyy, HH, mm, ss seconds"); return View ();}

In this way, we can configure the same settings as before, and the code looks quite fresh.

Summary: My personal understanding of the response cache is that MVC does not request the server during the browser's refresh operation by returning the HTTP Response Header, read directly from the cache...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.