Often PHP is used to generate pages that do not change all that often. In such cases, it is a waste of CPU time to regenerate the web page for every user.
One solution is to not have your PHP pages accessed directly but to have a separate script that uses your PHP to generate the output and save that to a file. Then have the web server reference that file. This works, but adds an extra step that you must remember to do.
In this situation, instead, there is a dynamic solution. Some code that you can include at the top of every PHP file ensures that the page only gets generated once per hour for each script. The function goes one step further and understands that different GET and POST parameters generate different pages and will therefore generate separate cached copies for each different combination of GET and POST parameters that occur.
// A function to call that will set up & handle the output caching:
// The one optional parameter is to specify how many seconds must pass
// before the page is recreated (defaults to 1 hour)
function cache_page($refresh = 3600) {
// First of all generate the filename for this cache file. Base it upon
// the filename plus GET and POST variables, and use sha1() to hash it
// to a 40 character string so we don't run over filename length limits:
$hash = sha1($_SERVER['PHP_SELF'] . '|G|' . serialize($_GET) .
'|P|' . serialize($_POST));
// Create the actual filename of where to find this. It will be in a
// subdirectory called 'cache' below the location of this library.
$file = dirname(__FILE__) . '/cache/' . $hash;
// Now that we've done all that, see if the last modified time is
// less than the current time plus the refresh time
if ((time() - @filemtime($file)) < $refresh) {
// We are done, just return the file and exit
readfile($file);
exit();
} else {
// Otherwise, we have to actually create the page and save it.
// To do this, first stop the user from aborting at this point so
// it will definitely complete.
ignore_user_abort();
// Now set up a shutdown function, so that we can ensure we actually
// finish up properly when the script exits (for any reason)
register_shutdown_function('_cache_page_exit', $file);
// Now, to finish, we need to start output buffering
ob_start();
}
}
// Create the shutdown function that will be called for us when the script
// exits, allowing us to clean up everything.
function _cache_page_exit($file) {
// We were buffering and are done. First step is to get the contents
// and display it to the user.
$output = ob_get_flush();
// Flush all buffers so the user can get the data while we finish up.
flush();
// Save this data into the cache file, blowing away current contents
// if they existed. Request an exclusive lock for the writing just
// so that two copies of this don't interfere with each other.
file_put_contents($file, $output, LOCK_EX);
}
?>
This script can be customized to meet your own needs, such as allowing for cookies or even sessions to be considered in whether a page is different.
- 907 reads













Post new comment