Source: http://www.jb51.net/article/15671_2.htm
A lot of people first want to get started with the program from the server cache.
Optimization: many different server cache methods have their own characteristics. For example, in some projects I have participated in, I used COM +/enterprise based on different cache hit rates.
Libiary caching/Windows Services, static files, and other server-side cache and HTTP
Compression technology, but the client cache is often ignored, even if the server cache makes your page access very fast, but she still needs to rely on the browser to download and output, and when you
When you add the client cache, it will bring you a lot of benefits, because she can cache the most frequently accessed pages on the site to fully improve the web
Server throughput (usually calculated based on the number of requests per second) to improve application performance and scalability.
According to an online shopping survey, most people are willing to queue up in the store, but they are not willing to wait for online shopping. According to the websense survey, as many as 70% of netusers say they do not want to read pages for more than 10 seconds. More than 70% of users will cancel the current order because of the slow speed.
Basic knowledge
1) What is "last-modified "?
When the browser requests a URL for the first time, the server returns 200, and the content is the resource you requested, at the same time, there is a last-modified attribute to mark the last modification time of this file on the service end. The format is similar to this:
Last-modified: Fri, 12 May 2006 18:53:33 GMT
When the client requests this URL for the second time, according to the HTTP protocol, the browser will send the IF-modified-since header to the server, asking if the file has been modified after the time:
If-modified-since: Fri, 12 May 2006 18:53:33 GMT
For example
If the server resources remain unchanged, HTTP 304 (not
Changed.) status code. The content is empty, which saves the amount of data transmitted. When the server code changes or the server is restarted, the resource is re-issued, and the returned result is similar to the first request.
This ensures that resources are not repeatedly sent to the client, and that the client can obtain the latest resources when the server changes.
2) What is "etag "?
HTTP
The Protocol Specification defines etag as the object Value of the requested variable (See Chapter 14.19 ).
In other words, etag is a token that can be associated with web resources ). A typical web resource can be a Web page, but it may also be a JSON or XML document. Server ticket
It is solely responsible for determining what a mark is and its meaning, and transmitting it to the client in the HTTP Response Header. The following is the format returned by the server:
Etag: "50b1c1d4f775c61: df3"
The query update format of the client is as follows:
If-None-Match: W/"50b1c1d4f775c61: df3"
If etag does not change, status 304 is returned and no result is returned, which is the same as last-modified. I personally tested etag mainly for resumable download.
How does last-modified and etags help improve performance?
Cong
The developer will use the HTTP header of the last-modified and etags requests together, so that the cache of the client (such as the browser) can be used. Because the server first generates
Last-modified/etag mark. The server can use it later to determine whether the page has been modified. Essentially, the client requests the server to verify the token by sending it back to the server.
User) cache.
The process is as follows:
1. The client requests a page ().
2. The server returns to page A and adds a last-modified/etag to page.
3. The client displays the page and caches the page together with last-modified/etag.
4. The customer requests page a again and passes the last-modified/etag returned by the server in the last request to the server.
5. The server checks the last-modified or etag and determines that the page has not been modified since the last client request. The server returns the response 304 and an empty response body.
The following example describes how to use the server code to operate the Client Cache:
Code:
1 // default cache seconds
2 int secondstime = 100;
3
4 // determine whether the last modification time is within the required time
5 // if the file on the server side has not been modified, the returned status is 304, and the content is blank, which saves the amount of data transmitted. If the files on the server have been modified, the returned results are similar to those in the first request.
6
If (request. headers ["If-modified-since"]! = NULL &&
Timespan. fromticks (datetime. Now. ticks-
Datetime. parse (request. headers ["If-modified-since"]). ticks). Seconds
<Secondstime)
7 {
8 // test code. The following date is not output when the browser returns the 304 status.
9 response. Write (datetime. Now );
10
11 response. statuscode = 304;
12 response. headers. Add ("content-encoding", "gzip ");
13 response. statusdescription = "not modified ";
14}
15 else
16 {
17 // output the current time
18 response. Write (datetime. Now );
19
20 // set the Client Cache status
21 setclientcaching (response, datetime. Now );
22}
23
24 # region setclientcaching ..
25 /// <summary>
26 // set the Client Cache status
27 /// </Summary>
28 /// <Param name = "response"> </param>
29 /// <Param name = "lastmodified"> </param>
30 private void setclientcaching (httpresponse response, datetime lastmodified)
31 {
32 response. cache. setetag (lastmodified. ticks. tostring ());
33 response. cache. setlastmodified (lastmodified );
34 // public to specify that the response can be cached by the client and the shared (proxy) cache.
35 response. cache. setcacheability (httpcacheability. Public );
36 // indicates the maximum absolute time for a document to exist before it is deemed as obsolete.
37 Response. cache. setmaxage (New timespan (7, 0, 0, 0 ));
38 // set the cache expiration time from the absolute time to the adjustable time
39 response. cache. setslidingexpiration (true );
40}
41 # endregion
If your cache is based on files, such as XML or. ashx in HTTP, you can also use the following file-based client cache:
[Copy this code] Code:
1 # region setfilecaching ..
2 /// <summary>
3 // Set client cache based on files
4 /// </Summary>
5 /// <Param name = "FILENAME"> </param>
6 private void setfilecaching (httpresponse response, string filename)
7 {
8 response. addfiledependency (filename );
9 // set the etag HTTP header Based on the timestamp of the handler file dependency.
10 response. cache. setetagfromfiledependencies ();
11 // set the last-modified HTTP header Based on the timestamp of the handler file dependency.
12 response. cache. setlastmodifiedfromfiledependencies ();
13 response. cache. setcacheability (httpcacheability. Public );
14 response. cache. setmaxage (New timespan (7, 0, 0, 0 ));
15 response. cache. setslidingexpiration (true );
16}
17 # endregion
The tool used is httpwatchpro running in IE. In Firefox, you can use firebug + yslow for testing.
Yslow is a tool that runs based on firebug. It can analyze why your web page is cached and give a score and a reason for slowness. this tool is developed by Yahoo's R & D team.
Conclusion
Me
We have already seen how to use the client cache to reduce bandwidth and computing methods. As described above, if you can correctly and reasonably use different caches, they will bring you a lot of benefits. I hope this article is for you now or
In the future, Web-based projects will provide spiritual food and use the last-modified and etag response headers at the underlying level to optimize your projects.