View cache for ASP. NET Core 2.1+ (response cache)

Source: Internet
Author: User

The response Cache Razor page is not supported in ASP. NET Core 2.0. This feature will support the ASP. NET Core 2.1 release.

In the old version of MVC, there is a feature that caches views (OutputCache) that can hold requests for the same parameter, read directly from the MVC cache for n periods of time, and not go through the logic of the view.

    [OutputCache (Duration =20)]//set expiration time is 20 seconds public      ActionResult examplecacheaction ()      {          var  time= DateTime.Now.ToString ("yyyy mm month DD day hh" mm min ss seconds ");          Viewbag.time= time;          return View ();      }  

in ASP. NET Core 2.1, the official documentation says: response caching reduces the number of requests to the Web server by the client or agent. The response cache also reduces the amount of work that the Web server executes to generate the response. The response cache is defined by the header, which specifies how you want the client, proxy, and cache responses to be controlled by the middleware.

in ASP. NET Core 2.1, without the outputcache, replaced by Responsecache, Responsecache must take one parameter: Duration unit is seconds, set at least one second

[Responsecache (Duration = 5)]        Public Iactionresult About ()        {            viewbag.time = DateTime.Now.ToString ("yyyy mm month dd Day hh mm min ss sec");            return View ();        }

And then the browser requests this view

Max-age=5 appears in the Cache-control of the browser's response header, which is interpreted by the HTTP protocol

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

If the cache is disabled in the browser, then Responsecache will not have any effect

Vary filtration

[Responsecache (VaryByHeader = "User-agent", Duration = 5)]        Public Iactionresult About ()        {            viewbag.time = DateTime.Now.ToString ("yyyy mm month dd Day hh mm min ss sec");            return View ();        }

About vary in the HTTP response header function is: Tell the cache server or CDN, I am still the same browser request, you give me cache on the line, if you change a browser to request, then the value of vary is definitely empty, then the cache server will think you are a new request, It will read the latest data to the browser.

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

Disabling caching (Nostore and Location.none)

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

[Responsecache (location = Responsecachelocation.none, Nostore = True)]        Public Iactionresult About ()        {            viewbag.time = DateTime.Now.ToString ("yyyy mm month dd Day hh mm min ss sec");            return View ();        }
Responsecachelocation.none is to set a No-cache property in Cache-control so that the browser does not cache the current URL

缓存配置(CacheProfiles)

在一个正常的项目中,肯定有很多个控制器,但是不可能每个控制器的缓存策略都一样,这时候,我们就需要一个缓存的配置来灵活应对这个问题
在mvc的服务注入的时候,我们可以在option里面注入进我们的缓存策略
Services. Addmvc (option=> {                option. Cacheprofiles.add ("Test1", New CacheProfile ()                {                    Duration = 5                });                Option. Cacheprofiles.add ("Test2", New CacheProfile ()                {location                    = responsecachelocation.none,                    Nostore = True                });            });

And then when we're using it, just use the name of the configuration policy right now.

[Responsecache (Cacheprofilename = "test1")]        Public Iactionresult About ()        {            viewbag.time = DateTime.Now.ToString ("yyyy mm month dd Day hh mm min ss sec");            return View ();        }

这样我们就能和之前在特性后边配置一样了,而且代码看起来也清爽了不少


Summary: For the response cache, my personal understanding is: MVC by returning the HTTP response header, so that the browser in how much time to perform the refresh operation, do not request the server, read directly from the cache ...

Https://www.cnblogs.com/boxrice/p/8492508.html

View cache for ASP. NET Core 2.1+ (response 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.