Posts tagged ‘caching’

Whilst reading some of the PHP RSS feeds, I saw this handy tutorial for some quick and dirty PHP caching. This obviously doesn’t work well for things like GET, POST, etc…but if you are handling categories, or frequently requested data, this may be a great solution.

Here is the full tutorial, but the basic code is:

// TOP of your script
$cachefile = 'cache/'.basename($_SERVER['SCRIPT_URI']);
$cachetime = 120 * 60; // 2 hours
// Serve from the cache if it is younger than $cachetime
if (file_exists($cachefile) && (time() - $cachetime < filemtime($cachefile))) {
include($cachefile);
echo "<!-- Cached ".date('jS F Y H:i', filemtime($cachefile))." -->\n";
exit;
}
ob_start(); // start the output buffer

// Your normal PHP script and HTML content here

// BOTTOM of your script
$fp = fopen($cachefile, 'w'); // open the cache file for writing
fwrite($fp, ob_get_contents()); // save the contents of output buffer to the file
fclose($fp); // close the file
ob_end_flush(); // Send the output to the browser