Last active
August 29, 2015 13:56
-
-
Save DanH42/9193127 to your computer and use it in GitHub Desktop.
Simple PHP cache
This file contains hidden or 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 | |
$cache = json_decode(file_get_contents("cache.json"), true); | |
function get($url, $max_age=1){ | |
global $cache; | |
$hash = crc32($url); | |
if(isset($cache[$hash])){ | |
if(time() - $cache[$hash] > $max_age) | |
return cache_add($url, $hash); | |
return file_get_contents("cache/" . $hash . ".txt"); | |
}else | |
return cache_add($url, $hash); | |
}function cache_add($url, $hash){ | |
global $cache; | |
$data = file_get_contents($url); | |
file_put_contents("cache/" . $hash . ".txt", $data); | |
$cache[$hash] = time(); | |
file_put_contents("cache.json", json_encode($cache)); | |
return $data; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment