ASP. NET performance optimization to allow the browser to cache dynamic web pages

Source: Internet
Author: User

Previous Article ASP. NET Performance Optimization to build a custom File Cache. We use OutputCache to allow requests to access the server's asp.net output cache. We have extended OutputCacheProvider, which is equivalent to accessing static resources on the server. OutputCache is intended for all users accessing server resources. The browser cache described in this article is intended for individual users, so that the browser does not continuously access the dynamic content on the server under our control, that is, we need to make the browser part of our cache mechanism and maximize ASP in some specific scenarios. NET Site performance. If OutputCache improves concurrency efficiency in terms of breadth, browser cache improves efficiency in depth.

I. Introduction to HTTP headers

1.1 first browser request

Assume that we request a URL address. For example, if a static page on my server is http: // 192.168.0.77/luminji2/html/test1.htm, the following HTTP header information is returned:

The meanings of each parameter in the HTTP header information are not listed at the moment. We will focus on the three information related to this article:

First, the response status is 200OK, indicating that the data is successfully captured from the server.

Next, Last-Modified:Fri, 09 Sep 2011 02:56:45 GMT

The WEB server is telling the browser that the last modification date of this file isFri, 09 Sep 2011 02:56:45 GMT. It must be noted that it is GMT, that is, Greenwich Mean Time. Generally, the GMT + 8 time zone is used in China (depending on the region settings of the system ).

Etag,The flag value of the object Currently responded by the WEB server. For example, if an html file is modified, its Etag will not be modified.

1.2 What is browser cache

I am using FireFox. I typed "about: cache" in the address bar? Device = disk, we will see the above HTML page cached by the browser, as shown below:

(Note that the Last Modified here has nothing to do with the Last-Modified in the Http header ).

Each browser has its own caching mechanism, but it is similar. It is not shown here for the time being.

1.3 How to hit Cache

Request the URL again. The header information is as follows:

The status changes to 304 Not Modified, which means the WEB server tells the browser to use its own cache instead of downloading the body content here. So what is the basis for the WEB server to tell the browser?

Here, we need If-Modified-Since in the request header information. The request header is sent by the browser to the WEB server. Once this parameter is included, the browser tells the WEB Server:Fri, 09 Sep 2011 02:56:45 GMT, your content has not changed. The WEB server will judge based on this. If there is no change, it will return the 304 Not Modified to the browser, just like in this example. In this way, the browser will take the text data locally, reducing network traffic.

If-None-Match is the Etag judgment mode, which is consistent with the purpose of Last-Modified.

Assume that we modified the test1.htm file. Imagine what the result will be. It must be 200OK. This mechanism is used between browsers and WEB servers to cache static WEB pages.

II. Implementation of asp.net browser cache

The above is the case of static pages, so what is the situation of aspx pages, that is, dynamic pages? Now, let's create a dynamic web page. First, create a simple aspx, as shown below:

<body>    <%=DateTime.Now %></body>

Request the following header information:

Then we made multiple requests, and we found that every time it was OK, and we found that a very important information was lost in the header information, that is, Last-Modified. The server did not tell the browser the last modification date of its own object, so the browser had to go to the server to retrieve all the data each time. Here, we should understand that to prevent the browser from getting data, the dynamic program has to try to add the header information by itself.

Okay. Now we can add the simplest header information in the background code of ASPX:

        protected void Page_Load(object sender, EventArgs e)        {            this.Response.AddHeader("Last-Modified", DateTime.Now.ToString("U", DateTimeFormatInfo.InvariantInfo));        }

After adding the header information, we find that after the URL is requested again, the header information becomes as follows:

The response header contains Last-Modified, and the request header contains If-Modified-Since.

Of course, we still find that each request is still 200OK. This is of course. Since the header information is added by ASP. NET in the background, we have to write the response status to the server. Now, let's assume that we want to cache the browser for 10 seconds. The complete code should be as follows:

        protected void Page_Load(object sender, EventArgs e)        {            this.Response.AddHeader("Last-Modified", DateTime.Now.ToString("U", DateTimeFormatInfo.InvariantInfo));            DateTime IfModifiedSince = DateTime.Parse(this.Request.Headers.Get("If-Modified-Since"));            if ((DateTime.Now - IfModifiedSince.AddHours(8)).Seconds < 10)            {                Response.Status = "304 Not Modified";                Response.StatusCode = 304;            }        }

After this modification, If we continuously request this aspx page within 10 seconds, the system will always return the 304 status, that is, the browser will not get the body from the server, it will only read its own cache locally, so that the pressure on the server is naturally less. If we do not request this page from the server within 10 seconds, then OK will be returned in 10 seconds, that is, the page data will be retrieved from the server again.

Now, we use AB to simulate 100 concurrent users for 1000 requests. The comparison result is as follows (Note: To enhance the effect, we need to simulate some time-consuming actions in the background, for example, read the database ):

The left side is the aspx page that has not been cached, and the right side is the cache aspx page. We can see that the throughput is 10 times different.

Tip: when using AB for stress testing, add the If-Modified-Since Header information. The command is as follows:

C: \> AB-n1000-c100-H "If-Modified-Since: Friday, 09 September 2011 09:35:23 GMT" http: // 192.168.0.77/luminji2/aspx/test1.aspx

Download the code in this article: MvcApplication320110909.rar

Iii. Question proposal

In the browser cache implementation mentioned above, the browser determines whether to call the cache through the communication and coordination mechanism with the WEB server. This means that the dynamic program still needs to process requests from the client, if there is a mechanism that allows the browser to decide whether to call the cache without requesting the server, it can completely remove the server to process this process. This mechanism will be further elaborated in the next article.

 

ASP. NET performance optimization-Previous Article: ASP. NET performance optimization-Build a custom File Cache

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.