Created
July 20, 2018 15:36
-
-
Save elliotboney/d9a3398352d0112fa195ef9b9d28902f to your computer and use it in GitHub Desktop.
Easy php caching from http://wern-ancheta.com/blog/2014/01/26/getting-started-with-caching-in-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 = sys_get_temp_dir().'/phpcache.' . basename($_SERVER['SCRIPT_NAME']); //location of cache file | |
$current_time = time(); | |
$cache_last_modified = filemtime($file); //time when the cache file was last modified | |
if(file_exists($file) && ($current_time < strtotime('+1 day', $cache_last_modified))){ //check if cache file exists and hasn't expired yet | |
include($file); //include cache file | |
}else{ | |
ob_start(); //start output buffering | |
?> | |
<h1>Hello World!</h1> | |
<?php | |
/* | |
some code accessing the database | |
for some data here | |
*/ | |
/* | |
probably some complex computations here | |
*/ | |
$fp = fopen($file, 'w'); //open cache file | |
fwrite($fp, ob_get_contents()); //create new cache file | |
fclose($fp); //close cache file | |
ob_end_flush(); //flush output buffered | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment