[Translation] cache tutorials for web developers and web administrators

Source: Internet
Author: User

Original article address:

Caching Tutorial for Web Authors and Webmasters1 what is web cache? Why do people use them? 2 web Cache type 2.1 browser cache 2.2 Proxy Cache 2.3 gateway cache 4 web Cache work 6 build a cache-aware site skills 7 Write cache-aware scripts

 

1. What is web cache? Why do people use them?

The web Cache is located between one or more servers and one or more clients. It monitors requests and reduces the number of responses. For example, pages, images, and files. If you have another request for the same url address, you can use the existing response instead of sending a new request to the original server.

There are two main reasons for using web Cache:

1. reduce latency. Because the request is from the cache, this is closer to the client and gets a response, not from the original server. It takes less time to obtain and present the content, making the web seem faster to respond.

2. Reduce network transmission. Because the response is reused, the bandwidth usage of the client is reduced. If you need to pay for the bandwidth, you can save money and maintain a small demand for bandwidth.

2. web Cache type 2.1 browser cache

If you open the Settings dialog box of modern browsers, you will find a cache item setting. Allows you to use a hard disk to store cached content. Cache works in a similar way. It checks whether the content is fresh and checks every session, that is, a browser call.

This cache is very useful when you browse the pages you have browsed. If you view the same image, the browser will cache it and display it instantly.

2.2 Proxy Cache

The proxy cache works in a similar way, but it is larger. Agents serve hundreds of thousands of users in the same way. Large companies or ISPs usually set them on the firewall, or individual devices.

Because the proxy cache is not a part of the client or the original server, they are located somewhere on the network and the request passes through them. One way to use them is to set up your browser proxy and tell the browser to use those proxies. Another method is to intercept the proxy. The interception proxy is implemented through the bottom layer of the network, and the client does not need to set or even know their existence.

Proxy Cache is a shared cache, which is used by more than one user. It also allows many users to reduce latency and bandwidth. Because common content is used multiple times.

2.3 gateway Cache

Like "reverse proxy cache", the gateway cache is also located in the middle of the client and server. It is not deployed by the ISP to reduce bandwidth, but by the web site administrator, which makes the site easier to expand, it is more reliable and has better performance.

There are many ways to locate the request to the gateway cache, typically using Server Load balancer, the client seems to have accessed the original server.

CDN (Content Delivery Network) can also be used to provide the gateway cache function.

 

This tutorial mainly describes browser cache and proxy cache, some of which are useful to those interested in Gateway cache.

 

4. How does the web cache work?

All caches have some column rules to determine when the cache is used as a performance Layer Service when it is effective. Some rules are set in the Protocol (http1.0 http1.1), and some are set by the Administrator.

Generally, it includes the following rules:

1. If the response header contains no cache settings, no cache is required.

2. If the request is for verification or secure connection (https), it will not be cached.

3. In the following cases, the content in the cache is fresh, which means that it can be sent to the client without accessing the original server)

3.1 contains the expires setting, age-controlling setting, and is still in the fresh-keeping period.

3.2 If the cached content has just been used by the presentation layer, and it has not been modified.

Fresh content is directly obtained from the cache or from the original server.

4. If the presentation layer expires, it will access the original server and verify it, or tell the cache whether the cached copy is available.

5. In the following cases, for example, if the connection is disconnected from the network, the cache will provide the old response content without accessing the original server.

If the response does not contain the ETag or Last-Modified information, and there is no clear information about the freshness, usually, not always, it is considered that no cache is used.

Freshness and verification information are very important for cache work. Freshness information is directly obtained from the cache, and verification information is ensured without being modified, avoid sending complete content.

 

6 skills used to build a cache-aware site

In addition to refreshing information and verifying, the following tips Make your site more cache-friendly.

1. Use a consistent url. This is a golden rule about caching. If you provide the same content for different users or sites on different pages, you should use the same url address. This is the easiest and most effective way to make your site cache friendly. For example, if you use "/index.html" somewhere, please always use this address.

2. Use public image libraries and public elements in different places.

3. For images and pages that are not frequently changed in the Cache, use Cache-Control: max-age and design a large value.

4. Set the appropriate value for max-age or expire for regularly changed content in the cache.

5. If the content of a resource, especially the file to be downloaded, changes, you can rename the resource. In this case, you can set a future expiration time and ensure that the correct version is provided.

6. Do not change the unchanged files. If you do this, all files will have a new Last-Modified date, which will cause cache failure. For example, when you update a site, do not copy and overwrite the entire site, but replace the changed files.

7. Use ssl as little as possible, because encrypted pages are not cached and used only when needed, and images are rarely used on ssl pages.

8. Use REDbot to check your webpage. It can help you apply many concepts in this tutorial.

 

7. Write cache-aware scripts

By default, many scripts do not return the Last-Modified or ETag in the verification response header), or refresh the Expires or Cache-Control information ). At the same time, many scripts are dynamic, that is, each request will return different responses,

Generally, if a script can be reused for the same requests after a period of time, it should be able to be cached. If the content changed by the script depends on the url, it can be cached. If the generated content depends on cookies, verification information, or other external conditions, do not cache it.

1. The best way to make a script cache friendly performance) is to output the script content to a common file when the script changes. In this way, the web service can treat it like other web pages, making your work easier. Remember, the file is written only when the script changes, so that the time in Last-Modified is retained.

2. Another fresh method for caching scripts is to set age-related headers. Although it can be done through expires, the easiest way is to use Cache-Control: max-age, which can save fresh content for a period of time after the request.

3. If you do not follow the above method, you need to generate a verification in the script to respond to the request with If-Modified-Since or If-None-Match. It can be implemented by parsing the http header. If no modification is made, the 304 Not Modified response is returned.

4. If POST is not used at will, the post response will not be cached. If you use get, the cache will store future information.

5. Do not embed special user information in the url, unless the embedded information is unique to the user.

6. Generate Content-Length in the response header. This is easy to do, and it allows scripts to be used for responses in persistent connections. Allow the client to respond multiple times in a TCP/IP connection, instead of establishing a connection for each request. Make your site faster.

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.