[Go] HTTP protocol (caching mechanism cache)

Source: Internet
Author: User
Tags ranges browser cache

Transferred from: http://www.cnblogs.com/Jessy/p/3568935.html

Caching of HTTP

As for the entity that responds to the message, it is similar to the entity content of the request message, where only the user-agent header is borrowed

The contents of the User-agent header domain contain the user information that made the request.

Cache-control header domain (Request and Reply Universal header domain) Cache-control Specifies the caching mechanism that requests and responses follow. Setting Cache-control in a request message or response message does not modify the caching process in another message processing process. The cache directives for the request include No-cache, No-store, Max-age, Max-stale, Min-fresh, only-if-cached, and the instructions in the response message include public, private, No-cache, No-store, No-transform, Must-revalidate, Proxy-revalidate, Max-age. The instructions in each message have the following meanings: Public indicates that the response can be cached by any buffer. Private indicates that the entire or partial response message for a single user cannot be shared with the cache. This allows the server to simply describe a partial response message for the current user, which is not valid for another user's request. No-cache indicates that a request or response message cannot be cached no-store is used to prevent important information from being inadvertently published. Sending in the request message will make the request and response messages do not use the cache.

If-modified-since: <date> max-age indicates that the client can receive a response that is not longer than the specified time (in seconds). Min-fresh indicates that the client can receive a response that is less than the current time plus a specified time. Max-stale indicates that the client can receive a response message that exceeds the timeout period. If the value of the Max-stale message is specified, then the client can receive a response message that is within the specified value of the timeout period

Cache-control, expires, last-modified, etc. important head domain Cache-control: Specifies the caching mechanism that requests and responses follow.

Setting Cache-control in a request message or response message does not modify the caching process in another message processing process. The cache directives for the request include No-cache, No-store, Max-age, Max-stale, Min-fresh, only-if-cached, and the instructions in the response message include public, private, No-cache, No-store, No-transform, Must-revalidate, Proxy-revalidate, Max-age.

Expires: Tells the browser to indicate when the document should be considered out of date and thus no longer caches it. Code implementation: Header ("Expires:".) Date (' d, D M Y h:i:s \g\m\t ', Time () +10));--------This is to turn the timing into the green time zone string to the Expires header field, which will be 8 hours less than Beijing time in China, and the East 8 zone: Header (" Expires: ". Date (' R ', Time () +10)) Last-modified: This is the server returned to the browser, the browser next request to assign the value to the If-modified-since header domain to the server, the server can be judged by this value whether there is a change, Yes, continue running, no return 304 not modified

Refer: Programming books \ Network Knowledge \http protocol (cache caches). ppt

Programming books \ Network Knowledge \http Protocol Foundation (contains cache). ppt

