Browser cache control and browser cache

Source: Internet
Author: User
Tags http etag website server

Browser cache control and browser cache

Browser cache Control

Last-Modified/If-Modified-Since

When the browser requests a URL for the first time, the server returns the status code 200, which is the resource requested by the client, at the same time, there is a Last-Modified attribute to mark the Last modification time of the file on the server.

The Last-Modified 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 sendsIf-Modified-SinceHeaderTo check whether the file has been modified after the specified time:

If-Modified-Since: Fri, 12 May 2006 18:53:33 GMT

If the resources on the server are Not Changed, the HTTP 304 (Not Changed.) status code is automatically returned and the content is blank, 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.

Last-ModifiedProblems

1. Some files will be edited, but the content has not changed. In this case, you do not want the client to think that the file has been modified and then retrieve the resources again.

2. Some files are frequently Modified. For example, If-Modified-Since cannot be checked for modifications within seconds.

3. Some servers cannot accurately obtain the last file modification time.

 

Expires

Expires is used to control the cache expiration date. When the browser sees an Expires header in the response, it will save it to its cache together with the corresponding component. As long as the component has not expired, the browser will use the cached version.NoHTTPRequest. Long-lastingExpiresThe header is applicable to images and other infrequently updated resources.The date format set for Expires must be GMT (Greenwich Mean Time ).

Expires: Wed, 11 Jan 2017 08:10:26 GMT

ExpiresLimitations:

First, the Expires header uses a specific time and requires strict clock synchronization between the client and the server. If the time of the server and the client is inconsistent, the cache may become invalid in advance and there may be instability. Next, if the date of Expires comes, a new date needs to be provided in the server configuration.

 

Cache-Control

HTTP1.1 introduces the Cache-Control header to overcome the limitations of the Expires header. Cache-Control uses max-age to determine how long the component is cached, in seconds. For example

Cache-Control: max-age = 3600 indicates that the component will be cached for 60 minutes.

If both max-age and Expires appearMax-ageHigher priorityThe browser determines the cache expiration time based on the max-age time.

CommonCache-directiveValue

Cache-directive

Description

Public

All content will be cached (both client and proxy servers can be cached)

Private

The content is only cached in the private cache (only the client can be cached, and the proxy server cannot be cached). The default value is

No-cache

You must confirm with the server whether the returned response has been changed before using the response to meet subsequent requests for the same URL. Therefore, if an appropriate authentication token (ETag) exists, no-cache initiates a round-trip communication to verify the cache response. If the resource is not changed, you can avoid downloading.

No-store

All contents are not cached in the cache or Temporary Internet Files.

Must-revalidation/proxy-revalidation

If the cached content is invalid, the request must be sent to the server/proxy for reverification.

Max-age = xxx (xxx is numeric)

The cached content will expire after xxx seconds. This optionOnlyHTTP 1.1Available. If it is used with Last-Modified, the priority is higher.

 

Different browser operations

Open new window

If the cache-control values are private, no-cache, and must-revalidate, the server will be accessed again when a new window is opened. If the max-age value is specified, the server will not be accessed again during this period, for example: Cache-control: max-age = 5 indicates that the server will not be accessed again within 5 seconds after the webpage is accessed.

Press enter in the address bar

If the value is private or must-revalidate, the server will be accessed only during the first access and will not be accessed in the future. If the value is no-cache, it will be accessed every time. If the value is max-age, the access will not be repeated before expiration.

Back and press the button

If the values are private, must-revalidate, and max-age, no re-access will be performed. If the value is no-cache, the access will be repeated each time.

Press the refresh button

No matter what the value is, it will be accessed again.

 

Etag (Entity Tag)

The server has two methods to check whether cached components match those on the original server:

(1) Compare the Last Modified Date; Last-Modified/If-Modified-since

(1) Compare object tags; Etag/If-None-Match (higher priority than If-Modified-since)

Entity tags are a mechanism used by web servers and browsers to verify the effectiveness of cache components.

ETag: "50b1c1d4f775c61: df3"

First request:

1. The client initiates an http get request for a resource;

2. The server processes the request and returns the resource, including Http Etag and Status Code 200.

The second request:

1. The client initiates an http get request for a file, which contains an If-None-Match header. The content is the Etag value returned by the server during the first request.

2. The server checks whether the received Etag matches the calculated Etag. If yes, status code 304 is returned, and the client continues to use the local cache. If not, the system returns the resource and the new ETag.

ETagProblems

ETag usually uses certain attributes of a component to construct it. These attributes are unique for a specific hosted website server. When the browser obtains the original component from a server and then initiates a conditional GET request to another server, the ETag does not match.

Unnecessary re-loading of components also affects server performance and increases bandwidth overhead. If your RoundRobin Rotation cluster contains n servers, the probability that the Etag in the next user cache can match the server is 1/n. Etag also reduces the Proxy Cache efficiency because the proxy does not match itself and will be re-downloaded.

HTTPResponse Optimization

Remove ETag and Expires and use Cache-Control to Control the local Cache.

Related Article

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.