Under Apache environment PHP USES the principle of HTTP cache protocol for parsing and application analysis

  • 2020-03-31 20:19:09
  • OfStack

There is also an Etag for static pages.

First, look at the first case: apache static pages

Static pages sent by apache to clients typically contain last-modified and Etag tags whose values come from the modification time and inodes of the static file.

The following is the truncated header for the apache return client

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

The reason search engines like static files is that they have these two identifiers to tell if the file has been updated

Ii. Dynamic pages such as PHP

Since PHP is dynamically generated, its contents cannot be determined according to the PHP program's time to determine the last modification date, so the default PHP return client contains any cache control, in order to make good use of the cache must understand the caching mechanism, and reduce b,s interaction, reduce bandwidth traffic, reduce the burden of the server... A lot of good.

Three, the specific meaning of cache control

First, I will explain the meaning of these tags that I have tested to understand

Cache-control: specifies the caching mechanism that the request and response follow. Setting cache-control in a request message or response message does not modify the Cache processing in another message processing. Request cache instructions include no cache, no store, Max - age, Max - stale, min - fresh, only - if - cached, including public, private, the instructions of the response message 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 cache.

Private indicates that the entire or partial response message for a single user cannot be processed by the Shared cache. This allows the server to describe only part of the response message when the user, which is not valid for requests from other users.

No-cache indicates that the request or response message cannot be cached

No-store is used to prevent important information from being inadvertently published. Sending in a request message causes both the request and response messages to be cached.

Max-age indicates that the client can receive a response with a lifetime of no more than the specified time in seconds.

Min-fresh indicates that the client can receive a response with a response time less than the current time plus the specified time.

Max - stale instructs the client can receive beyond timeout period response message. If Max - stale news value is specified, then the client can receive more than within the period specified values of the response message.

PHP usage:

Header (), which you can put anywhere in the program if you use ob_start()

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

Max-age =8 represents a maximum lifetime of 8 seconds, beyond which the browser must go to the server to re-read, which is timed from the user's page read, and Expires is the absolute time.

Expires: the absolute time at which a cache Expires. If it Expires, the browser dismisses the cache and goes to the server to request a new one.

Last-modified: the Last time a document was Modified

If it is a static file, the client will send the time in its cache, apache will compare it, and if it finds no changes, it will directly return a header, the status code is 304, 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:

Client hair than time, PHP will determine whether changes, if you modify the same time, it will only return to 1024 bytes, as to why return 1024 unknown, if your PHP generated file is very large, it also return only 1024, so save the bandwidth, the client will automatically according to the modified time of the server sent from the cache files will be displayed.

Note: cache-control and Expires work without a last-modified header, but return the actual number of bytes per request, not 1024

Four, HOW?

If you want to better control the cache of static pages, apache has several modules that you can control very well, which I won't discuss here

PHP page:

Here are two:

1. Infrequently changed pages are similar to news releases. The characteristics of this type of page: after the first release, there will be several changes, and as time goes by, it will not be modified. The control strategy should be: 1. Send the last-modified after the first release, set the max-age for 1 day, update the last-modified after the modification, and the max-age time is normal with the number of modifications. This seems like a lot of work, and you need to keep track of the number of changes you've made, or you can predict the next possible change time by using Expires

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

Frequently changed pages

Similar BBS, forum program, this kind of page update speed is relatively fast, the main role of the cache is to prevent users from frequently refreshing the list, resulting in the server database burden, not only to ensure the timeliness of the update, but also to ensure that the cache can be used

This is generally controlled by cache-control, which controls max-age flexibly according to the frequency of forum posts.

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

Five additional

1 refresh, go to, force refresh of the difference

There are refresh and go buttons on the browser, some browsers support CTRL +F5 to force the page refresh, what is the difference between them?

Go to: when the user clicks the link, it will go to. It completely USES the caching mechanism. If there is last-modified, it will not communicate with the server.

Refresh (F5) : this refresh also depends on whether the cache has last-modified, if any, it will be transferred to 304 or 1024(PHP), if there is no Last update time, then go to the server to read, return the real document size

Force refresh: completely abandon the caching mechanism, go to the server to read the latest document, and send the following headers to the server

XML/HTML code
 
Cache-Control: no-cache 

2 debugging tools

A good tool for viewing browser and server interactions is httpwatch pro, which now supports ie7 in version 4.1

There are other proxy grab tools that can be analyzed, HTTP debugging. I haven't used it. There's also the TCP packet capture tool, which comes with the 2000 network. There is also the TCP packet capture tool, which comes with the 2000 network monitor but is not particularly difficult to use for HTTP.

Related articles: