When you rewrite the home page of the blog Park website with ASP. 3, pay attention to this caching issue and share it with this blog post.
in ASP. NET MVC 3, if you use OutputCache, be sure to add the following code to the Action, remember!
Response.Cache.SetOmitVaryStar (TRUE);
This is a companion to ASP. 1.0 to 4.0 OutputCache Bug,asp.net MVC 3 is based on the ASP. NET 4.0, so also hide.
Problem Demo
Let's try it without Response.Cache.SetOmitVaryStar (true); The situation.
Example Action code:
[OutputCache (Duration = +)]public actionresult sitehome (int? pageIndex) { ...}
Note: The default value for Outputcache.location is Outputcachelocation.any (server, client, proxy, etc. are cached)
First time Request:
Second request (F5 Refresh browser):
Third request (F5 Refresh browser):
Then the fourth request returns 304, and the fifth request returns 200 ...
Then experience the addition of Response.Cache.SetOmitVaryStar (true); The situation.
[OutputCache (Duration = +)]public actionresult sitehome (int? pageIndex) { Response.Cache.SetOmitVaryStar (true) ; ...}
First time Request:
Second request (F5 Refresh browser):
Third request (F5 Refresh browser):
Note: The server returns 304 as long as it is within the cache validity period.
Problem analysis
1. The difference between 200 and 304
When the return status code is 200, the server side sends the entire page of the current request to the client (consuming downstream bandwidth).
When the return status code is 304, due to the last-modified time provided by the client browser during the server-side cache validity period, the server sends only this status code, does not send any content of the page (almost does not consume downstream bandwidth), the browser directly from the local cache to obtain content.
So, the 304 benefit is to save bandwidth and respond faster.
2. Impact on server-side caching
Add no Response.Cache.SetOmitVaryStar (TRUE), the server cache is the same. Just without Setomitvarystar (true), for the same client browser, every other request, the server will resend the page content regardless of the client browser's cache, but as long as the content is read from the server-side cache during the cache validity period.
Problem hazards
This bizarre behavior of the ASP. NET cache lets you unknowingly waste bandwidth resources.
Attention! An OutputCache problem with ASP. NET MVC 3