Skip to content

Instantly share code, notes, and snippets.

@MitMaro
Created June 16, 2012 15:51
Show Gist options
  • Save MitMaro/2941752 to your computer and use it in GitHub Desktop.
Save MitMaro/2941752 to your computer and use it in GitHub Desktop.
PHP Cache Example
<?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