HTTP Caching Optimized web site

Source: Internet
Author: User
Tags time zones

HTTP Caching is used well, which can greatly reduce server load and reduce network bandwidth. It is necessary to have an in-depth understanding of the caching protocol for HTTP.

First look at the request/response process:

HTTP request/Response

1, with last-modified head

The response header on the first request returns the Last-modified content, with the time format such as: Wed, 07:08:07 GMT. is GMT time in 0 time zones, Response.adddateheader ("Last-modified", Date.gettime ()) can be used in servlets; Add a response header.

Last-modified and If-modified-since

Last-modified corresponds to the If-modified-since, the former is the response head, the latter is the request header. The server to handle the If-modified-since request header and last-modified to see if there is an update, if there is no update to return 304 response, otherwise as normal request processing. If you want to use them in dynamic content, you need to process them.

Ps:servlet take if-modified-since can be used long last = Requst.getdateheader ("if-modified-since");

2, with the Etag head

A lot of time may not take time to determine whether content is updated. That can be used with the ETag header, where the ETag calculates an identity with the content. The way of calculation can be decided by oneself, for example can use CRC32, MD5 and so on.

Etag and If-none-match

The Etag corresponds to the If-none-match, which is the response header, which is the request header. The server to determine whether the request content calculation of the etag is consistent with the request header If-none-match, if the consistent means that there is no update, return 304 can be, otherwise as normal request processing. Refer to: Implement Etag filter with Httpservletresponsewrapper

3, with Expires head, expiry time

When the requested content has a Expires header, the browser will not download the content of the request at this time (this behavior is not valid for F5 or CTRL+F2, with Ie7,firefox 3.5 test, valid for example: Enter after the address input).

Expires Expiration Time

Response.adddateheader ("Expires", Date.gettime ()) can be used in servlets; Add outdated content.

PS: You can see the Result as (Cached) state in HttpWatch.

4, with Max-age's Cache-control head

The value of Max-age indicates how many seconds it expires and the browser does not download the requested content until it expires (of course, this behavior is not valid for F5 or CTRL+F2). For example: Server write 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 AddHeader, but a response.addheader ("Cache-control", "Private, Max-age=10 "); Otherwise IE may be invalid for max-age, because it reads only the first Cache-control header.

Summary:

Last-modified with the Etag header (i.e., mode 1 and 2) or to request the server, only 304 headers are returned, and no content is returned. So browse how F5, 304 are valid. But using Ctrl+f5 is a completely new request (this is the browser behavior, not sending cache-related headers).

Expires headers and max-age caches do not require a request server and are taken directly from the local cache. However, F5 ignores caching (so when using HTTP protocol monitoring tools such as HttpWatch, do not F5 mistakenly that Expires and max-age are invalid).

HTTP Protocol monitoring tool:

Firebox:httpfox, Live HTTP header

Ie:httpwatch, Iehttpheader

Important reference article: How to Optimize Your Site with HTTP Caching

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.