Asp. NET performance Optimization reduction requests

Source: Internet
Author: User
Tags browser cache

In the previous article "ASP." NET performance optimization in the browser cache Dynamic Web page in the scenario, the browser sends if-modified-since will need to use their own cache to the Web server to decide, the server tells the browser to read the cache, the browser will read the cache. The performance loss of this mechanism is that the server's ASP. NET still receives the request, processing the request. The mechanism in this article is to let the browser decide whether or not to read the cache, thus completely eliminating the request for the server.

1: Reduce static page requests

To allow the static page to support this requirement, we need to use the Cache-control:max-age in the HTTP header. It is noteworthy that Cache-control is the identity under the http/1.1 protocol, which is an upgrade of expires in the http/1.0 protocol. In order for static pages to support Cache-control, one scenario is to set up in IIS, as below, I right-click on a page or folder that requires a static cache:

I'm here to set the expiration time to 1 minutes, and then we request the page via HttpWatch observation IE (first request), get the HTTP header as follows:

You can see that the cache-control:max-age=60 is already in the HTTP header that it gets.

Now, I need to repeatedly request this static page within 1 minutes, the behavior of the request is implemented in the following ways, respectively,

F5 Represents a refresh of the browser, which is valid for last-modified, but not valid for Cache-control
Click "Go" or move your cursor into the address bar and enter Effective for Cache-control
Ctrl+f5 Force refresh, return all body

The results we obtained through HttpWatch are as follows:

Note that I use the red and red boxes in the illustration:

First request Return status code 200, obviously get the full body, 545 bytes.
F5 Refresh, valid for last-modified, it is to let the server determine whether to read the cache, so, there are still requests and return data, we can see, respectively, is 352 and 239. The status code is 304.
Click "Go" or move your cursor into the address bar and enter For Cache-control, the browser itself decides whether to read the cache, because it is within 1 minutes, so the browser does not send a request to the Web server, we can see that send and receive data is all 0. No interaction, so no status code.
Ctrl+f5 Equivalent to a forced flush, so the status code 200OK, returning all the body data, we can see and the first request is the same as 545 bytes.

(Off the map above, there is a 404 for favicon.ico, we should always provide favicon.ico for the site, it is you can set the site logo, or some browsers will repeatedly to request the logo, which will also bring performance loss).

After the above processing, can meet us compared to the previous article further reduced the HTTP request, improved efficiency, because there are always some users through the "go" or the cursor into the address bar and then enter to request data. We can set the cache time for a particular page based on actual needs.

1.1 Setting up a static file cache via Web. config

In the example above, we set the cache for the static file through IIS, or we can set it in Web. config, as follows (under <configuration>):

<system.webserver>  <Validationvalidateintegratedmodeconfiguration= "false"/>  <Modulesrunallmanagedmodulesforallrequests= "true"/>  <staticcontent>    <ClientcacheCachecontrolmode= "Usemaxage"Cachecontrolmaxage= "0.00:00:60"/>  </staticcontent></system.webserver>

1.2 Closing the static file cache

You can also close the cache for a separate folder or file, as follows (<configuration>):

< LocationPath= "Test2.htm">  <system.webserver>    <staticcontent>      <ClientcacheCachecontrolmode= "DisableCache"/>    </staticcontent>  </system.webserver></ Location>

2: Reduce dynamic page Requests

With the analysis of the above static pages or resources, we know that we can use the same strategy for dynamic page requests, which requires us to write our own code implementation:

protected voidPage_Load (Objectsender, EventArgs e) {    //process Click "Go" or the cursor moves into the address bar and then enter, which is described in this article     This. Response.AddHeader ("Cache-control","max-age=60"); //Is really used to deal with F5 refresh, that is, to last-modified effective     This. Response.AddHeader ("last-modified", DateTime.Now.ToString ("U", Datetimeformatinfo.invariantinfo));    DateTime ifmodifiedsince; if(Datetime.tryparse ( This. Request.Headers.Get ("if-modified-since"), outifmodifiedsince)) {        if((Datetime.now-ifmodifiedsince.addhours (8)). Seconds < -) {Response.Status="304 Not Modified"; Response.statuscode=304; return; }    }}

The results are as follows:

The result is exactly the same as the static page we use.

2.1 Using configuration file Configuration

You can also configure the dynamic files in batches in the configuration file as follows:

<system.web>...<Caching>    <outputcachesettings>      <outputCacheProfiles>        <Addname= "Cache1"Duration= "$" Location= "Client"VaryByParam= "None"/>      </outputCacheProfiles>    </outputcachesettings>  </Caching></system.web>

Then in the page foreground reference:

<%@ OutputCache CacheProfile="cache1" %>

The code is relatively simple, the key is to understand the ideas contained therein, that is, the browser as a cache of your site is an extremely important part.

Note

In this article, I used AddHeader to add the HTTP header, a friend in the previous article to use the Response.Cache.SetMaxAge method, it should be reminded that the use of this method will result in the loss of 304 states. Deep-seated causes are no longer scrutiny.

Asp. NET performance Optimization reduction requests

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.