Asp. NET performance Optimization reduction Request _ Practical Tips

Source: Internet
Author: User
The performance loss of this mechanism is that the server's asp.net still receive requests and process requests. The mechanism in this article is to let the browser decide whether to read the cache itself, so that the request for the server is completely eliminated.

1: Reduce static page requests

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

I set the expiration time here to 1 minutes, and then we observe IE through HttpWatch to request the page (the first request) and get the HTTP headers 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, and the requested behavior is implemented in the following ways,

F5 A refresh on behalf of the browser, which is valid for last-modified, but not for Cache-control
Click "Go" or the cursor moves 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 logo in the Red and red boxes in the diagram:

First request Returns the status code 200, which obviously gets the full body, and is 545 bytes.
F5 Refresh, valid for last-modified, is to let the server decide whether to read the cache, so there are still requests and return data, we can see 352 and 239, respectively. The status code is 304.
Click "Go" or the cursor moves into the address bar and enter Valid for Cache-control, is the browser decides whether to read the cache, because in 1 minutes, so the browser did not send the request to the Web server, we can see send and receive data are all 0. No interaction, so no status code.
Ctrl+f5 The equivalent is forced refresh, so the status code 200OK, return all body data, we can see and the first request is the same, 545 bytes.

(digression, the above figure has 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 go to request the logo, this will also bring performance loss).

After the above processing, you can meet us compared to the previous one further reduced the HTTP request, improve the efficiency, because there are always some users will "go" or the cursor moved into the address bar and then enter to request data. We can set the cache time of a particular page according to the actual requirement.

1.1 Setting up static file caching via Web.config

In the example above, we set the cache of static files through IIS, or we can set them in Web.config, as follows (at <configuration>):

Copy Code code as follows:

<system.webServer>
<validation validateintegratedmodeconfiguration= "false"/>
<modules runallmanagedmodulesforallrequests= "true"/>
<staticContent>
<clientcache cachecontrolmode= "Usemaxage" cachecontrolmaxage= "0.00:00:60"/>
</staticContent>
</system.webServer>

1.2 Turning off the static file cache
You can also turn off caching for a separate folder or file, as follows (under <configuration>):
Copy Code code as follows:

<location path= "Test2.htm" >
<system.webServer>
<staticContent>
<clientcache cachecontrolmode= "Disablecache"/>
</staticContent>
</system.webServer>
</location>

2: Reduce dynamic page Requests
With the above static page or resource analysis, we know that we can use the same strategy for dynamic page requests, which requires us to write code to implement:
Copy Code code as follows:

protected void Page_Load (object sender, EventArgs e)
{
Processing click "Go" or the cursor moved 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, which 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"), out Ifmodifiedsince)
{
if ((Datetime.now-ifmodifiedsince.addhours (8)). Seconds < 60)
{
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 dynamic files in bulk in a configuration file as follows:

Copy Code code as follows:

<system.web>
......
<caching>
<outputCacheSettings>
<outputCacheProfiles>
<add name= "Cache1" duration= "location=" "Client" varybyparam= "None"/>
</outputCacheProfiles>
</outputCacheSettings>
</caching>
</system.web>

Then, at the front of the page, quote:
Copy Code code as follows:

<%@ OutputCache cacheprofile= "Cache1"%>

The key to this article is to realize the idea that the browser is a very important part of your site's cache.
Note
In this article, I used the AddHeader to add HTTP headers, and in the previous chapter a friend proposed to use the Response.Cache.SetMaxAge method, which needs to be reminded that using this method will result in the loss of the 304 state. The deep reason is no longer to scrutinize.

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.