diff options
Diffstat (limited to 'include/MyCache.class.php')
-rw-r--r-- | include/MyCache.class.php | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/include/MyCache.class.php b/include/MyCache.class.php new file mode 100644 index 0000000..e074a3b --- /dev/null +++ b/include/MyCache.class.php @@ -0,0 +1,113 @@ +<?php +/** + * Simple on-disk variable caching class. Implements file caching logic. + */ +class MyCache { + + private function __construct() {} + + private function __clone() {} + + + /** + * Sets given cache line to given value + * + * @param id string cache line name. It should follow file naming conventions. + * @param value object - any serializable variable + * + * @return its second argument + */ + static function set($id, $value) { + file_put_contents(BASEPATH.'/caches/MyCache/'.$id, serialize($value)); + return $value; + } + + + /** + * fetches cached value from disk + * + * @param id string - cache line identifier to fetch. It should follow file naming conventions. + * @param valid int - if exists and non zero, method throws ExceptionCacheLineExpired if cache line + * is older than given time in seconds + * + * @return restored variable contents + */ + static function get($id, $valid=0, $callback=null, $passexception=true) { + $isValid = self::valid($id, $valid); + if ($valid != 0 && !$isValid && $callback===null) { + throw new CacheLineExpiredException(); + } + if ($isValid) { + return unserialize(file_get_contents(BASEPATH.'/caches/MyCache/'.$id)); + } + elseif ($callback===null) { + if (file_exists(BASEPATH.'/caches/MyCache/'.$id)) { + throw new CacheLineExpiredException(); + } + else { + throw new CacheLineNotFoundException($id); + } + } + else { + try { + $value = call_user_func($callback); + self::set($id,$value); + return $value; + } + catch (Exception $e) { + if ($passexception) { + throw $e; + } + return self::get($id); + } + } + } + + + /** + * Removes given cache line from disk + * + * @param id string cache line name + * + * @return void + */ + static function delete($id) { + if (file_exists(BASEPATH.'/caches/MyCache/'.$id)) { + unlink(BASEPATH.'/caches/MyCache/'.$id); + } + } + + /** + * validates if cache line is not older than given time in seconds + * + * @param id string cache line name. + * @param cacheLifeTime int cache line age limit (default 300 seconds) + * + * @return true if cache is stil valid, false otherwise + */ + static function valid($id, $cacheLifeTime=null) { + + $cacheLifeTime = (int)$cacheLifeTime; //seconds + + if ($cacheLifeTime==null) { + return is_readable(BASEPATH.'/caches/MyCache/'.$id); + } + + if (!is_readable(BASEPATH.'/caches/MyCache/'.$id)) { + return false; + } + return (time()-filemtime(BASEPATH.'/caches/MyCache/'.$id)) <= $cacheLifeTime; + } + + + /** + * checks if cache line exists without validating its age + * + * @param id sting cache line name + * + * @return boolean true if cache line is found + */ + static function exist($id) { + return is_readable(BASEPATH .'/caches/MyCache/'.$id); + } +} |