Note! An outputcache problem in ASP. net mvc 3 is an outputcache bug that is accompanied by ASP. NET from 1.0 to 4.0.

Source: Internet
Author: User

When I used ASP. net mvc 3 to rewrite the homepage of the blog site, I paid special attention to this cache problem. I will share this blog post.

If outputcache is used in ASP. net mvc 3, you must add the following code to the action. Remember!

Response.Cache.SetOmitVaryStar(true);

This is an outputcache bug with ASP. NET from 1.0 to 4.0. ASP. net mvc 3 is based on ASP. NET 4.0, so it cannot be avoided.

 

Problem demonstration

Next, let's try without response. cache. setomitvarystar (true.

Sample Action Code:

[OutputCache(Duration = 120)]public ActionResult SiteHome(int? pageIndex){    ...}

Note: The default value of outputcache. Location is outputcachelocation. Any (cache is performed on the server, client, and Proxy Server)

First request:

The second request (F5 refresh the browser ):

The third request (F5 refresh the browser ):

Then the fourth request will return 304, and the fifth request will return 200...

 

Try adding response. cache. setomitvarystar (true.

[OutputCache(Duration = 120)]public ActionResult SiteHome(int? pageIndex){    Response.Cache.SetOmitVaryStar(true);    ...}

First request:

The second request (F5 refresh the browser ):

The third request (F5 refresh the browser ):

Note: As long as the cache is valid, the server returns 304.

 

Problem Analysis

1. Differences between 200 and 304

When the return status code is 200, the server sends all the pages of the current request to the client (consuming downstream bandwidth ).

When the returned status code is 304, the server only sends this status code because the last-modified time provided by the client browser is within the cache validity period of the server, without sending any content on the page (almost no downstream bandwidth consumption), the browser directly retrieves the content from the local cache.

Therefore, the benefit of 304 is that it saves bandwidth and responds faster.

2. Impact on server Cache

If response. cache. setomitvarystar (true) is not added, the cache conditions on the server are the same. When setomitvarystar (true) is not added, the server resends the page content regardless of the cache of the client browser at every request of the same client browser, but as long as it is within the cache validity period, the content is still read from the server cache.

 

Hazards

The strange behavior of ASP. NET cache makes you waste bandwidth resources without knowing it.

 

Feelings

After years of development with ASP. NET, The outputcache bug from 1.0 to 4.0 of ASP. NET was discovered last year. In the previous test, after the first request, press F5 to check that 304 is returned, and the problem is exactly the next F5. Occasionally, press F5 to see 200. It can be seen that carefulness is very important to programmers. Many bugs and many performance problems are not enough, but not careful enough.

Excellent programmers are careful people, not only when writing code, but also in life. Don't look at him. He will observe and understand everything you have done to him. Be careful programmers and cherish careful programmers!

Related Article

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.