Attention! An OutputCache problem with ASP. NET MVC 3

Source: Internet
Author: User

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

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.