How to define the best Cache-control strategy

Source: Internet
Author: User
Tags browser cache

define the best Cache-control policy

Follow the decision tree above to determine the best cache policy for the specific resource or set of resources your app uses. Ideally, your goal should be to cache as many responses as possible on the client, cache as long as possible, and provide a validation token for each response to enable efficient re-validation.

Cache-control Instructions and instructions
max-age=86400 The browser and any intermediate caches can cache the response (if it is a "public" response) for up to 1 days (60 seconds x 60 Minutes x 24 hours).
Private, max-age=600 The client's browser can only cache the response for a maximum of 10 minutes (60 seconds x 10 minutes).
No-store Cached responses are not allowed, and each request must be fully fetched.

According to HTTP Archive, in the top 300,000 sites (according to Alexa rankings), half of all download responses can be cached by the browser, which can massively reduce duplicate web browsing and access. Of course, this does not mean that your particular application has 50% of the resources to cache. Some sites have more than 90% of the resources that can be cached, while other sites may have many private or time-sensitive data that cannot be cached at all.

Discard and update cached responses

    • The local cached response will be used until the resource expires.
    • You can force the client to update the response to the new version by embedding the file content thumbprint in the URL.
    • For best performance, each app needs to define its own cache hierarchy.

All HTTP requests made by the browser are routed first to the browser cache to confirm that a valid response is cached that can be used to satisfy the request. If there is a matching response, the response is read from the cache, which avoids network latency and traffic costs incurred by the transfer.

However, what if you want to update or discard the cached response? For example, suppose you have told visitors to cache a CSS stylesheet for up to 24 hours (max-age=86400), but the designer has just submitted an update that you want all users to be able to use. How do you notify all visitors who have now "obsolete" CSS cache copies to update their caches? You cannot do this without changing the URL of the resource.

After the browser caches the response, the cached version will be used until it expires (as determined by max-age or expires) or until it is removed from the cache for some other reason, such as a user clearing the browser cache. Therefore, when you build a Web page, different users may end up using different versions of the file, the user who just gets the resource will use the new version of the response, and the user who caches the earlier (but still valid) copy will use the old version of the response.

So, how can you get your cake and eat it both: client-side Caching and quick updates? You can force the user to download a new response by changing its URL when the content of the resource changes. Typically, you can do this by embedding the thumbprint or version number of the file in the filename-for example, a style. x234dff. css.

Because you can define a cache policy for each resource, you can define a cache hierarchy so that you can control not only the cache time of each response, but also the speed at which visitors see the new version. To illustrate, let's analyze the above example together:

    • HTML is marked as "No-cache", which means that the browser will always re-validate the document on each request and get the latest version when the content changes. In addition, within HTML tags, you embed fingerprints in the URLs of CSS and JavaScript assets: if the contents of these files change, the HTML of the page changes, and a new copy of the HTML response is downloaded.
    • Allows browsers and intermediate caches (such as CDN) to cache CSS and set the CSS to expire after 1 years. Note that you can safely use the 1 "forward expiration" because you embed the thumbprint of the file in the filename: the URL changes when the CSS is updated.
    • JavaScript is also set to expire after 1 years, but is marked private, perhaps because it contains some user private data that the CDN should not cache.
    • The image cache does not contain a version or a unique thumbprint and is set to expire after 1 days.

You can combine ETag, Cache-control, and unique URLs to achieve a single swoop: longer expiration, control over where the response can be cached, and on-demand updates.

Cache Check List

There is no best caching policy. You need to define and configure the appropriate settings for each resource, as well as the overall cache hierarchy, based on the communication pattern, the type of data provided, and the application-specific data update requirements.

Here are some tips and tricks to keep in mind when you develop your caching strategy:

    • use consistent URLs : If you provide the same content on different URLs, you will get and store the content multiple times. Tip: Be aware that URLs are case sensitive.
    • ensure that the server provides the authentication token (ETAG): With the authentication token, the same bytes are not required when the resources on the server have not changed.
    • determine which resources the intermediate cache can cache : resources that respond exactly to all users are well-suited for caching by CDN and other intermediate caches.
    • determine the best cache cycle for each resource : Different resources may have different update requirements. Review and determine the appropriate max-age for each resource.
    • determine the cache hierarchy that best suits your site : You can control how quickly clients get updates by using a resource URL that contains the content thumbprint and a short or no-cache period for the HTML document.
    • Minimize churn : Some resources are updated more frequently than other resources. If a specific part of a resource, such as a JavaScript function or a CSS style set, is updated frequently, consider providing its code as a separate file. This way, each time an update is obtained, the remainder, such as content library code that changes infrequently, can be obtained from the cache, minimizing the content size of the download.

How to define the best Cache-control strategy

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.