In HTTP, last-modified and if-modified-since are both HTTP header information used to record the last modification time of the page, note that in this last-modified is the HTTP header sent by the server to the client, and the other If-modified-since is the header that is sent by the client to the server, and you can see that the client sends the previous server side through the If-modified-since header when it requests the locally existing cache page Last-modified The last modification timestamp sent back, this is to let the server side to verify, through this timestamp to determine whether the client's page is up-to-date, if not up-to-date, then return the new content, if it is up to date, then return 304 to tell the client that its local cache page is up-to-date, So the client can load the page directly from the local, so that the data transmitted on the network will be greatly reduced, but also reduce the burden on the server. And in some AJAX applications, the required data is always up-to-date, rather than reading the data in the cache, making such a setup is necessary.
Inadvertent testing revealed that Nginx and Apache have different algorithms for this:
Apache:
(1) Send request directly, return 200,last-modified:mon, APR 13:22:17 GMT
[Email protected] ~]# curl-i http://www.pengyao.org/test.html
http/1.1 date:mon, Apr 14:59:09 GMT server:apache/1.3.41 (Unix) Last-modified:mon, Apr 2010 13:22:17 GMT ETag: "92c027-897-4bd59389″accept-ranges:bytes content-length:2199 content-type:text/plain (2) Specifies that if-modified-since send a GET request with the same last-modified time, returning 304
[Email protected] ~]# curl-i-g-h "If-modified-since:mon, APR 13:22:17 GMT" http://www.pengyao.org/test.html
http/1.1 304 Not Modified Date:mon, APR 15:02:06 GMT server:apache/1.3.41 (Unix) ETag: "92c027-897-4bd59389″
(3) After the If-modified-since 1 hours, send the GET request again, return to 304 [[email protected] ~]# curl-i-g-h "If-modified-since:mon, April 2010 14 : 22:17 GMT "http://www.pengyao.org/test.html
http/1.1 304 Not Modified Date:mon, APR 15:05:02 GMT server:apache/1.3.41 (Unix) ETag: "92c027-897-4bd59389″
The description of Apache in determining whether the browser cache expires, based on the beginning of the if-modified-since, the file last-modified whether modified to judge, and RFC1945 in the if-modified-since description match.
Then look at Nginx's test results for this:
(1) Send request directly, return 200,last-modified:wed, APR 13:14:21 GMT [[email protected] pengyao.org]# curl-i http://www.pengyao.o rg/index.html http/1.1 OK server:nginx/0.7.61 date:mon, April Apr 15:18:29 GMT content-type:text/html; Charset=utf-8 content-length:323 last-modified:wed, Apr 13:14:21 GMT connection:keep-alive accept-ranges:bytes
(2) Specify the same as last-modified time if-modified-since send GET request, return 304
[Email protected] pengyao.org]# curl-i-g-h "if-modified-since:wed, APR 13:14:21 GMT" http://www.pengyao.org/i ndex.html http/1.1 304 Not Modified server:nginx/0.7.61 date:mon, Apr 15:20:45 GMT last-modified:wed, Apr 20 Ten 13:14:21 GMT connection:keep-alive
(3) After the If-modified-since 1 hours, send a GET request again, found that the result returned is 200, different from Apache
[Email protected] pengyao.org]# curl-i-g-h "if-modified-since:wed, APR 14:14:21 GMT" http://www.pengyao.org/i ndex.html http/1.1 OK server:nginx/0.7.61 date:mon, April Apr 15:21:19 GMT content-type:text/html; Charset=utf-8 content-length:323 last-modified:wed, Apr 13:14:21 GMT connection:keep-alive accept-ranges:bytes It can be judged that nginx in determining whether the browser cache expires, determine whether If-modified-since and last-modified match, if not match, the cache expires, return 200 re-download.
To say more like which algorithm, the individual is more inclined to nginx this, because before the file update, I am accustomed to the need to modify the file backup (retention timestamp), once the test problems, timely rollback, the use of Nginx algorithm can ensure that the browser cache failure before and after the rollback, While the Apache strategy caused the rollback to the previous timestamp (last-modified), local because of the test, the local cache file if-modified-since time is generally newer than the last-modified after the rollback, This allows the local cache to still be valid, returning 304, while the actual file is not up-to-date. Of course, the Apache design is fully compliant with the RFC, and personal preferences do not represent real needs.

