Apache STRUTS2 Apache Environment PHP using HTTP Caching Protocol principle analysis and application

Source: Internet
Author: User
For static pages there is an etag.
First of all, look at this: Apache static page
The static page that Apache sends to the client typically contains the last-modified and ETag, the values of which are from the modified time of the static file and the Inode.
Below is the truncated Apache return client header
xml/html Code

Copy the Code code as follows:


Last-modified:fri, Jan 01:53:34 GMT
ETag: "3f9f640-318-cb9f8380"


Search engines like static files because they have these two identities, you can tell if the file has been updated
Second, PHP and other dynamic pages
Because PHP is dynamically generated, its content is not based on the PHP program time to determine the last modification date, so the default PHP back to the client when the fill contains any cache control, to take advantage of the cache must understand the caching mechanism, and the rationale for reducing b,s interaction, reduce bandwidth traffic, reduce the burden on the server ... A lot of benefits.
Third, the specific meaning of the cache control
First explain what I have tested and understand the meaning of these several tags
Cache-control: Specifies the caching mechanism that requests and responses follow. Setting Cache-control in a request message or response message does not modify the caching process in another message processing process. The cache directives for 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 instructions in each message have the following meanings:
Public indicates that the response can be cached by any buffer.
Private indicates that the entire or partial response message for a single user cannot be shared with the cache. This allows the server to simply describe a partial response message for the user, and this response message is not valid for another user's request.
No-cache indicates that a request or response message cannot be cached
No-store is used to prevent the inadvertent release of important information. Sending in the request message will make the request and response messages do not use the cache.
Max-age indicates that the client can receive a response that is not longer than the specified time (in seconds).
Min-fresh indicates that the client can receive a response that is less than the current time plus a specified time.
Max-stale indicates that the client can receive a response message that exceeds the timeout period. If you specify a value for the Max-stale message, the client can receive a response message that exceeds the specified value for the timeout period.
PHP Usage:
Use header () before output, (if using Ob_start () you can place the header anywhere in the program)
PHP code

Copy the Code code as follows:


Header (' cache-control:max-age=8 ');


Max-age=8 represents a maximum lifetime of 8 seconds, more than 8 seconds the browser must go to the server to re-read, this time is the user's reading page started timing, and expires is absolute time.
Expires: The absolute time the cache expires, if it is over the specified point in time, the browser will not recognize the cache, to go to the server to re-request a copy of the latest.
Last-modified: The last modification time of the document, it's magical: 1
If it is a static file, the client will be sent up in its cache time, Apache will be compared, if found not modified directly return a header, the status code is 304, the number of bytes is very small, (advanced version will also increase the comparison etag to determine whether the file changes)
2 php Dynamic files:
The client sends a comparison time, PHP will determine whether to modify, if the modification time is the same, will only return 1024 bytes, as to why return 1024 is not known, if your PHP generated file is very large, it will only return 1024, so the comparison of bandwidth, The client is automatically displayed from the cache file based on the modification time sent by the server side.
Note: If there is no last-modified header, Cache-control and expires can also work, but each request will return the actual number of bytes of file, instead of 1024
Iv. how?
Static pages do not have to worry about it, if you want to better control the cache of static pages, Apache has a few modules can be very good control, not discussed here
PHP Page:
There are two types of:
1, infrequently changed pages, similar to the press release, this type of page features: After the first release there will be several changes, as time passes the basic will not be modified. The control strategy should be: 1 The first release of the Send Last-modified,max-age set 1 days, modified after the update Last-modified,max-age time with the correct number of changes. This seems more cumbersome, but also to record the number of changes, you can also anticipate the next possible modification time with expires specified to approximate time expires
PHP code

Copy the Code code as follows:


Header (' cache-control:max-age=86400 ');//Cache Day
Header (' Expires:mon, Jan 08:56:01 GMT ');//Specify Expiration time
Header (' last-modified: '. Gmdate (' d, D M Y 01:01:01 ', $time). ' GMT, $time is the timestamp at the time the file was added


2 frequently-changed pages
Similar BBS, forum program, this page update speed is relatively fast, the main role of the cache is to prevent users from frequently refreshing the list, causing the server database burden, both to ensure the timeliness of updates, but also to ensure that the cache can be exploited
Here generally with Cache-control to control, according to the forum's post frequency flexible control max-age.
PHP code

Copy the Code code 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, $time is the last update timestamp for the post


Five extra
1 refresh, go to, force refresh the difference
The browser has a refresh and go to the button, some browsers support the use of Ctrl+f5 force to refresh the page, what is the difference between them?
Go: The user clicks the link is to go, it completely uses the cache mechanism, if has the last-modified then does not communicate with the server, uses the grasping the package tool to be able to see to send the byte is 0byte, if the cache expires, then it performs the F5 refreshes the action.
Refresh (F5): This refresh is also based on whether the cache has last-modified to decide, if there will be transferred to 304 or more (PHP), if there is no last update time then go to the server to read, return the real document size
Forced refresh: Completely discard the cache mechanism, go to the server to read the latest documents, sent to the server header as follows
xml/html Code

Copy the Code code as follows:


Cache-control:no-cache


2 Debugging Tools
View browser and server interaction the better tool is HttpWatch Pro, now version 4.1, supports IE7
There are other agents to grab the package tool can be analyzed, HTTP debugging. No, there is the TCP grab Kit, 2000 comes with the network. There is also a TCP grab kit, 2000 of the Network Monitor, but not specifically for HTTP is more difficult to use.

The above describes the Apache struts2 Apache environment PHP using HTTP Caching Protocol principles and application analysis, including the Apache struts2 aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.