abstract class CacheAbstract {
/**
* Disables object cloning
*
* @return void does not return any value
*/
protected function __clone() {
throw new Exception('Cloning Cache objects is not allowed!');
}
/**
* Class constructor
*
* @return void does not return any value
*/
protected function __construct() {
}
} /* class */
interface CacheDriver {
/**
* Adds an object to the cache
*
* @param string stores the variable using this name, should be a cache-unique name
* @param mixed the variable to store in cache
* @param integer time-to-live of cached data
* @return boolean TRUE on success, FALSE otherwise
*/
public function addValue($key, $var, $ttl = 300);
/**
* Returns a dump of all files and user variables from the cache
*
* @return string dump of the user variables, or NULL if an unknown error encountered
*/
public function dumpCache();
/**
* Flushes (clears) the user and system cache
*
* @return void does not return any value
*/
public function flushCache();
/**
* Provides a statistics and meta-data of caching subsystem
*
* @return array array containing cache statistics
*/
public function getStatistics();
/**
* Fetches a stored variable from the cache
*
* @param string key name used to store the value
* @return mixed stored variable on success, FALSE otherwise
*/
public function getValue($key);
/**
* Checks if container contains an item stored under this key name
*
* @param string key name used to store the value
* @return boolean TRUE if the key exists, FALSE otherwise
*/
public function isCached($key);
/**
* Checks whether the chosen driver is supported by PHP installation
*
* @return boolean TRUE if driver can be used, FALSE otherwise
*/
public function isSupported();
/**
* Removes a stored variable from the cache
* @param string key name used to store the value
* @return boolean TRUE on success, FALSE otherwise
*/
public function removeValue($key);
} /* class */
final class CacheDriverApc extends CacheAbstract implements CacheDriver {
/**
* Adds an object to the cache
*
* @param string stores the variable using this name, should be a cache-unique name
* @param mixed the variable to store in cache
* @param integer time-to-live of cached data
* @return boolean TRUE on success, FALSE otherwise
*/
public function addValue($key, $var, $ttl = 300) {
return apc_store($key, $var, $ttl);
}
/**
* Returns a dump of all files and user variables from the cache
*
* @return string dump of the user variables, or NULL if an unknown error encountered
*/
public function dumpCache() {
return apc_bin_dump();
}
/**
* Flushes (clears) the user and system cache
*
* @return void does not return any value
*/
public function flushCache() {
apc_clear_cache();
apc_clear_cache('user');
}
/**
* Provides a statistics and meta-data of caching subsystem
*
* @return array array containing cache statistics
*/
public function getStatistics() {
return array('data' => apc_cache_info
('user'), 'info' => '', 'size' => ''); }
/**
* Fetches a stored variable from the cache
*
* @param string key name used to store the value
* @return mixed stored variable on success, FALSE otherwise
*/
public function getValue($key) {
$var = apc_fetch($key, $result);
if(!$result) {
return false;
}
return $var;
}
/**
* Checks if container contains an item stored under this key name
*
* @param string key name used to store the value
* @return boolean TRUE if the key exists, FALSE otherwise
*/
public function isCached($key) {
return (bool) apc_exists($key);
}
/**
* Checks whether the chosen driver is supported by PHP installation
*
* @return boolean TRUE if driver can be used, FALSE otherwise
*/
public function isSupported() {
return false;
}
return true;
}
/**
* Removes a stored variable from the cache
* @param string key name used to store the value
* @return boolean TRUE on success, FALSE otherwise
*/
public function removeValue($key) {
return apc_delete($key);
}
} /* class */
final class Cache extends CacheAbstract {
private $driver;
private $storage;
/**
* Class constructor.
*
* @param string specifies primary cache driver
* @param string specifies secondary (failover) cache driver
* @return object the Cache object
*/
protected function __construct($driver, $failover) {
if(!$this->loadDriver($driver)) {
if(!$this->loadDriver($failover)) {
$this->loadDriver('dummy');
}
}
}
/**
* Returns a dump of all files and user variables from the cache
*
* @return string dump of the user variables, or NULL if an unknown error encountered
*/
public function dumpCache() {
return $this->storage->dumpCache();
}
/**
* Returns a name of currently used cache storage driver
*
* @return string name of used storage driver
*/
public function getDriver() {
return $this->driver;
}
/**
* Returns the global Cache object, creating it only if it does not exist already
*
* @param string specifies primary cache driver
* @param string specifies secondary (failover) cache driver
* @return object the Cache object
*/
public static function getInstance
($driver = 'file', $failover = 'dummy') { $instance = new Cache($driver, $failover);
}
return $instance;
}
/**
* Returns a list of available cache storage drivers
*
* @return array an array containing a list of available cache drivers
*/
public static function getStorageList
() { $directory = System::getSystemDir() . '/cache/driver'; while(($filename = readdir($dh)) !== false) { if($driver != '' && $driver != '.') {
$list[] = $driver;
}
}
return $list;
}
/**
* Loads the specified cache driver
*
* @param string cache driver to load
* @return boolean TRUE on success, FALSE otherwise
*/
private function loadDriver($driver) {
if(in_array($driver, $this->getStorageList())) { $class = 'CacheDriver' . ucfirst($driver); $this->storage = new $class();
if($this->storage->isSupported()) {
$this->driver = $driver;
return true;
}
}
return false;
}
} /* class */