Created
June 16, 2012 15:51
-
-
Save MitMaro/2941752 to your computer and use it in GitHub Desktop.
PHP Cache Example
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 | |
define("CACHE_FILE_NAME", "cache.html"); | |
define("CACHE_TIME", 1 * 60 * 60); | |
// timeout cache (you can also delete the file on update to page and check for existing file only or combination of both) | |
if (filemtime(CACHE_FILE_NAME) + CACHE_TIME >= time() ) { | |
echo file_get_contents(CACHE_FILE_NAME); | |
return; | |
} | |
// start buffering output | |
ob_start(); | |
// do stuff here | |
// get the buffered output | |
$contents = ob_get_clean(); | |
file_put_contents(CACHE_FILE_NAME, $contents); | |
echo $contents; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment