Skip to content

Instantly share code, notes, and snippets.

@victorknust
Last active December 18, 2017 15:41
Show Gist options
  • Save victorknust/92e3a6a053911dd7bdfead59e4c88258 to your computer and use it in GitHub Desktop.
Save victorknust/92e3a6a053911dd7bdfead59e4c88258 to your computer and use it in GitHub Desktop.
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