Index: branches/5.2.x/core/kernel/utility/cache.php =================================================================== diff -u -N -r15226 -r15309 --- branches/5.2.x/core/kernel/utility/cache.php (.../cache.php) (revision 15226) +++ branches/5.2.x/core/kernel/utility/cache.php (.../cache.php) (revision 15309) @@ -1,6 +1,6 @@ isWorking() ) { // defined cache handler is not working -> use default trigger_error('Failed to initialize "' . $handler_class . '" caching handler.', E_USER_WARNING); - $handler = new FakeCacheHandler(); + $handler = new FakeCacheHandler($this); } - elseif ( $this->Application->isDebugMode() && ($handler->cachingType == CACHING_TYPE_MEMORY) ) { + elseif ( $this->Application->isDebugMode() && ($handler->getCachingType() == CACHING_TYPE_MEMORY) ) { $this->Application->Debugger->appendHTML('Memory Caching: "' . $handler_class . '"'); } - $this->_handler =& $handler; - $this->cachingType = $handler->cachingType; - $this->debugCache = $handler->cachingType == CACHING_TYPE_MEMORY && $this->Application->isDebugMode(); + $this->_handler = $handler; + $this->cachingType = $handler->getCachingType(); + $this->debugCache = $handler->getCachingType() == CACHING_TYPE_MEMORY && $this->Application->isDebugMode(); $this->_storeStatistics = defined('DBG_CACHE') && DBG_CACHE; if ( $this->_storeStatistics ) { @@ -340,6 +340,19 @@ } /** + * Returns cached value from local cache + * + * @param string $prepared_name Prepared key name from kCache::prepareKeyName() function + * @return mixed + * @see prepareKeyName + * @access public + */ + public function getFromLocalStorage($prepared_name) + { + return array_key_exists($prepared_name, $this->_localStorage) ? $this->_localStorage[$prepared_name] : false; + } + + /** * Returns value from cache * * @param string|Array $names @@ -545,34 +558,130 @@ } - class FakeCacheHandler { + abstract class kCacheHandler { - var $cachingType = CACHING_TYPE_TEMPORARY; + /** + * Remembers status of cache handler (working or not) + * + * @var bool + * @access protected + */ + protected $_enabled = false; - function FakeCacheHandler() - { + /** + * Caching type that caching handler implements + * + * @var int + * @access protected + */ + protected $cachingType; + /** + * + * @var kCache + * @access protected + */ + protected $parent; + + public function __construct(kCache $parent) + { + $this->parent = $parent; } /** * Retrieves value from cache * * @param string $names * @return mixed + * @access public */ - function get($names) + abstract public function get($names); + + /** + * Stores value in cache + * + * @param string $name + * @param mixed $value + * @param int $expiration + * @return bool + * @access public + */ + abstract public function set($name, $value, $expiration = 0); + + /** + * Stores value in cache (only if it's not there already) + * + * @param string $name + * @param mixed $value + * @param int $expiration + * @return bool + * @access public + */ + abstract public function add($name, $value, $expiration = 0); + + /** + * Deletes key from cach + * + * @param string $name + * @return bool + * @access public + */ + abstract public function delete($name); + + /** + * Determines, that cache storage is working fine + * + * @return bool + * @access public + */ + public function isWorking() { + return $this->_enabled; + } + + /** + * Returns caching type of current storage engine + * + * @return int + * @access public + */ + public function getCachingType() + { + return $this->cachingType; + } + } + + + class FakeCacheHandler extends kCacheHandler { + + public function __construct(kCache $parent) + { + parent::__construct($parent); + + $this->_enabled = true; + $this->cachingType = CACHING_TYPE_TEMPORARY; + } + + /** + * Retrieves value from cache + * + * @param string|Array $names + * @return mixed + * @access public + */ + public function get($names) + { if ( is_array($names) ) { $res = Array (); foreach ($names as $name) { - $res[$name] = false; + $res[$name] = $this->parent->getFromLocalStorage($name); } return $res; } - return false; + return $this->parent->getFromLocalStorage($names); } /** @@ -582,8 +691,9 @@ * @param mixed $value * @param int $expiration * @return bool + * @access public */ - function set($name, $value, $expiration = 0) + public function set($name, $value, $expiration = 0) { return true; } @@ -595,8 +705,9 @@ * @param mixed $value * @param int $expiration * @return bool + * @access public */ - function add($name, $value, $expiration = 0) + public function add($name, $value, $expiration = 0) { return true; } @@ -606,39 +717,31 @@ * * @param string $name * @return bool + * @access public */ - function delete($name) + public function delete($name) { return true; } - - /** - * Determines, that cache storage is working fine - * - * @return bool - */ - function isWorking() - { - return true; - } } - class MemcacheCacheHandler { + class MemcacheCacheHandler extends kCacheHandler { - var $_enabled = false; - /** * Memcache connection * * @var Memcache + * @access protected */ - var $_handler = null; + protected $_handler = null; - var $cachingType = CACHING_TYPE_MEMORY; - - function MemcacheCacheHandler($default_servers = '') + public function __construct(kCache $parent, $default_servers = '') { + parent::__construct($parent); + + $this->cachingType = CACHING_TYPE_MEMORY; + $vars = kUtil::getConfigVars(); $memcached_servers = isset($vars['MemcacheServers']) ? $vars['MemcacheServers'] : $default_servers; @@ -672,8 +775,9 @@ * * @param string $name * @return mixed + * @access public */ - function get($name) + public function get($name) { return $this->_handler->get($name); } @@ -685,8 +789,9 @@ * @param mixed $value * @param int $expiration * @return bool + * @access public */ - function set($name, $value, $expiration = 0) + public function set($name, $value, $expiration = 0) { // 0 - don't use compression return $this->_handler->set($name, $value, 0, $expiration); @@ -699,8 +804,9 @@ * @param mixed $value * @param int $expiration * @return bool + * @access public */ - function add($name, $value, $expiration = 0) + public function add($name, $value, $expiration = 0) { // 0 - don't use compression return $this->_handler->add($name, $value, 0, $expiration); @@ -711,36 +817,26 @@ * * @param string $name * @return bool + * @access public */ - function delete($name) + public function delete($name) { return $this->_handler->delete($name, 0); } - - /** - * Determines, that cache storage is working fine - * - * @return bool - */ - function isWorking() - { - return $this->_enabled; - } } - class ApcCacheHandler { + class ApcCacheHandler extends kCacheHandler { - var $_enabled = false; - - var $cachingType = CACHING_TYPE_MEMORY; - - function ApcCacheHandler() + public function __construct(kCache $parent) { + parent::__construct($parent); + + $this->cachingType = CACHING_TYPE_MEMORY; $this->_enabled = function_exists('apc_fetch'); - // verify, that apc is working - if ($this->_enabled && !$this->set('test', 1)) { + // verify, that apc is working + if ( $this->_enabled && !$this->set('test', 1) ) { $this->_enabled = false; } } @@ -750,8 +846,9 @@ * * @param string $name * @return mixed + * @access public */ - function get($name) + public function get($name) { return apc_fetch($name); } @@ -763,8 +860,9 @@ * @param mixed $value * @param int $expiration * @return bool + * @access public */ - function set($name, $value, $expiration = 0) + public function set($name, $value, $expiration = 0) { return apc_store($name, $value, $expiration); } @@ -776,8 +874,9 @@ * @param mixed $value * @param int $expiration * @return bool + * @access public */ - function add($name, $value, $expiration = 0) + public function add($name, $value, $expiration = 0) { return apc_add($name, $value, $expiration); } @@ -787,35 +886,26 @@ * * @param string $name * @return bool + * @access public */ - function delete($name) + public function delete($name) { return apc_delete($name); } - - /** - * Determines, that cache storage is working fine - * - * @return bool - */ - function isWorking() - { - return $this->_enabled; - } } - class XCacheCacheHandler { - var $_enabled = false; + class XCacheCacheHandler extends kCacheHandler { - var $cachingType = CACHING_TYPE_MEMORY; - - function XCacheCacheHandler() + public function __construct(kCache $parent) { + parent::__construct($parent); + + $this->cachingType = CACHING_TYPE_MEMORY; $this->_enabled = function_exists('xcache_get'); - // verify, that xcache is working - if ($this->_enabled && !$this->set('test', 1)) { + // verify, that xcache is working + if ( $this->_enabled && !$this->set('test', 1) ) { $this->_enabled = false; } } @@ -825,8 +915,9 @@ * * @param string|Array $names * @return mixed + * @access public */ - function get($names) + public function get($names) { if ( is_array($names) ) { $res = Array (); @@ -848,8 +939,9 @@ * @param mixed $value * @param int $expiration * @return bool + * @access public */ - function set($name, $value, $expiration = 0) + public function set($name, $value, $expiration = 0) { return xcache_set($name, $value, $expiration); } @@ -861,8 +953,9 @@ * @param mixed $value * @param int $expiration * @return bool + * @access public */ - function add($name, $value, $expiration = 0) + public function add($name, $value, $expiration = 0) { // not atomic operation, like in Memcached and may fail if ( xcache_isset($name) ) { @@ -877,19 +970,10 @@ * * @param string $name * @return bool + * @access public */ - function delete($name) + public function delete($name) { return xcache_unset($name); } - - /** - * Determines, that cache storage is working fine - * - * @return bool - */ - function isWorking() - { - return $this->_enabled; - } } \ No newline at end of file