Analysis of the Principle of Using Client Cache for website optimization page 1/2

Source: Internet
Author: User

Many people first want to optimize the program from the server cache aspect. Many different server cache methods have their own characteristics, such as some projects I have participated in, based on the cache hit rate, the server cache and HTTP Compression technologies of Com +/Enterprise Libiary Caching/Windows Services, static files, and other methods have been used, but the client cache is often ignored, even if the server cache allows you to access the page very quickly, but she still needs to rely on the browser to download and output, and when you add the client cache, it will bring you many benefits. because she can cache the most frequently accessed pages on the site to fully improve the Web server's throughput (usually calculated based on the number of requests per second) to improve application performance and scalability.
According to an online shopping survey, most people are willing to queue up in the store, but they are not willing to wait for online shopping. According to the Websense survey, as many as 70% of netusers say they do not want to read pages for more than 10 seconds. More than 70% of users will cancel the current order because of the slow speed.


Basic knowledge
1) What is "Last-Modified "?

When the browser requests a URL for the first time, the server returns 200, and the content is the resource you requested, at the same time, there is a Last-Modified attribute to mark the Last modification time of this file on the service end. The 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 will send the If-Modified-Since header to the server, asking If the file has been Modified after the 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.

2) What is "Etag "?

The HTTP Protocol Specification defines ETag as the object Value of the requested variable (See Chapter 14.19 ). In other words, ETag is a token that can be associated with Web resources ). A typical Web resource can be a Web page, but it may also be a JSON or XML document. The server is solely responsible for determining what the mark is and its meaning, and transmitting it to the client in the HTTP Response Header. The following is the format returned by the server:

ETag: "50b1c1d4f775c61: df3"

The query update format of the client is as follows:

If-None-Match: W/"50b1c1d4f775c61: df3"

If ETag does not change, status 304 is returned and no result is returned, which is the same as Last-Modified. I personally tested Etag mainly for resumable download.


How does Last-Modified and Etags help improve performance?
Smart developers will use the http header of the Last-Modified and ETags requests together, so that the cache of the client (such as the browser) can be used. Because the server first generates the Last-Modified/Etag mark, the server can use it later to determine whether the page has been Modified. Essentially, the client sends this mark back to the server and requires the server to verify its (client) cache.
The process is as follows:
1. The client requests A page ().
2. The server returns to page A and adds A Last-Modified/ETag to page.
3. The client displays the page and caches the page together with Last-Modified/ETag.
4. The customer requests page A again and passes the Last-Modified/ETag returned by the server in the Last request to the server.
5. The server checks the Last-Modified or ETag and determines that the page has not been Modified since the Last client request. The server returns the response 304 and an empty response body.

The following example describes how to use the server code to operate the Client Cache:

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.