[Overview of Web Cache Mechanism] 3-how to build a cacheable site

Source: Internet
Author: User
ArticleDirectory
    • Server Configuration

=== Index ====

[Overview of Web Cache Mechanism] 1-roles and types of Web Cache

[Overview of Web Cache Mechanism] 2-Web browser cache mechanism

[Overview of Web Cache Mechanism] 3-how to build a cacheable site

[Overview of Web Cache Mechanism] 4-Web Cache Mechanism in the HTML5 Era

[Overview of Web Cache Mechanism] 5-new ideas on Cache Mechanism in the web app age

================

After learning about the importance of the Web cache running mechanism, we can try to improve our site from the following aspects to ensure that the cache is used most effectively and achieve the best performance.

The same resource ensures URL Stability

URL is the basis of the browser cache mechanism, so if a resource needs to be referenced in multiple places, try to ensure that the URL is fixed. At the same time, we recommend that you use public libraries, such as Google Ajax Library, to maximize cache usage.

Add an HTTP cache header to CSS, JS, and image resources, and force the HTML entry not to be cached.

For infrequently modified static resources, such as CSS, JS, and images, you can set a long expiration time or add at least last-modified/etag, it is not recommended to set cache for HTML page entry files. This not only ensures that the static resources remain unchanged, but also avoids repeated downloads without re-sending requests or directly using 304, and ensures that the resources are updated, by adding a timestamp to a resource or changing the path, you can access the latest resource.

Reduce cookie Dependencies

Excessive use of cookies will greatly increase the burden on HTTP requests. Each get or POST request will carry cookies, increasing network transmission traffic and increasing interaction time; at the same time, the cache is very difficult to cache. It should be used as little as possible, or this should be used on dynamic pages.

Reduces the use of HTTPS encryption protocols

Resources requested through HTTPS are not cached by default. resources can be cached only through special configuration. We recommend that you only transmit requests involving sensitive information over HTTPS. Other static resources such as CSS, JS, and images should be avoided.

Use get to request dynamic CGI

Although the POST request method is more secure than get, it can avoid sensitive information such as passwords being transmitted over the network and intercepted by agents or others, but the GET request method is faster and more efficient, and can be cached. We recommend that you try to use the get Method for requests that do not involve sensitive information.

Dynamic CGI can also be cached

If the content entered by a dynamic script or CGI is fixed within a certain period of time, or the input content is the same according to the same get parameter, we also think that the request can be cached, there are several ways to achieve this effect:

    1. Allows dynamic scripts to periodically export content changes to static files. The web directly accesses static files with last-modified/etag.
    2. Developers can useCodeAdd cache-control: Max-age to the response header of the dynamic script to notify the browser that the copy can be used directly before expiration.
    3. Add the last-modified/etag information to the response header of the dynamic script by using code. When the browser requests again, if-modified-since/if-None-match can be parsed to determine whether the browser has a cache, and whether the code logic controls whether to return 304

 

How to add a cache mechanism to a site

The cache header in the HTTP Request/Response Header effectively utilizes the site cache. As a Web Front-end developer, what should I do? The answer is: No need to do anything. However, we need to push web operators and web backend developers to add appropriate cache headers to servers and dynamic script CGI.

Server Configuration

Apache configuration reference:Mod_headers, mod_headers

Write a dynamic script that can be cached

The server configuration method is simple and common, but if you do not have the permission to modify the server configuration or want to add more detailed expires/cache-control/etag information, you may try to add this information at the code level. Different Languages have different implementations, but their ideas are consistent. You can open up an independent module, call the interface provided by the language library to add a header, and set the header information as needed. When a request's dynamic script needs to be cached, the public module can be called by reference modules such as include and require to implement the cache mechanism.

PHPThe implementation code example is as follows:

 
Cache. php

  

  

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.