Simple PHP Caching

Awesome stuff.

< ?php $cachefile = 'cache/index-cached.html'; $cachetime = 5 * 60; // Serve from the cache if it is younger than $cachetime if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) { include($cachefile); echo "\n”;
exit;
}
ob_start(); // Start the output buffer

/* The code to dynamically generate the page goes here */

// Cache the output to a file
$fp = fopen($cachefile, ‘w’);
fwrite($fp, ob_get_contents());
fclose($fp);
ob_end_flush(); // Send the output to the browser
?>

via Simon Willison

Leave a Reply