Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Cache
<?php
// Cache the contents to a file
$cached = fopen($cachefile, 'w');
fwrite($cached, ob_get_contents());
fclose($cached);
ob_end_flush(); // Send the output to the browser
<?php
include('top-cache.php');
// Your regular PHP code goes here
include('bottom-cache.php');
<?php
$file = $parsed_url['path'];
$cachefile = 'cached-'.$file.'.html';
$cachetime = 18000;
// Serve from the cache if it is younger than $cachetime
if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) {
echo "<!-- Cached copy, generated ".date('H:i', filemtime($cachefile))." -->\n";
include($cachefile);
exit;
}
ob_start(); // Start the output buffer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment