HTTP cache concept and examples of forcing browsers to use Cache

Source: Internet
Author: User

For a static html page, the browser puts the page into the cache after the first visit.

When you access this page again, the request header will be attached:

If-Modified-Since: Mon, 17 Sep 2012 08:52:31 GMT data.

Sent to the server. After receiving the message, the server will send a 304 response to the client if it finds that the date has not changed. The client displays its own cache.

If you press CTRL + F5, the request is forcibly refreshed and the request header does not contain If-Modified-Since. Therefore, the server sends a 200 response directly after receiving the request.

The cache used by ASP. NET also has the same effect. For example, the following page code:

<% @ OutputCache Duration = "600" VaryByParam = "None" %>

Default: <% Response. Write (DateTime. Now. ToString ("yyyyMMdd"); %>

If Location = "ServerAndClient" is set here, the second access is a 304 response. The access to this page is the same as the Access to html, and the second access will have a 304 response. Of course, after the page is updated, the system will receive a 200 response. That's because the request header contains data such as If-Modified-Since: Tue, 18 Sep 2012 12:32:14 GMT. Note: There will be a 304 response in Chrome's attempt. However, in firefox, the request header does not have the If-Modified-Since information, so there is no 304, and each request is a 200 response. The reason is unknown .)

If Location = "Server" is set here, the 200 response is returned each time. This is cached in the Server memory, so although every time it is 200, it saves the page generation overhead.
When Location = "Server", the response header is changed to Cache-Control: no-cache. In fact, even if the response header Cache-Control is marked as no-cache, there will still be Cache locally, the cache content will not be sent to the client if it is not verified for the first time, the better name of no-cache is "no validation is not used as The output cache" NOTE: from HTTP The Definitive Guide by David Gourley and Brian Totty)

If Location = "Client" is set here, the 200 response is returned each time. This is the question. Although the client has a cache and the request header also has If-Modified-Since, the server data has not changed, but 200 is returned. We speculate that for dynamic pages, if there is no special processing, the status is always 200.

For example, if the following page is a php page or aspx page, even if the page content does not have any dynamic code.

For example:

 
 
  1. <Html xmlns = "http://www.w3.org/1999/xhtml">
  2. <Head>
  3. <Title> </title>
  4. </Head>
  5. <Body>
  6. <Form id = "form1">
  7. <Div>
  8. Do not modify
  9. </Div>
  10. </Form>
  11. </Body>
  12. </Html>
  13.  

Each request always gets a 200 response and never uses the cache.

The following shows how the server uses the 304 status to trick the client into calling its own cache to relieve network pressure.

First, describe the following tested rules:

Response Header last-modified. After it is cached to the client, that is, when the client refreshes, this value will become the value of the request header of the client.

For example, Response. addHeader ("last-modified", new DateTime (, 1), then when you refresh or resubmit this page, Request. the Headers ["If-Modified-Since"] value is 2001-01-01. Therefore, If-Modified-Since can be understood as the last update time of the cache.

Assume that the following function is implemented. If the client requests a page, the client sends a 304 status code again within 10 seconds, requesting the client to read the cache. If the client CTRL + F5 cannot read the cache)

The ASPX code is implemented as follows:

 
 
  1. <% @ Page Title = "Homepage" Language = "C #" AutoEventWireup = "true" CodeBehind = "Default. aspx. cs"
  2. Inherits = "WebApplication1. _ Default" %>
  3. <%
  4. Response. write ("If-Modified-Since:" + Request. headers ["If-Modified-Since"] + "<br/>"); // print the value of If-Modified-Since in the Read Request Header
  5. Response. Write ("Now:" + DateTime. Now. ToString () + "<br/>"); // print the current time
  6. DateTime If_Modified_Since = new DateTime ();
  7. If (Request. Headers ["If-Modified-Since"] = null | Request. Headers ["If-Modified-Since"] = "")
  8. {
  9. If_Modified_Since = new DateTime (1900, 1, 1); // If the read request header contains no value If-Modified-Since, the default value is 19000101.
  10. }
  11. Else
  12. {
  13. If_Modified_Since = DateTime. Parse (Request. Headers ["If-Modified-Since"]);
  14. }
  15. If (If_Modified_Since.AddSeconds (10)> DateTime. Now) // if the last modification time of the cache is 10 seconds different from the current time
  16. {
  17. Response. StatusCode = 304; // return code 304 to read the cache.
  18. Response. End ();
  19. }
  20. Else // otherwise
  21. {
  22. // Display content
  23. Response. AddHeader ("last-modified", DateTime. Now. ToString ());
  24. Response. Write ("Content Changed;" + DateTime. Now. ToString () + "<br> ");
  25. }
  26. %>

The PHP code is as follows:

 
 
  1. <? Php
  2. Date_default_timezone_set ('prc'); // time of the People's Republic of China
  3. $ Arr = getallheaders ();
  4. $ If_Modified_Since = new DateTime ();
  5. If (isset ($ arr ["If-Modified-Since"]) = false)
  6. {
  7. $ If_Modified_Since = new DateTime ("1900-1-1"); // If the read request header contains no value If-Modified-Since, the default value is 19000101.
  8. }
  9. Else
  10. {// Echo "##< br> ";
  11. // Echo "If-Modified-Since:". $ arr ["If-Modified-Since"]. "<br> ";
  12. // Echo "##< br> ";
  13. $ If_Modified_Since = new DateTime ($ arr ["If-Modified-Since"]);
  14. }
  15. $ Now = new DateTime ();
  16. Echo "##< br> ";
  17. Echo $ now-> format ('Y-m-d H: I: s '). "---". $ If_Modified_Since-> format ('Y-m-d H: I: s '). "<br> ";
  18. Echo "##< br> ";
  19. $ Diff = strtotime ($ now-> format ('Y-m-d H: I: s ')) -strtotime ($ If_Modified_Since-> format ('Y-m-d H: I: s '));
  20. Echo "diff:". "$ diff <br> ";
  21. If ($ diff <10) // if the last modification time of the cache is 10 seconds different from the current time
  22. {
  23. Header ('etag: ', true, 304); // return code 304 to read the cache.
  24. Exit ();
  25. }
  26. Else // otherwise
  27. {
  28. $ Now = new DateTime ();
  29. // Display content
  30. Header ("last-modified:". $ now-> format ('Y-m-d H: I: s '));
  31. Print ("Content Changed:". $ now-> format ('Y-m-d H: I: s'). "<br> ");
  32. }
  33. ?>
  34.  

Take the PHP page as an example:

At the first access, the response header is 200, and the request header does not contain If_Modified_Since.

650) this. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "title =" clipboard "border =" 0 "alt =" clipboard "height =" 406 "src =" http://www.bkjia.com/uploads/allimg/131228/1245014415-0.png "/>

Access again within 10 s:

650) this. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "title =" clipboard [1] "border =" 0 "alt =" clipboard [1] "height =" 463 "src =" http://www.bkjia.com/uploads/allimg/131228/1245015X3-1.png "/>

The value of the If_Modified_Since header sent is 16:16:30. The server sent 304 messages within 10 seconds, forcing the browser to call the cache.

This article is from the "one blog" blog, please be sure to keep this source http://cnn237111.blog.51cto.com/2359144/997633

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.