Skip to content

Instantly share code, notes, and snippets.

@DanH42
Last active August 29, 2015 13:56
Show Gist options
  • Save DanH42/9193127 to your computer and use it in GitHub Desktop.
Save DanH42/9193127 to your computer and use it in GitHub Desktop.
Simple PHP cache
<?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