How do I optimize my site with client-side caching? If-modified-since & If-none-match
How do you choose to optimize your website if you have a lot of concurrent access and are unable to withstand stress? Many people first want to start to optimize the program from the server cache, many different server caches have their own characteristics, such as I have been involved in some projects, according to the cache hit rate of different use of com+/enterprise libiary caching/ Windows services, static files and other ways of server-side caching and HTTP compression technology, but the client cache is often ignored, even if the server's cache to make your page access is very fast, but she still need to rely on the browser download and output, and when you join the client cache,         will bring you a lot of benefits. Because she can cache the most frequently accessed pages in the site to maximize the throughput of the WEB server (typically in the number of requests per second) to improve application performance and scalability. An online shopping survey shows that most people are willing to queue up in stores but are reluctant to wait while shopping online. Websense survey said up to 70% of internet users said they were reluctant to read pages for more than 10 seconds. More than 70% of people 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-side return status is 200, the content is the resource you requested, and there is a last-modified attribute that marks the last time the file was modified at the end of the service period, similar in format:
Last-modified:fri, 2006 18:53:33 GMT
When the client requests this URL for the second time, according to the HTTP protocol, the browser transmits the If-modified-since header to the server, asking if the file has been modified after that time:
If-modified-since:fri, 2006 18:53:33 GMT
If the server-side resource does not change, the HTTP 304 (not Changed.) Status code is returned automatically, and the content is empty, which saves the amount of data transferred. When the server-side code changes or restarts the server, the resource is re-emitted, similar to when the first request is returned. This ensures that the resources are not duplicated to the client, and that the client is able to get the latest resources when the server changes.
2) What is an "Etag"?
The HTTP protocol specification defines the etag as the entity value of the requested variable (see-section 14.19). Another argument is that the ETag is a token that can be associated with a Web resource. A typical web resource can be a Web page, but it may also be a JSON or XML document. The server is solely responsible for judging what the token is and what it means, and transmitting it to the client in the HTTP response header, which is the format returned by the server side:
ETag: "50b1c1d4f775c61:df3"
The client's query update format is this:
if-none-match:w/"50b1c1d4f775c61:df3"
If the etag does not change, it returns the status 304 and does not return, which is the same as last-modified. I test the etag is mainly in the breakpoint download is more useful.
How do last-modified and etags help improve performance? Smart developers will use the HTTP header for Last-modified and ETAGS requests, which can take advantage of caching by clients such as browsers. Because the server first generates the LAST-MODIFIED/ETAG tag, the server can later use it to determine if the page has been modified.         Essentially, the client requires the server to validate its (client) cache by passing the token back to the server.                 The process is as follows: 1. The client requests a page (a).                 2. The server returns page A, and adds a last-modified/etag to a.                 3. The client presents the page and caches the page along with Last-modified/etag.                 4. The Customer requests page A again and passes the Last-modified/etag that the server returned on 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, directly returning response 304 and an empty response body.
The example code below describes how to use server-side code to manipulate client caches:
         Code 1//Default cache number of seconds 2 int secondstime = 100; 3 4//Determine if the last modification time is within the required time 5//If the server-side file has not been modified, then the return status is 304, the content is empty, which saves the amount of data transferred. If the server-side file has been modified, it is similar to when the first request is returned. 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, found here, when the browser returns 304 status, the following date does not output 9      Response.Write (DateTime.Now); Ten 11     response. StatusCode = 304; 12     Response. Headers.add ("content-encoding", "gzip"); 13     Response. Statusdescription = "Not Modified"; + Else (17    //output Current time 18     Response.Write (datetime.now); 20&nbsp ;   //Set client cache status 21     setclientcaching (response, DateTime.Now); /**////<summary> 25//Set client cache status//</summary>//<param name= "response" ></param>//<param name= "LastModified" ></param> 29 private void setclientcaching (HttpResponse response, DateTime lastmodified), {31     response. Cache.setetag (LastModified.Ticks.ToString ()); 32     Response. Cache.setlastmodified (lastmodified); 33    //public to specify that the response can be cached by the client and the shared (proxy) cache. 34     Response. Cache.setcacheability (Httpcacheability.public); 35    //is the maximum absolute time that a document is allowed to exist before it is considered stale. 36     Response. Cache.setmaxage (New TimeSpan (7, 0, 0, 0)); 37    //Set cache expiration from absolute time to adjustable time 38     response. Cache.setslidingexpiration (TRUE); 39}
If your cache is file-based, such as the. ASHX processing in XML or HTTP, you can also use the following file-based client-side caching:
Setfilecaching 1/**////<summary> 2///Set client-side caching based on file mode 3///</summary> 4///<param name= "FileName" >< /param> 5private void setfilecaching (HttpResponse response, String fileName) 6{7 response. AddFileDependency (FileName); 8//The ETag HTTP header is set based on the timestamp of the handler file dependency. 9 Response. Cache.setetagfromfiledependencies (); 10//Last-modified HTTP header is set based on the timestamp of the handler file dependency. One by one response. Cache.setlastmodifiedfromfiledependencies (); Response. Cache.setcacheability (Httpcacheability.public); Response. Cache.setmaxage (New TimeSpan (7, 0, 0, 0)); Response. Cache.setslidingexpiration (TRUE); 15} 16
Conclusion we have seen how to use client-side caching to reduce bandwidth and compute, as mentioned earlier, if you can use a variety of different caches properly and reasonably, they will give you a lot of benefits. I hope this article has provided you with a spiritual food for your current or future Web-based project, and is using last-correctly at the bottom. Modified and ETag response headers to optimize your project.

[Go] HTTP protocol (caching mechanism cache)

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.