Created
January 4, 2017 16:50
-
-
Save daif/e18d9f0c92047e1415df57e8680e18c5 to your computer and use it in GitHub Desktop.
Tiny Cache system
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 | |
//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