Skip to content

Instantly share code, notes, and snippets.

@elliotboney
Created July 20, 2018 15:36
Show Gist options
  • Save elliotboney/d9a3398352d0112fa195ef9b9d28902f to your computer and use it in GitHub Desktop.
Save elliotboney/d9a3398352d0112fa195ef9b9d28902f to your computer and use it in GitHub Desktop.
<?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