Skip to content

Instantly share code, notes, and snippets.

@daif
Created January 4, 2017 16:50
Show Gist options
  • Save daif/e18d9f0c92047e1415df57e8680e18c5 to your computer and use it in GitHub Desktop.
Save daif/e18d9f0c92047e1415df57e8680e18c5 to your computer and use it in GitHub Desktop.
Tiny Cache system
<?php
//Tiny Cache system
function file_cache_contents($url, $cache_time=360000) { // 60*60*5
$cache_dir = __DIR__.'/cache/';
$cache_file = $cache_dir.md5($url).'.cache';
@mkdir($cache_dir, 0775, true);
// return data from cached file if file exists and not expired
if(file_exists($cache_file) && filemtime($cache_file)+$cache_time >= time()) {
return(unserialize(file_get_contents($cache_file)));
}
// get data from url and cache it then return the data
$cache_data = file_get_contents($url);
file_put_contents($cache_file, serialize($cache_data), LOCK_EX);
return($cache_data);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment