Cache of Http static Resources

Source: Internet
Author: User

Recently, I have been researching on page caching and compression. Because the company server uses iis6.0, many performance optimizations are not supported. Therefore, I began to try to write a simple handler myself.

To reduce the server bandwidth requirements, we need to reduce the amount of data between the client and the server. The solution is as follows:
1. cache static resources with few changes, such as JS, CSS, and images.
2. Compress network interaction data, such as JS, CSS, HTML, and images.
3. Reduce the interactions between the client and the server. We can merge all JS files and all CSS files.
The cache is located between the server and the client. A copy of the output content, such as html pages, images, and files, is saved according to the request. When the next request arrives, if the URL is the same, directly use a copy to respond to an access request without sending a request to the source server.

The following provides some knowledge about Http request headers:

Cache type
1. browser cache
The browser has cache settings. Store copies of websites that have been viewed on the hard disk of your computer.
2. Proxy Server Cache
The Web Proxy server uses the same cache principle. The proxy server group uses the same mechanism for hundreds of users. The proxy server cache is shared cache, which is used by a large number of users, therefore, it is effective to reduce the corresponding time and bandwidth usage, and the same copy will be reused multiple times.
3. Gateway Cache
It is also called reverse proxy cache or indirect proxy cache. Gateway cache is also an intermediate server. Unlike the deployment of cache by the Intranet administrator, Gateway cache is generally deployed by the website administrator, it makes it easier for their websites to expand and achieve better performance. Requests can be routed to the gateway Cache Server in several ways. Typically, one or more Server Load balancer servers are used to make the client look like the source server.

How the cache works
1. If the response header information is returned, it indicates that the cache is not retained and the cache does not cache the corresponding content.
2. If the request information requires authentication or security encryption, the corresponding content will not be cached by default.
3. If there is no validator (ETag or Last-Modified header information) in the response, the Cache Server considers that there is no direct update information and the content will be considered non-cached.
4. If a cached copy contains the following information, the content will be considered new enough.

  • Contains the complete expiration time and life control header information, and the content is still in the fresh-keeping period.
  • The browser has used cached copies and checked the freshness of the content in a session.
  • The cache proxy server has recently used cached copies, and the last update time of the content is before the last use period.

5. If the cached copy is too old, the cache server sends a request verification request to the source server to determine whether the current copy can be used to continue the service.

Four header labels commonly used to control Client Cache
1. Last-Modified Last modification time
This header is a response header, indicating the last modification time of the resources requested by the client (usually the browser) on the server. Generally, after the client accepts this header, A 'if-Modified-Since 'header will be appended to future requests for this resource. This header is used to tell the server the last time the client requested the resource was Modified, for some static file resources such as images, css, and js, the configured apache server will understand these If-Modified-Since Request Header labels, compare the time in the header and the last modification time of the file and make a response, if the two are equal, a 304 NotModfied message is sent to inform the client that the requested resources are not modified, so that the client can safely use the resources in the cache, otherwise, a new resource and a new header of Last-Modified will be resending.
2. ETag (Entity Tag) Entity Tag
Similar to Last-Modified, the ETag format of apache1.3 and 2.0 is inode-size-timestamp, therefore, when a resource is Modified, its ETag also changes. ETag is more precise than Last-Modified, and Last-Modified can only be accurate to s, but ETag may cause confusion on multiple servers.
3. Expires expiration time
This attribute tells the cache about how long the replica is fresh. After this time, the cache will send a request to the source server to check whether the document has been modified. Almost all cache servers support the Expires attribute. You can design an absolute interval: based on the time when the customer last views the copy (the last access time) or based on the time when the document was last modified on the server. Expires header information is particularly useful for setting static image cache. These images are rarely modified. You can set a particularly long expiration time for them, which will make the website very fast for users. 4. Cache-Control Cache Control
Let the website publisher control their content and locate the expiration time limit. For example, Cache-Control: max-age = 3600, public, the options are as follows:

    Max-age = [second] the Maximum Cache execution time. [second] is a number in seconds: the number of seconds from the request time to the expiration time.
  • S-maxage = [second] is similar to the max-age attribute, except that it is applied to the shared (such as the proxy server) cache.
  • Public-marked authentication content can also be cached. Generally, content that can be accessed only after HTTP Authentication cannot be cached automatically;
  • No-cache forces each request to be sent directly to the source server without the local cache version verification. This is useful for applications that require validation (can be used together with the public), or applications that require strict use of the latest data (do not hesitate to sacrifice all the benefits of using the cache );
  • No-store forces the cache to keep no copies under any circumstances
  • Must-revalidate indicates that the cache must follow the freshness of all the copies you give. HTTP allows the cache to return expired data in certain conditions. This attribute is specified and the cache is used, we hope to strictly follow your rules.
  • Proxy-revalidate is similar to must-revalidate, except that it only works for the cache proxy Server

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.