Skip to content

Instantly share code, notes, and snippets.

@jcutrell
Created April 14, 2013 23:56
Show Gist options
  • Save jcutrell/5384766 to your computer and use it in GitHub Desktop.
Save jcutrell/5384766 to your computer and use it in GitHub Desktop.
Simple PHP caching function
<?php
$handle = opendir('_cache/');
while (false !== ($entry = readdir($handle))) {
if (strlen($entry) > 2){
$a = explode(".", $entry);
$a = explode("_", $a[0]);
$tstamp = intval($a[1]);
$t = time();
if ($t > ($tstamp + 12)){
// do the call, rewrite the timestamp
rename("_cache/" . $entry, "_cache/file_" . $t . ".cache");
echo file_get_contents("_cache/file_" . $t . ".cache");
} else {
echo file_get_contents("_cache/" . $entry);
}
}
}
?>
@jcutrell
Copy link
Author

This relies on a pre-existing _cache folder and file_xxxx.cache file. The check for the strlen of $entry is to prevent . or .. from being considered cache files. Only intended for one cache file, one call, etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment