name = $name; $this->location = $location; $this->expires = $expires; $this->gzip = $gzip; return $this; } /** * Allows for chaining from the constructor. Requires PHP 5.3 or newer. * * @param string $name (Required) A name to uniquely identify the cache object. * @param string $location (Optional) The location to store the cache object in. This may vary by cache method. The default value is NULL. * @param integer $expires (Optional) The number of seconds until a cache object is considered stale. The default value is 0. * @param boolean $gzip (Optional) Whether data should be gzipped before being stored. The default value is true. * @return object Reference to the cache object. */ public static function init($name, $location = null, $expires = 0, $gzip = true) { if (version_compare(PHP_VERSION, '5.3.0', '<')) { throw new Exception('PHP 5.3 or newer is required to use CacheCore::init().'); } $self = get_called_class(); return new $self($name, $location, $expires, $gzip); } /** * Set the number of seconds until a cache expires. * * @param integer $expires (Optional) The number of seconds until a cache object is considered stale. The default value is 0. * @return $this */ public function expire_in($seconds) { $this->expires = $seconds; return $this; } /** * Provides a simple, straightforward cache-logic mechanism. Useful for non-complex response caches. * * @param string|function $callback (Required) The name of the function to fire when we need to fetch new data to cache. * @param array params (Optional) Parameters to pass into the callback function, as an array. * @return array The cached data being requested. */ public function response_manager($callback, $params = null) { // Automatically handle $params values. $params = is_array($params) ? $params : array($params); if ($data = $this->read()) { if ($this->is_expired()) { if ($data = call_user_func_array($callback, $params)) { $this->update($data); } else { $this->reset(); $data = $this->read(); } } } else { if ($data = call_user_func_array($callback, $params)) { $this->create($data); } } return $data; } } /*%******************************************************************************************%*/ // CORE DEPENDENCIES // Include the ICacheCore interface. if (file_exists(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'icachecore.interface.php')) { include_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'icachecore.interface.php'; } /*%******************************************************************************************%*/ // EXCEPTIONS class CacheCore_Exception extends Exception {}