Asp. NET performance optimization method for browsers to cache Dynamic Web pages-Practical tips

Source: Internet
Author: User
Tags browser cache
OutputCache is for all users accessing the server resources, the browser cache to be described in this article is for individual users, let the browser under our control completely not continuous access to dynamic content on the server, that is, we want to make the browser into our caching mechanism part of the Maximizes the performance of asp.net sites in certain scenarios. If OutputCache is a breadth-enhancing concurrency efficiency, browser caching is a deep enhancement of efficiency.

One: Introduction to HTTP Headers

1.1 The first time the browser requests

Suppose we request a URL address, such as a static page http://192.168.0.77/luminji2/html/test1.htm on my server, that returns the following HTTP header information:

The meaning of each parameter in HTTP header information Here we are concerned about the 3 information related to this article:

First, the response status is 200OK, which means the success of fetching data from the server.

Second, last-modified:Fri, 09 Sep 2011 02:56:45 GMT

这是WEB服务器在告诉浏览器,我这个文件的最后修改日期是Fri, 09 Sep 2011 02:56:45 GMT。必须要说明的是,它是GMT时间,也就是格林威治时间,一般中国境内使用的是GMT+8时区(要看系统的区域设置而定)。

最后就是Etag,The flag value of this object that the Web server is currently responding to, in terms of an object, such as an HTML file, which, if modified, will not be modified Etag.

1.2什么是浏览器缓存

我使用的是FireFox,在地址栏内敲入about:cache?device=disk,我们就会看到被浏览器缓存起来的上面这个HTML页面,如下:

(Note that the last modified here has nothing to do with the last-modified in the HTTP header).

Each browser will have its own caching mechanism, but all the same, here is not the table.

1.3 How to hit the cache

Again request just the URL, we get the header information as follows:

You can see the status changed to 304 not modified, which is equivalent to the Web server telling the browser, please use your own cache, do not come to me to download the body content. So what does the Web server decide to tell the browser?

Here, we need to request the If-modified-since in the header information. The request headers are sent to the Web server by the browser, and once this parameter is included, the browser and the Web server say: please check sinceFri, 09 Sep 2011 02:56:45 GMT一来,你的内容变动过没。WEB服务器就会根据这个来判断,如果没有变动过,就会给浏览器返回304 Not Modified,就像本例。这样子,浏览器就会去本地拿正文数据,减少了网络流量。

If-none-match is ETag judgment mode, and last-modified actually complete the purpose is consistent, here for the moment not table.

Suppose we modify the file test1.htm, imagine what will be the result, it must be 200OK. This mechanism is used between browsers and Web servers to complete the caching of static Web pages.

Second: ASP.net browser cache implementation

Above we are talking about the static page, then the ASPX page, that is, dynamic page will be what kind of situation? Now, let's create a dynamic Web page to look at. First, create one of the simplest ASPX, as follows:

Copy Code code as follows:

<body>
<%=datetime.now%>
</body>

To request, get the header information as follows:

And then again and again, we found that each time it was 200OK, and we found a very important message missing from the header, and that was last-modified. The server did not tell the browser its own object's last modification date, so the browser had to go to the server each time to retrieve all the data. See here, we should understand that in order to let the browser not to get the data, the dynamic program has to try to add this header information themselves.

OK, so now we're going to implement one of the simplest header information additions in the ASPX background code:

Copy Code code as follows:

protected void Page_Load (object sender, EventArgs e)
{
This. Response.AddHeader ("Last-modified", DateTime.Now.ToString ("U", Datetimeformatinfo.invariantinfo));
}

After we have added the header information, we find that after we request the URL again, the header information becomes as follows:

Can be delighted to see that the response header contains last-modified, and the request header contains the if-modified-since.

Of course, we still find that each request is still 200OK. This is of course, since the header information is asp.net in the background, then we want to return what kind of response state to the server this logic must also be written by themselves. Now, let's say we want the browser to cache 10 seconds of the time, and the complete code should look like this:

Copy Code code as follows:

protected void Page_Load (object sender, EventArgs e)
{
This. Response.AddHeader ("Last-modified", DateTime.Now.ToString ("U", Datetimeformatinfo.invariantinfo));
DateTime ifmodifiedsince;
If Datetime.tryparse (this. Request.Headers.Get ("If-modified-since"), out Ifmodifiedsince)
{
if ((Datetime.now-ifmodifiedsince.addhours (8)). Seconds < 10)
{
Response.Status = "304 Not Modified";
Response.statuscode = 304;
Return
}
}
Other
}

After this change, if we continue to request the ASPX page within 10 seconds, then always return 304 status, that is, the browser will not go to the server to get the body, only local to read their own cache, so that the server pressure naturally small. If we do not request this page in 10 seconds, then 10 seconds will return 200OK, that is, to the server to get the page data.

Now, using AB to simulate 100 concurrent users to make 1000 requests, the comparison results are as follows (note that in order to enhance the effect, we need to simulate some of the more time-consuming actions in the background, such as reading the database):

On the left is an ASPX page that is not cached, and on the right is a cached ASPX page that can be seen with a 10 times-fold difference in throughput.

Tip, when using AB for stress testing, you need to add If-modified-since header information, which commands the following:

C:\>ab-n1000-c100-h "If-modified-since:friday, September 09:35:23 GMT" http://192.168.0.77/luminji2/aspx/t Est1.aspx

This article code downloads: Mvcapplication320110909.rar

Third: The question of the proposed

In the browser cache implementation mentioned above, the browser uses the communication coordination mechanism with the Web server to determine whether it needs to call the cache, which means that the dynamic program still needs to process requests from the client, and if there is a mechanism that allows the browser to decide whether to call the cache without requesting the server, will be able to completely shed the server to deal with this link. The next article will continue to elaborate on this mechanism.

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.