Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
// define the path and name of cached file
$cachefile = 'cached-files/'.date('M-d-Y').'.php';
// define how long we want to keep the file in seconds. I set mine to 5 hours.
$cachetime = 18000;
// Check if the cached file is still fresh. If it is, serve it up and exit.
if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) {
// if there is either no file OR the file to too old, render the page and capture the HTML.
output all your html here.
// We're done! Save the cached content to a file
$fp = fopen($cachefile, 'w');
fwrite($fp, ob_get_contents());
// finally send browser output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment