* @license PHP License * @package WB * @subpackage base */ /** * Caching * * Very simple to use caching interface. * * @version 0.3.2 * @package WB * @subpackage base */ class WBCache extends WBStdClass { /** * used driver * @var WBCache_Driver */ static protected $driver; /** * cache index * @var array */ static protected $index; /** * constructor * * Avoid creating object */ private function __construct() { } /** * fetch cache data * * Load cached data with id. In case cache-id does not exists, null is returned. * Also expire-information will be evaluated. * * Expire values: * - filemtime * Makes cache invalid of cached data is older than file's modification time. * - use * The use counter tells how often cache data may be loaded from cache. Beyond that * count, cached data will be flushed * - ttl * The ttl is a number of seconds the cache may be valid. * * @see set() * @param string $id * @param string $minDate timestamp - cache must be younger than give minDate * @return mixed return whatever was cached or null */ static function get($id, $minDate = null) { if (!WBParam::get('wb/cache/use', true)) { return null; } self::startDriver(); $id = md5($id); if (!isset(self::$index[$id])) { return null; } $expire = self::$index[$id]; // minimum timestamp if (!empty($minDate)) { $minDate = strtotime($minDate); if ($minDate > $expire['cached']) { unset(self::$index[$id]); self::$driver->flush($id); return null; } } // filemtime if (isset($expire['filemtime'])) { if (filemtime($expire['filemtime']) > $expire['cached']) { unset(self::$index[$id]); self::$driver->flush($id); return null; } } // evaluate time to live if (isset($expire['ttl'])) { if (($expire['ttl'] + $expire['cached'] ) < time()) { unset(self::$index[$id]); self::$driver->flush($id); return null; } } // max count of uses if (isset($expire['use']) && $expire['use'] < $expire['used']) { unset(self::$index[$id]); self::$driver->flush($id); return null; } ++self::$index[$id]['used']; return self::$driver->get($id); } /** * store data in cache * * Use id to store in cache. See get() for a list of expire parameter * * @see get() * @param string $id * @param mixed $data whatever you want to cache * @param array $expire parameter to tell * @return bool true on success */ static function set($id, $data, $expire = array()) { if (!WBParam::get('wb/cache/use', true)) { return true; } if (!is_array($expire)) { $expire = array(); } // not need to cache anything that has not time to live if (isset($expire['ttl']) && 1 > $expire['ttl']) { return true; } self::startDriver(); $id = md5($id); $expire['used'] = 0; $expire['cached'] = time(); self::$index[$id] = $expire; return self::$driver->set($id, $data); } /** * Fluh cache data * * Use any cache id * * @param string $id */ static public function flush($id) { if (!WBParam::get('wb/cache/use', true)) { return; } self::startDriver(); $id = md5($id); if (!isset(self::$index[$id])) { return; } unset(self::$index[$id]); self::$driver->flush($id); } /** * Start driver on demand * * Start cache driver and load index */ static protected function startDriver() { if (self::$driver) { return true; } $driver = WBParam::get('wb/cache/driver', 'file'); $driver = ucfirst(strtolower($driver)); self::$driver = WBClass::create('WBCache_Driver_' . $driver); self::$index =& self::$driver->getIndex(); return true; } }