Last active
December 18, 2017 15:41
-
-
Save victorknust/92e3a6a053911dd7bdfead59e4c88258 to your computer and use it in GitHub Desktop.
Cache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include('top-cache.php'); | |
// Your regular PHP code goes here | |
include('bottom-cache.php'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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