Skip to content

Instantly share code, notes, and snippets.

@ksred
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ksred/1e3e3d3e268fa1caea91 to your computer and use it in GitHub Desktop.
Save ksred/1e3e3d3e268fa1caea91 to your computer and use it in GitHub Desktop.
Time interval cache invalidation
/*
* Used to invalidate cache by three URIs and a timestamp
*
* Cache invalidation only happens after a certain predefined time has passed
* Two times are set, one for normal slow data, another for faster data (live)
* These are defined as constants CACHE_VALID and CACHE_VALID_LIVE
* The timestamp of the files on disk is used to check when last the files were written
* and then invalidated if that time is longer than set time
*
* @param one (string) first URI, default "default"
* @param two (string) second URI, default "index"
* @param three (string) third URI, default "index"
* @param valid (int) valid time in minutes
* @return
*/
function invalidate_cache($one = 'default', $two = 'index', $three = 'index', $valid = CACHE_VALID) {
//Check if dir exists
$path = BASE_SERVER_PATH.'application/cache/'.$one.'+'.$two.'+'.$three.'/';
if(is_dir($path)) {
//If request is live data, use shorter update
if (in_array('live', array($one, $two, $three)) || in_array('index', array($one, $two, $three)))
$valid = CACHE_VALID_LIVE;
//Check cache, invalidate after set time
$cache_mod = filemtime($path.'.');
$time = (int) ((time() - $cache_mod) / 60);
if ($time > $valid) {
//Remove cache. First delete files then rm folder
array_map('unlink', glob($path.'*'));
rmdir($path.'/');
}//end if cache still valid
}//end if dir exists
}//end invalidate_cache
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment