HTTP caching can greatly reduce server load and network bandwidth. It is necessary to have a deep understanding of the HTTP caching protocol.
Let's take a look at the request/response process:
HTTP Request/Response
1. Use the last-modified Header
In the response header of the first request, the last-modified content is returned. The time format is wed, 22 Jul 2009 07:08:07 GMT. Is the GMT time of zero time zone. You can use response. adddateheader ("last-modified", date. gettime (); In the servlet to add the response header.
Last-modified and if-modified-since
Last-modified corresponds to if-modified-since. The former is the response header and the latter is the request header. The server needs to compare the IF-modified-since request header with last-modified to check whether there is any update. If there is no update, the server returns the 304 response; otherwise, the server processes the request normally. If you want to use them in dynamic contentProgram.
PS: servlet can use long last = requst. getdateheader ("If-modified-since") to retrieve if-modified-since ");
2. Use the etag Header
Many times may not be used to determine whether the content is updated. You can use the etag header, which is an identifier for content calculation. The calculation method can be determined by yourself, such as CRC32 and MD5.
Etag and if-None-match
Etag corresponds to if-None-match. The former is the response header and the latter is the request header. The server must determine whether the etag calculated in the request content is consistent with the IF-None-match in the request header. If the etag is consistent, it indicates that no update is available. If the etag is consistent, 304 is returned. Otherwise, the request is processed normally. See: Using httpservletresponsewrapper to implement etag filters.
3. Use the Expires header and the expiration time
When the requested content has an Expires header, the browser will not download the requested content during this time (this behavior is invalid for F5 or Ctrl + F2, IE7 is used, firefox 3.5 has been tried. valid example: Enter the address and press Enter ).
Expires expiration time
In servlet, you can use response. adddateheader ("expires", date. gettime (); to add expired content.
PS: In httpwatch, the result is in the (cached) state.
4. Use the max-age cache-control header
The value of max-age indicates the number of seconds after which the request expires. Before the expiration, the browser will not download the requested content (of course, this behavior is invalid for F5 or Ctrl + F2 ). For example, the server writes the max-age response: Response. addheader ("cache-control", "Max-age = 10 ");
PS: If you want to add some cache-control content, such as private, it is best not to write two addheaders, but a response. addheader ("cache-control", "Private, Max-age = 10"); otherwise, ie may be invalid for Max-age because it only reads the first cache-control header.
Summary:
The last-modified and etag headers (that is, the methods 1 and 2) still need to request the server, but only the 304 headers are returned and NO content is returned. So how to browse F5, 304 is valid. However, CTRL + F5 is the new request (this is browser behavior and does not send cache-related headers ).
The Expires header and max-age cache do not need to be requested from the server and are directly retrieved from the local cache. However, F5 ignores the cache (so when using HTTP Protocol monitoring tools such as httpwatch, do not think that expires and max-age are invalid ).
HTTP Protocol monitoring tools:
Firebox: httpfox, live HTTP Header
IE: httpwatch, iehttpheader