Home > Backend Development > PHP Tutorial > apache struts2 PHP uses HTTP caching protocol principle analysis and application analysis in Apache environment

apache struts2 PHP uses HTTP caching protocol principle analysis and application analysis in Apache environment

WBOY
Release: 2016-07-29 08:41:50
Original
1129 people have browsed it

There is also Etag for static pages.
1. Let’s look at the first case: apache static page
The static page sent by apache to the client generally contains Last-Modified and Etag. The values ​​of these two tags come from the modification time and inode of the static file.
The following is the header
XML/HTML code that is intercepted and returned from apache to the client

Copy the code The code is as follows:


Last-Modified: Fri, 26 Jan 2007 01:53:34 GMT
ETag: "3f9f640- 318-cb9f8380"


The reason why search engines like static files is because with these two identifiers, they can determine whether the file has been updated
Second, PHP and other dynamic pages
Since PHP is dynamically generated, its content cannot be based on PHP The time of the program is used to determine the last modification date, so by default PHP does not include any cache control when returning to the client. If you want to make good use of cache, you must understand the cache mechanism and manage it to reduce the interaction between b and s, reduce bandwidth traffic, and reduce the burden on the server. ..There are many benefits.
3. The specific meaning of cache control
First explain the meaning of these tags that I have tested and understood
Cache-Control: Specifies the caching mechanism followed by requests and responses. Setting Cache-Control in a request message or response message does not modify the caching process during the processing of another message. The caching instructions during the request include no-cache, no-store, max-age, max-stale, min-fresh, only-if-cached, and the instructions in the response message include public, private, no-cache, no-store, no-transform, must-revalidate, proxy-revalidate, max-age.
The meaning of the instructions in each message is as follows:
Public indicates that the response can be cached by any cache area.
Private indicates that all or part of the response message for a single user cannot be processed by the shared cache. This allows the server to only describe a partial response from a user that is not valid for other users' requests.
no-cache indicates that the request or response message cannot be cached.
no-store is used to prevent important information from being released unintentionally. Sending it in the request message will cause both the request and response messages to use caching.
max-age indicates that the client can receive responses with a lifetime no greater than the specified time in seconds.
min-fresh indicates that the client can receive responses with a response time less than the current time plus the specified time.
max-stale indicates that the client can receive response messages beyond the timeout period. If you specify a value for max-stale messages, the client can receive response messages that exceed the specified value of the timeout period.
php usage:
Use header() before output, (if you use ob_start(), you can place the header anywhere in the program)
PHP code

Copy the code The code is as follows:


header('Cache- Control: max-age=8');


max-age=8 means the maximum lifetime is 8 seconds. If it exceeds 8 seconds, the browser must go to the server to read it again. This time starts from the user's read page. Expires is an absolute time.
Expires: The absolute time for cache expiration. If it passes the specified time point, the browser will not recognize the cache and will go to the server to request the latest one again.
Last-Modified: The last modification time of the document. Its wonderful functions are: 1.
If it is a static file, the client will send the time in its cache, and Apache will compare it. If it finds that there is no modification, it will directly return a header. The status code is 304, and the number of bytes is very small. (The advanced version will also add a comparison Etag to determine whether the file has changed)
2 PHP dynamic file:
The client sends the comparison time, and PHP will determine whether it has been modified. If the modification time is the same , it will only return 1024 bytes. It is unknown why 1024 is returned. If the file generated by your php is very large, it will only return 1024, so it saves bandwidth. The client will automatically respond based on the modification time sent by the server. Displayed from the cache file.
Note: If there is no Last-Modified header, Cache-Control and Expires can also work, but each request must return the real number of file bytes, not 1024
4. HOW?
Don’t worry about static pages. , if you want to better control the caching of static pages, Apache has several modules that can be well controlled. We will not discuss php pages here:
There are two types:
1. Pages that do not change frequently, similar to news releases, this Characteristics of class pages: There will be several changes after the first release, but they will basically not be modified as time goes by. The control strategy should be: 1. Send Last-Modified for the first release, set max-age for 1 day, update Last-Modified after modification, and max-age time will be normal with the number of modifications. This seems more cumbersome, and you have to record the number of modifications. You can also predict the next possible modification time and use Expires to specify the approximate time of expiration.
PHP code

Copy the code The code is as follows:

//header(' Cache-Control: max-age=86400');//Cache for one day
header('Expires: Mon, 29 Jan 2007 08:56:01 GMT');//Specify the expiration time
header('Last-Modified: ' .gmdate('D, d M Y 01:01:01',$time).'GMT');//Greenwich time, $time is the timestamp when the file is added


2 Frequently changed pages
Similar to bbs and forum programs, this kind of page updates relatively quickly. The main function of the cache is to prevent users from frequently refreshing the list, which causes a burden on the server database. It is necessary to ensure the timeliness of updates and the cache performance. Being exploited
Cache-Control is generally used here to control, and the max-age can be flexibly controlled according to the frequency of postings in the forum.
PHP code

Copy code The code is as follows:


header('Cache-Control: max-age=60');//Cache for one minute
header('Last-Modified: '.gmdate(' D, d M Y 01:01:01',$time).'GMT');//Greenwich time, $time is the last updated timestamp of the post


Five extra
1 The difference between refresh, go to and forced refresh
There are refresh and go buttons on the browser. Some browsers support using ctrl+F5 to force refresh the page. What is the difference between them?
Go to: The user clicks on the link to go to it. It completely uses the cache mechanism. If there is Last-Modified, it will not communicate with the server. You can use the packet capture tool to see that the sent byte is 0byte. If the cache expires, then it will execute F5 refresh action.
Refresh (F5): This refresh is also determined based on whether there is Last-Modified in the cache. If there is, it will be transferred to 304 or 1024 (php). If there is no last update time, go to the server to read and return the real document size.
Forced refresh : Completely abandon the caching mechanism, go to the server to read the latest document, and send the header to the server as follows
XML/HTML code

Copy the codeThe code is as follows:


Cache-Control: no-cache


2 Debugging Tools
A better tool to view the interaction between the browser and the server is httpwatch pro. The current version 4.1 supports ie7.
There are other proxy packet capture tools that can analyze, http debugging. I haven't used it before. There is also a tcp packet capture tool, which is the network that comes with 2000. There are also tcp packet capture tools. The network monitor that comes with 2000 is not specifically for http, but it is difficult to use.

The above introduces the principle analysis and application analysis of PHP using HTTP caching protocol in apache struts2 Apache environment, including the content of apache struts2. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template