PHP Code:
//check if the cache file already exists
//generate a unique filename based on the REQUEST_URI to
//implement this in a generic fashion
if (file_exists('cache.html')) {
// the output is already cached, so just
// dump the file's contents to the browser
readfile('cache.html');
} else {
// otherwise, the cache doesn't exist yet
// so re-generate the content
// turn on output buffering so your code's output
// gets stored in the buffer instead of sent to the
// browser
ob_start();
// here you can place the existing PHP and HTML
// you want to cache
// now, write the output you captured in the buffer
// into the cache file
$fp = fopen('cache.html', 'w');
fwrite($fp, ob_get_contents());
fclose($fp);
// and output the buffer's contents to the
// browser as well
ob_end_flush();
}
If you have header and footer or some other template system already, then you could have this implemented across the entire site in minutes.
There's probably a WordPress plugin for caching somewhere if that's what you're using... I'm not the type to go look, though. There are a couple pieces of my blog template that are separately cached to files and update when the cache is 5 minutes old (checking filemtime compared to current time)... just quickly coded into the theme.
Bookmarks