The performance loss of this mechanism means that ASP. NET of the server still needs to receive and process requests. The mechanism described in this article is to allow the browser to decide whether to read the cache by itself, so as to completely eliminate requests against the server.
1: reduce static page requests
To make static pages support this requirement, we need to use the Cache-Control: max-age in the http header. It is worth noting that Cache-Control is the identifier under HTTP/1.1, which is an upgrade of Expires in HTTP/1.0. To enable static pages to support Cache-Control, one method is to set in IIS, as shown in the following figure. Right-click the page or folder on which static pages need to be cached-> properties:
Here, I set the expiration time to 1 minute. Then, we use HttpWatch to observe the IE request to this page (the first request). The obtained HTTP header is as follows:
The obtained http header contains the Cache-Control: max-age = 60 option.
Now, I need to repeatedly request the static page within one minute. The request behavior is implemented in the following ways,
| F5 |
Indicates a refresh of the browser. It is valid for Last-Modified, but not for Cache-Control. |
| Click "go" or move the cursor to the address bar and press Enter. |
Effective for Cache-Control |
| CTRL + F5 |
Force refresh. All bodies are returned. |
The result obtained through HttpWatch is as follows:
Note that I use the red and red boxes in the diagram:
| First Request |
The return Status Code 200 is displayed. The entire body is 545 bytes. |
| F5 |
Refresh is effective for Last-Modified. It allows the server to determine whether to read the cache. Therefore, there are still requests and returned data. We can see that they are 352 and 239, respectively. The status code is 304. |
| Click "go" or move the cursor to the address bar and press Enter. |
Effective for Cache-Control, the browser determines whether to read the Cache. Because the Cache is within one minute, the browser does not send a request to the WEB server, we can see that the data of send and receive is all 0. No interaction, so no status code. |
| Ctrl + f5 |
It is equivalent to force refresh, so the status code is 545 OK and all body data is returned. We can see that the status code is the same as the first request, which is bytes. |
(In the above figure, there is a topic for favicon. ico 404, we should always provide favicon for the website. ico, which is a website LOGO you can set. Otherwise, some browsers will repeatedly request the LOGO, which will also lead to performance loss ).
After the above processing, we can further reduce the HTTP request and improve the efficiency compared with the previous one, because there are always some users who will "go" or move the cursor into the address bar and press enter to request data. You can set the cache time for a specific page as needed.
1.1 set static File Cache through web. config
In the preceding example, we set the static File Cache through IIS or in web. config, as shown in <configuration> ):
Copy codeThe Code is as follows: <system. webServer>
<Validation validateIntegratedModeConfiguration = "false"/>
<Modules runAllManagedModulesForAllRequests = "true"/>
<StaticContent>
<ClientCache cacheControlMode = "UseMaxAge" cacheControlMaxAge = "0.00: 00: 60"/>
</StaticContent>
</System. webServer>
1.2 disable static file caching
You can also disable caching for a folder or file as follows (under <configuration> ):Copy codeThe Code is as follows: <location path = "test2.htm">
<System. webServer>
<StaticContent>
<ClientCache cacheControlMode = "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 adopt the same policy for dynamic page requests, which requires us to write code to implement it ourselves:Copy codeThe Code is as follows: protected void Page_Load (object sender, EventArgs e)
{
// Click "go" or move the cursor into the address bar and press enter, which is described in this article.
This. Response. AddHeader ("Cache-Control", "max-age = 60 ");
// It is used to process F5 refresh, that is, it is effective for Last-Modified.
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 result is as follows:
The result is exactly the same as the static page we use.
2.1 use configuration file configuration
You can also configure dynamic files in batches in the configuration file as follows:
Copy codeThe Code is as follows: <system. web>
......
<Caching>
<OutputCacheSettings>
<OutputCacheProfiles>
<Add name = "cache1" duration = "60" location = "Client" varyByParam = "none"/>
</OutputCacheProfiles>
</OutputCacheSettings>
</Caching>
</System. web>
Then reference it at the front-end of the page:Copy codeThe Code is as follows: <% @ OutputCache CacheProfile = "cache1" %>
The code in this article is relatively simple, and the key is to understand the ideas contained in it, that is, regard the browser as an extremely important part of your website's cache.
Remarks
In this article, I used AddHeader to add http headers. in the previous article, a friend proposed to use Response. cache. the SetMaxAge method should be noted that using this method will cause 304 loss. The deep-seated reasons are not detailed.