From 3cc051e1b6523fa7ae776e1cbb889de0c0044ef0 Mon Sep 17 00:00:00 2001 From: Michael M Slusarz Date: Mon, 15 Nov 2010 19:30:50 -0700 Subject: [PATCH] Abstracted storage code in Horde_Cache --- framework/Cache/lib/Horde/Cache.php | 70 ++++++++-------- framework/Cache/lib/Horde/Cache/Mock.php | 79 ------------------ framework/Cache/lib/Horde/Cache/Null.php | 56 ------------- framework/Cache/lib/Horde/Cache/Storage.php | 93 ++++++++++++++++++++++ .../Cache/lib/Horde/Cache/{ => Storage}/Apc.php | 25 ++---- .../lib/Horde/Cache/{ => Storage}/Eaccelerator.php | 31 ++------ .../Cache/lib/Horde/Cache/{ => Storage}/File.php | 34 ++------ .../lib/Horde/Cache/{ => Storage}/Memcache.php | 63 ++++++++------- framework/Cache/lib/Horde/Cache/Storage/Mock.php | 57 +++++++++++++ framework/Cache/lib/Horde/Cache/Storage/Null.php | 44 ++++++++++ .../lib/Horde/Cache/{ => Storage}/Session.php | 25 ++---- .../Cache/lib/Horde/Cache/{ => Storage}/Sql.php | 29 ++----- .../Cache/lib/Horde/Cache/{ => Storage}/Stack.php | 31 +++----- .../Cache/lib/Horde/Cache/{ => Storage}/Xcache.php | 25 ++---- framework/Cache/package.xml | 47 ++++++----- framework/Core/lib/Horde/Core/Factory/Cache.php | 46 +++++------ 16 files changed, 354 insertions(+), 401 deletions(-) delete mode 100644 framework/Cache/lib/Horde/Cache/Mock.php delete mode 100644 framework/Cache/lib/Horde/Cache/Null.php create mode 100644 framework/Cache/lib/Horde/Cache/Storage.php rename framework/Cache/lib/Horde/Cache/{ => Storage}/Apc.php (68%) rename framework/Cache/lib/Horde/Cache/{ => Storage}/Eaccelerator.php (67%) rename framework/Cache/lib/Horde/Cache/{ => Storage}/File.php (90%) rename framework/Cache/lib/Horde/Cache/{ => Storage}/Memcache.php (76%) create mode 100644 framework/Cache/lib/Horde/Cache/Storage/Mock.php create mode 100644 framework/Cache/lib/Horde/Cache/Storage/Null.php rename framework/Cache/lib/Horde/Cache/{ => Storage}/Session.php (71%) rename framework/Cache/lib/Horde/Cache/{ => Storage}/Sql.php (87%) rename framework/Cache/lib/Horde/Cache/{ => Storage}/Stack.php (73%) rename framework/Cache/lib/Horde/Cache/{ => Storage}/Xcache.php (69%) diff --git a/framework/Cache/lib/Horde/Cache.php b/framework/Cache/lib/Horde/Cache.php index b443fed15..7c9e07e43 100644 --- a/framework/Cache/lib/Horde/Cache.php +++ b/framework/Cache/lib/Horde/Cache.php @@ -1,7 +1,6 @@ * @author Michael Slusarz * @category Horde + * @license http://www.fsf.org/copyleft/lgpl.html LGPL * @package Cache */ -abstract class Horde_Cache +class Horde_Cache { /** * Cache parameters. @@ -23,8 +23,7 @@ abstract class Horde_Cache */ protected $_params = array( 'compress' => false, - 'lifetime' => 86400, - 'prefix' => '', + 'lifetime' => 86400 ); /** @@ -35,9 +34,17 @@ abstract class Horde_Cache protected $_logger; /** - * Construct a new Horde_Cache object. + * Storage object. * - * @param array $params Parameter array: + * @var Horde_Cache_Storage + */ + protected $_storage; + + /** + * Constructor. + * + * @param Horde_Cache_Storage $storage The storage object. + * @param array $params Parameter array: *
      * 'compress' - (boolean) Compress data? Requires the 'lzf' PECL
      *              extension.
@@ -47,11 +54,14 @@ abstract class Horde_Cache
      * 'logger' - (Horde_Log_Logger) Log object to use for log/debug messages.
      * 
*/ - public function __construct($params = array()) + public function __construct(Horde_Cache_Storage $storage, + array $params = array()) { if (isset($params['logger'])) { $this->_logger = $params['logger']; unset($params['logger']); + + $storage->setLogger($this->_logger); } if (!empty($params['compress']) && !extension_loaded('lzf')) { @@ -59,6 +69,7 @@ abstract class Horde_Cache } $this->_params = array_merge($this->_params, $params); + $this->_storage = $storage; } /** @@ -90,7 +101,7 @@ abstract class Horde_Cache */ public function get($key, $lifetime = 1) { - $res = $this->_get($key, $lifetime); + $res = $this->_storage->get($key, $lifetime); return ($this->_params['compress'] && ($res !== false)) // lzf_decompress() returns false on error @@ -99,41 +110,28 @@ abstract class Horde_Cache } /** - * @see get() - */ - abstract protected function _get($key, $lifetime); - - /** * Store an object in the cache. * * @param string $key Object ID used as the caching key. - * @param mixed $data Data to store in the cache. + * @param string $data Data to store in the cache. * @param integer $lifetime Object lifetime - i.e. the time before the * data becomes available for garbage * collection. If null use the default Horde GC * time. If 0 will not be GC'd. - * - * @throws Horde_Cache_Exception */ public function set($key, $data, $lifetime = null) { - if (!is_string($data)) { - throw new Horde_Cache_Exception('Cache data must be a string.'); - } - if ($this->_params['compress']) { $data = lzf_compress($data); } + $lifetime = is_null($lifetime) + ? $this->_params['lifetime'] + : $lifetime; - $res = $this->_set($key, $data, $lifetime); + $this->_storage->set($key, $data, $lifetime); } /** - * @see set() - */ - abstract protected function _set($key, $data, $lifetime); - - /** * Checks if a given key exists in the cache, valid for the given * lifetime. * @@ -142,7 +140,10 @@ abstract class Horde_Cache * * @return boolean Existence. */ - abstract public function exists($key, $lifetime = 1); + public function exists($key, $lifetime = 1) + { + return $this->_storage->exists($key, $lifetime); + } /** * Expire any existing data for the given key. @@ -151,18 +152,9 @@ abstract class Horde_Cache * * @return boolean Success or failure. */ - abstract public function expire($key); - - /** - * Determine the default lifetime for data. - * - * @param mixed $lifetime The lifetime to use or null for default. - * - * @return integer The lifetime, in seconds. - */ - protected function _getLifetime($lifetime) + public function expire($key) { - return is_null($lifetime) ? $this->_params['lifetime'] : $lifetime; + return $this->_storage->expire($key); } } diff --git a/framework/Cache/lib/Horde/Cache/Mock.php b/framework/Cache/lib/Horde/Cache/Mock.php deleted file mode 100644 index 234997d4d..000000000 --- a/framework/Cache/lib/Horde/Cache/Mock.php +++ /dev/null @@ -1,79 +0,0 @@ - - * @category Horde - * @license http://www.fsf.org/copyleft/lgpl.html LGPL - * @link http://pear.horde.org/index.php?package=Cache - * @package Cache - */ -class Horde_Cache_Mock extends Horde_Cache -{ - /** - * The storage location for this cache. - * - * @var array - */ - private $_cache = array(); - - /** - * Construct a new Horde_Cache_Mock object. - * - * @param array $params Configuration parameters: - */ - public function __construct($params = array()) - { - parent::__construct($params); - } - - /** - */ - protected function _get($key, $lifetime) - { - return isset($this->_cache[$key]) - ? $this->_cache[$key] - : false; - } - - /** - */ - protected function _set($key, $data, $lifetime) - { - $this->_cache[$key] = $data; - } - - /** - * Checks if a given key exists in the cache, valid for the given - * lifetime. - * - * @param string $key Cache key to check. - * @param integer $lifetime Lifetime of the key in seconds. - * - * @return boolean Existence. - */ - public function exists($key, $lifetime = 1) - { - return isset($this->_cache[$key]); - } - - /** - * Expire any existing data for the given key. - * - * @param string $key Cache key to expire. - * - * @return boolean Success or failure. - */ - public function expire($key) - { - unset($this->_cache[$key]); - } - -} diff --git a/framework/Cache/lib/Horde/Cache/Null.php b/framework/Cache/lib/Horde/Cache/Null.php deleted file mode 100644 index a332f858d..000000000 --- a/framework/Cache/lib/Horde/Cache/Null.php +++ /dev/null @@ -1,56 +0,0 @@ - - * - * See the enclosed file COPYING for license information (LGPL). If you - * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. - * - * @author Duck - * @category Horde - * @package Cache - */ -class Horde_Cache_Null extends Horde_Cache -{ - /** - */ - protected function _get($key, $lifetime) - { - return false; - } - - /** - */ - protected function _set($key, $data, $lifetime) - { - } - - /** - * Checks if a given key exists in the cache, valid for the given - * lifetime. - * - * @param string $key Cache key to check. - * @param integer $lifetime Lifetime of the key in seconds. - * - * @return boolean Existence. - */ - public function exists($key, $lifetime = 1) - { - return false; - } - - /** - * Expire any existing data for the given key. - * - * @param string $key Cache key to expire. - * - * @return boolean Success or failure. - */ - public function expire($key) - { - return false; - } - -} diff --git a/framework/Cache/lib/Horde/Cache/Storage.php b/framework/Cache/lib/Horde/Cache/Storage.php new file mode 100644 index 000000000..6db05d2c5 --- /dev/null +++ b/framework/Cache/lib/Horde/Cache/Storage.php @@ -0,0 +1,93 @@ + + * @category Horde + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + * @package Cache + */ +abstract class Horde_Cache_Storage +{ + /** + * Logger. + * + * @var Horde_Log_Logger + */ + protected $_logger; + + /** + * Parameters. + * + * @var array + */ + protected $_params = array(); + + /** + * Constructor. + * + * @param array $params Configuration parameters. + */ + public function __construct(array $params = array()) + { + $this->_params = array_merge($this->_params, $params); + } + + /** + * Set the logging object. + * + * @param Horde_Log_Logger $logger Log object. + */ + public function setLogger($logger) + { + $this->_logger = $logger; + } + + /** + * Retrieve cached data. + * + * @param string $key Object ID to query. + * @param integer $lifetime Lifetime of the object in seconds. + * + * @return mixed Cached data, or false if none was found. + */ + abstract public function get($key, $lifetime); + + /** + * Store an object in the cache. + * + * @param string $key Object ID used as the caching key. + * @param mixed $data Data to store in the cache. + * @param integer $lifetime Object lifetime - i.e. the time before the + * data becomes available for garbage + * collection. If 0 will not be GC'd. + */ + abstract public function set($key, $data, $lifetime); + + /** + * Checks if a given key exists in the cache, valid for the given + * lifetime. + * + * @param string $key Cache key to check. + * @param integer $lifetime Lifetime of the key in seconds. + * + * @return boolean Existence. + */ + abstract public function exists($key, $lifetime); + + /** + * Expire any existing data for the given key. + * + * @param string $key Cache key to expire. + * + * @return boolean Success or failure. + */ + abstract public function expire($key); + +} diff --git a/framework/Cache/lib/Horde/Cache/Apc.php b/framework/Cache/lib/Horde/Cache/Storage/Apc.php similarity index 68% rename from framework/Cache/lib/Horde/Cache/Apc.php rename to framework/Cache/lib/Horde/Cache/Storage/Apc.php index 324ceb4b6..12ea3a3a5 100644 --- a/framework/Cache/lib/Horde/Cache/Apc.php +++ b/framework/Cache/lib/Horde/Cache/Storage/Apc.php @@ -1,7 +1,6 @@ * @@ -10,13 +9,14 @@ * * @author Duck * @category Horde + * @license http://www.fsf.org/copyleft/lgpl.html LGPL * @package Cache */ -class Horde_Cache_Apc extends Horde_Cache +class Horde_Cache_Storage_Apc extends Horde_Cache_Storage { /** */ - protected function _get($key, $lifetime) + public function get($key, $lifetime) { $key = $this->_params['prefix'] . $key; $this->_setExpire($key, $lifetime); @@ -25,25 +25,17 @@ class Horde_Cache_Apc extends Horde_Cache /** */ - protected function _set($key, $data, $lifetime) + public function set($key, $data, $lifetime) { $key = $this->_params['prefix'] . $key; - $lifetime = $this->_getLifetime($lifetime); if (apc_store($key . '_expire', time(), $lifetime)) { apc_store($key, $data, $lifetime); } } /** - * Checks if a given key exists in the cache, valid for the given - * lifetime. - * - * @param string $key Cache key to check. - * @param integer $lifetime Lifetime of the key in seconds. - * - * @return boolean Existence. */ - public function exists($key, $lifetime = 1) + public function exists($key, $lifetime) { $key = $this->_params['prefix'] . $key; $this->_setExpire($key, $lifetime); @@ -51,11 +43,6 @@ class Horde_Cache_Apc extends Horde_Cache } /** - * Expire any existing data for the given key. - * - * @param string $key Cache key to expire. - * - * @return boolean Success or failure. */ public function expire($key) { diff --git a/framework/Cache/lib/Horde/Cache/Eaccelerator.php b/framework/Cache/lib/Horde/Cache/Storage/Eaccelerator.php similarity index 67% rename from framework/Cache/lib/Horde/Cache/Eaccelerator.php rename to framework/Cache/lib/Horde/Cache/Storage/Eaccelerator.php index 2a911f8ad..1a58a1b1c 100644 --- a/framework/Cache/lib/Horde/Cache/Eaccelerator.php +++ b/framework/Cache/lib/Horde/Cache/Storage/Eaccelerator.php @@ -1,7 +1,6 @@ * @@ -10,18 +9,15 @@ * * @author Duck * @category Horde + * @license http://www.fsf.org/copyleft/lgpl.html LGPL * @package Cache */ -class Horde_Cache_Eaccelerator extends Horde_Cache +class Horde_Cache_Storage_Eaccelerator extends Horde_Cache_Storage { /** - * Construct a new Horde_Cache object. - * - * @param array $params Parameter array. - * * @throws Horde_Cache_Exception */ - public function __construct($params = array()) + public function __construct(array $params = array()) { if (!function_exists('eaccelerator_gc')) { throw new Horde_Cache_Exception('eAccelerator must be compiled with support for shared memory to use as caching backend.'); @@ -32,7 +28,7 @@ class Horde_Cache_Eaccelerator extends Horde_Cache /** */ - protected function _get($key, $lifetime) + public function get($key, $lifetime) { $key = $this->_params['prefix'] . $key; $this->_setExpire($key, $lifetime); @@ -41,25 +37,17 @@ class Horde_Cache_Eaccelerator extends Horde_Cache /** */ - protected function _set($key, $data, $lifetime) + public function set($key, $data, $lifetime) { $key = $this->_params['prefix'] . $key; - $lifetime = $this->_getLifetime($lifetime); if (eaccelerator_put($key . '_expire', time(), $lifetime)) { eaccelerator_put($key, $data, $lifetime); } } /** - * Checks if a given key exists in the cache, valid for the given - * lifetime. - * - * @param string $key Cache key to check. - * @param integer $lifetime Lifetime of the key in seconds. - * - * @return boolean Existence. */ - public function exists($key, $lifetime = 1) + public function exists($key, $lifetime) { $key = $this->_params['prefix'] . $key; $this->_setExpire($key, $lifetime); @@ -67,11 +55,6 @@ class Horde_Cache_Eaccelerator extends Horde_Cache } /** - * Expire any existing data for the given key. - * - * @param string $key Cache key to expire. - * - * @return boolean Success or failure. */ public function expire($key) { diff --git a/framework/Cache/lib/Horde/Cache/File.php b/framework/Cache/lib/Horde/Cache/Storage/File.php similarity index 90% rename from framework/Cache/lib/Horde/Cache/File.php rename to framework/Cache/lib/Horde/Cache/Storage/File.php index d8fae0913..84cae41db 100644 --- a/framework/Cache/lib/Horde/Cache/File.php +++ b/framework/Cache/lib/Horde/Cache/Storage/File.php @@ -1,7 +1,6 @@ * @author Chuck Hagenbuch + * @author Michael Slusarz * @category Horde * @license http://www.fsf.org/copyleft/lgpl.html LGPL * @package Cache */ -class Horde_Cache_File extends Horde_Cache +class Horde_Cache_Storage_File extends Horde_Cache_Storage { /* Location of the garbage collection data file. */ const GC_FILE = 'horde_cache_gc'; @@ -118,7 +118,7 @@ class Horde_Cache_File extends Horde_Cache /** */ - protected function _get($key, $lifetime) + public function get($key, $lifetime) { if (!$this->exists($key, $lifetime)) { /* Nothing cached, return failure. */ @@ -135,7 +135,7 @@ class Horde_Cache_File extends Horde_Cache /** */ - protected function _set($key, $data, $lifetime) + public function set($key, $data, $lifetime) { $filename = $this->_keyToFile($key, true); $tmp_file = Horde_Util::getTempFile('HordeCache', true, $this->_dir); @@ -149,7 +149,6 @@ class Horde_Cache_File extends Horde_Cache @rename($tmp_file, $filename); - $lifetime = $this->_getLifetime($lifetime); if (($lifetime != $this->_params['lifetime']) && ($fp = @fopen($this->_dir . '/horde_cache_gc', 'a'))) { // This may result in duplicate entries in horde_cache_gc, but we @@ -162,7 +161,7 @@ class Horde_Cache_File extends Horde_Cache /** */ - public function exists($key, $lifetime = 1) + public function exists($key, $lifetime) { $filename = $this->_keyToFile($key); @@ -191,18 +190,6 @@ class Horde_Cache_File extends Horde_Cache } /** - */ - public function output($key, $lifetime = 1) - { - if (!$this->exists($key, $lifetime)) { - return false; - } - - $filename = $this->_keyToFile($key); - return @readfile($filename); - } - - /** * Map a cache key to a unique filename. * * @param string $key Cache key. @@ -237,13 +224,4 @@ class Horde_Cache_File extends Horde_Cache return $this->_file[$key]; } - /** - * TODO - * - * @throws Horde_Cache_Exception - */ - protected function _gcDir($dir, &$excepts) - { - } - } diff --git a/framework/Cache/lib/Horde/Cache/Memcache.php b/framework/Cache/lib/Horde/Cache/Storage/Memcache.php similarity index 76% rename from framework/Cache/lib/Horde/Cache/Memcache.php rename to framework/Cache/lib/Horde/Cache/Storage/Memcache.php index bb90bc82b..484d41cb3 100644 --- a/framework/Cache/lib/Horde/Cache/Memcache.php +++ b/framework/Cache/lib/Horde/Cache/Storage/Memcache.php @@ -1,6 +1,6 @@ @@ -12,22 +12,25 @@ * @author Duck * @author Michael Slusarz * @category Horde + * @license http://www.fsf.org/copyleft/lgpl.html LGPL * @package Cache */ -class Horde_Cache_Memcache extends Horde_Cache +class Horde_Cache_Storage_Memcache extends Horde_Cache_Storage implements Serializable { /** - * Memcache object. + * Cache results of expire() calls (since we will get the entire object + * on an expire() call anyway). * - * @var Horde_Memcache + * @var array */ - protected $_memcache; + protected $_expirecache = array(); /** - * Cache results of expire() calls (since we will get the entire object - * on an expire() call anyway). + * Memcache object. + * + * @var Horde_Memcache */ - protected $_expirecache = array(); + protected $_memcache; /** * Construct a new Horde_Cache_Memcache object. @@ -46,22 +49,14 @@ class Horde_Cache_Memcache extends Horde_Cache } $this->_memcache = $params['memcache']; + unset($params['memcache']); parent::__construct($params); } /** - * Do cleanup prior to serialization and provide a list of variables - * to serialize. */ - public function __sleep() - { - return array('_memcache'); - } - - /** - */ - protected function _get($key, $lifetime) + public function get($key, $lifetime) { $key = $this->_params['prefix'] . $key; if (isset($this->_expirecache[$key])) { @@ -93,10 +88,9 @@ class Horde_Cache_Memcache extends Horde_Cache /** */ - protected function _set($key, $data, $lifetime) + public function _set($key, $data, $lifetime) { $key = $this->_params['prefix'] . $key; - $lifetime = $this->_getLifetime($lifetime); if ($this->_memcache->set($key . '_e', time(), $lifetime) !== false) { $this->_memcache->set($key, $data, $lifetime); @@ -104,14 +98,8 @@ class Horde_Cache_Memcache extends Horde_Cache } /** - * Checks if a given key exists in the cache. - * - * @param string $key Cache key to check. - * @param integer $lifetime Lifetime of the key in seconds. - * - * @return boolean Existence. */ - public function exists($key, $lifetime = 1) + public function exists($key, $lifetime) { $key = $this->_params['prefix'] . $key; @@ -119,11 +107,6 @@ class Horde_Cache_Memcache extends Horde_Cache } /** - * Expire any existing data for the given key. - * - * @param string $key Cache key to expire. - * - * @return boolean Success or failure. */ public function expire($key) { @@ -134,4 +117,20 @@ class Horde_Cache_Memcache extends Horde_Cache return $this->_memcache->delete($key); } + /* Serializable methods. */ + + /** + */ + public function serialize() + { + return serialize($this->_memcache); + } + + /** + */ + public function unserialize($data) + { + $this->_memcache = unserialize($data); + } + } diff --git a/framework/Cache/lib/Horde/Cache/Storage/Mock.php b/framework/Cache/lib/Horde/Cache/Storage/Mock.php new file mode 100644 index 000000000..735241f74 --- /dev/null +++ b/framework/Cache/lib/Horde/Cache/Storage/Mock.php @@ -0,0 +1,57 @@ + + * @category Horde + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + * @link http://pear.horde.org/index.php?package=Cache + * @package Cache + */ +class Horde_Cache_Storage_Mock extends Horde_Cache_Storage +{ + /** + * The storage location for this cache. + * + * @var array + */ + private $_cache = array(); + + /** + */ + public function get($key, $lifetime) + { + return isset($this->_cache[$key]) + ? $this->_cache[$key] + : false; + } + + /** + */ + public function set($key, $data, $lifetime) + { + $this->_cache[$key] = $data; + } + + /** + */ + public function exists($key, $lifetime = 1) + { + return isset($this->_cache[$key]); + } + + /** + */ + public function expire($key) + { + unset($this->_cache[$key]); + } + +} diff --git a/framework/Cache/lib/Horde/Cache/Storage/Null.php b/framework/Cache/lib/Horde/Cache/Storage/Null.php new file mode 100644 index 000000000..36b88ec09 --- /dev/null +++ b/framework/Cache/lib/Horde/Cache/Storage/Null.php @@ -0,0 +1,44 @@ + + * + * See the enclosed file COPYING for license information (LGPL). If you + * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. + * + * @author Duck + * @category Horde + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + * @package Cache + */ +class Horde_Cache_Storage_Null extends Horde_Cache_Storage +{ + /** + */ + public function get($key, $lifetime) + { + return false; + } + + /** + */ + public function set($key, $data, $lifetime) + { + } + + /** + */ + public function exists($key, $lifetime) + { + return false; + } + + /** + */ + public function expire($key) + { + return false; + } + +} diff --git a/framework/Cache/lib/Horde/Cache/Session.php b/framework/Cache/lib/Horde/Cache/Storage/Session.php similarity index 71% rename from framework/Cache/lib/Horde/Cache/Session.php rename to framework/Cache/lib/Horde/Cache/Storage/Session.php index 6aeb6b5c8..3a5d75fcf 100644 --- a/framework/Cache/lib/Horde/Cache/Session.php +++ b/framework/Cache/lib/Horde/Cache/Storage/Session.php @@ -1,7 +1,6 @@ exists($key, $lifetime) ? $this->_sess[$key]['d'] @@ -56,24 +55,17 @@ class Horde_Cache_Session extends Horde_Cache /** */ - protected function _set($key, $data, $lifetime) + public function set($key, $data, $lifetime) { $this->_sess[$key] = array( 'd' => $data, - 'l' => $this->_getLifetime($lifetime) + 'l' => $lifetime ); } /** - * Checks if a given key exists in the cache, valid for the given - * lifetime. - * - * @param string $key Cache key to check. - * @param integer $lifetime Lifetime of the key in seconds. - * - * @return boolean Existence. */ - public function exists($key, $lifetime = 1) + public function exists($key, $lifetime) { if (isset($this->_sess[$key])) { /* 0 means no expire. */ @@ -89,11 +81,6 @@ class Horde_Cache_Session extends Horde_Cache } /** - * Expire any existing data for the given key. - * - * @param string $key Cache key to expire. - * - * @return boolean Success or failure. */ public function expire($key) { diff --git a/framework/Cache/lib/Horde/Cache/Sql.php b/framework/Cache/lib/Horde/Cache/Storage/Sql.php similarity index 87% rename from framework/Cache/lib/Horde/Cache/Sql.php rename to framework/Cache/lib/Horde/Cache/Storage/Sql.php index ead301c0d..c17e3d415 100644 --- a/framework/Cache/lib/Horde/Cache/Sql.php +++ b/framework/Cache/lib/Horde/Cache/Storage/Sql.php @@ -1,7 +1,6 @@ @@ -23,12 +22,13 @@ * See the enclosed file COPYING for license information (LGPL). If you * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. * - * @author Michael Slusarz * @author Ben Klang + * @author Michael Slusarz * @category Horde + * @license http://www.fsf.org/copyleft/lgpl.html LGPL * @package Cache */ -class Horde_Cache_Sql extends Horde_Cache +class Horde_Cache_Storage_Sql extends Horde_Cache_Storage { /** * Handle for the current database connection. @@ -85,7 +85,7 @@ class Horde_Cache_Sql extends Horde_Cache /** */ - protected function _get($key, $lifetime) + public function get($key, $lifetime) { $okey = $key; $key = hash('md5', $key); @@ -127,7 +127,7 @@ class Horde_Cache_Sql extends Horde_Cache /** */ - protected function _set($key, $data, $lifetime) + public function _set($key, $data, $lifetime) { $okey = $key; $key = hash('md5', $key); @@ -137,7 +137,7 @@ class Horde_Cache_Sql extends Horde_Cache // 0 lifetime indicates the object should not be GC'd. $expiration = ($lifetime === 0) ? 0 - : $this->_getLifetime($lifetime) + $timestamp; + : ($lifetime + $timestamp); if ($this->_logger) { $this->_logger->log(sprintf('Cache set: %s (Id %s set at %d expires at %d)', $okey, $key, $timestamp, $expiration), 'DEBUG'); @@ -164,16 +164,8 @@ class Horde_Cache_Sql extends Horde_Cache } /** - * Checks if a given key exists in the cache, valid for the given - * lifetime. - * - * @param string $key Cache key to check. - * @param integer $lifetime Maximum age of the key in seconds or 0 for - * any object. - * - * @return boolean Existence. */ - public function exists($key, $lifetime = 1) + public function exists($key, $lifetime) { $okey = $key; $key = hash('md5', $key); @@ -211,11 +203,6 @@ class Horde_Cache_Sql extends Horde_Cache } /** - * Expire any existing data for the given key. - * - * @param string $key Cache key to expire. - * - * @return boolean Success or failure. */ public function expire($key) { diff --git a/framework/Cache/lib/Horde/Cache/Stack.php b/framework/Cache/lib/Horde/Cache/Storage/Stack.php similarity index 73% rename from framework/Cache/lib/Horde/Cache/Stack.php rename to framework/Cache/lib/Horde/Cache/Storage/Stack.php index 55bc6bac5..6362a61c2 100644 --- a/framework/Cache/lib/Horde/Cache/Stack.php +++ b/framework/Cache/lib/Horde/Cache/Storage/Stack.php @@ -1,8 +1,8 @@ * @category Horde + * @license http://www.fsf.org/copyleft/lgpl.html LGPL * @package Cache */ -class Horde_Cache_Stack extends Horde_Cache +class Horde_Cache_Storage_Stack extends Horde_Cache_Storage { /** * Stack of cache drivers. @@ -27,7 +28,7 @@ class Horde_Cache_Stack extends Horde_Cache * * @param array $params Parameters: *
-     * 'stack' - (array) [REQUIRED] An array of Cache instances to loop
+     * 'stack' - (array) [REQUIRED] An array of storage instances to loop
      *           through, in order of priority. The last entry is considered
      *           the 'master' driver, for purposes of writes.
      * 
@@ -40,14 +41,14 @@ class Horde_Cache_Stack extends Horde_Cache throw new InvalidArgumentException('Missing stack parameter.'); } $this->_stack[] = $params['stack']; - unset($params['stack']); + parent::__construct($params); } /** */ - protected function _get($key, $lifetime) + public function get($key, $lifetime) { foreach ($this->_stack as $val) { $result = $val->get($key, $lifetime); @@ -61,7 +62,7 @@ class Horde_Cache_Stack extends Horde_Cache /** */ - protected function _set($key, $data, $lifetime) + public function set($key, $data, $lifetime) { /* Do writes in *reverse* order - it is OK if a write to one of the * non-master backends fails. */ @@ -82,15 +83,8 @@ class Horde_Cache_Stack extends Horde_Cache } /** - * Checks if a given key exists in the cache, valid for the given - * lifetime. - * - * @param string $key Cache key to check. - * @param integer $lifetime Lifetime of the key in seconds. - * - * @return boolean Existence. */ - public function exists($key, $lifetime = 1) + public function exists($key, $lifetime) { foreach ($this->_stack as $val) { $result = $val->exists($key, $lifetime); @@ -103,11 +97,6 @@ class Horde_Cache_Stack extends Horde_Cache } /** - * Expire any existing data for the given key. - * - * @param string $key Cache key to expire. - * - * @return boolean Success or failure. */ public function expire($key) { diff --git a/framework/Cache/lib/Horde/Cache/Xcache.php b/framework/Cache/lib/Horde/Cache/Storage/Xcache.php similarity index 69% rename from framework/Cache/lib/Horde/Cache/Xcache.php rename to framework/Cache/lib/Horde/Cache/Storage/Xcache.php index 2366bfa81..0c71248b7 100644 --- a/framework/Cache/lib/Horde/Cache/Xcache.php +++ b/framework/Cache/lib/Horde/Cache/Storage/Xcache.php @@ -1,7 +1,6 @@ * @@ -10,13 +9,14 @@ * * @author Duck * @category Horde + * @license http://www.fsf.org/copyleft/lgpl.html LGPL * @package Cache */ -class Horde_Cache_Xcache extends Horde_Cache +class Horde_Cache_Storage_Xcache extends Horde_Cache_Storage { /** */ - protected function _get($key, $lifetime) + public function get($key, $lifetime) { $key = $this->_params['prefix'] . $key; $this->_setExpire($key, $lifetime); @@ -29,25 +29,17 @@ class Horde_Cache_Xcache extends Horde_Cache /** */ - protected function _set($key, $data, $lifetime) + public function set($key, $data, $lifetime) { $key = $this->_params['prefix'] . $key; - $lifetime = $this->_getLifetime($lifetime); if (xcache_set($key . '_expire', time(), $lifetime)) { xcache_set($key, $data, $lifetime); } } /** - * Checks if a given key exists in the cache, valid for the given - * lifetime. - * - * @param string $key Cache key to check. - * @param integer $lifetime Lifetime of the key in seconds. - * - * @return boolean Existence. */ - public function exists($key, $lifetime = 1) + public function exists($key, $lifetime) { $key = $this->_params['prefix'] . $key; $this->_setExpire($key, $lifetime); @@ -55,11 +47,6 @@ class Horde_Cache_Xcache extends Horde_Cache } /** - * Expire any existing data for the given key. - * - * @param string $key Cache key to expire. - * - * @return boolean Success or failure. */ public function expire($key) { diff --git a/framework/Cache/package.xml b/framework/Cache/package.xml index 4cf25aa64..82c69399a 100644 --- a/framework/Cache/package.xml +++ b/framework/Cache/package.xml @@ -33,7 +33,8 @@ Performance Suite's content cache), memcached, or an SQL table. beta LGPL - * Add option to transparently compress cache data using lzf. + * Abstracted storage-specific code into 'Storage' drivers. + * Add option to transparently compress cache data using lzf. * Added Horde_Cache_Session::. * Horde_Cache::set() no longer returns a boolean result. * Added Horde_Cache_Exception::. @@ -47,17 +48,20 @@ Performance Suite's content cache), memcached, or an SQL table. - - + + + + + + + + + + + + - - - - - - - - + @@ -109,17 +113,18 @@ Performance Suite's content cache), memcached, or an SQL table. - - + + + + + + + + + + - - - - - - - - + diff --git a/framework/Core/lib/Horde/Core/Factory/Cache.php b/framework/Core/lib/Horde/Core/Factory/Cache.php index 28352df1e..b59baf4dd 100644 --- a/framework/Core/lib/Horde/Core/Factory/Cache.php +++ b/framework/Core/lib/Horde/Core/Factory/Cache.php @@ -46,51 +46,51 @@ class Horde_Core_Factory_Cache if (isset($GLOBALS['conf']['cache']['default_lifetime'])) { $params['lifetime'] = $GLOBALS['conf']['cache']['default_lifetime']; } + $params['logger'] = $injector->getInstance('Horde_Log_Logger'); - $logger = $injector->getInstance('Horde_Log_Logger'); - $params['logger'] = $logger; - - $base_params = $params; - - if (strcasecmp($driver, 'Memcache') === 0) { + $lc_driver = Horde_String::lower($driver); + switch ($lc_driver) { + case 'Memcache': $params['memcache'] = $injector->getInstance('Horde_Memcache'); - } elseif (strcasecmp($driver, 'Sql') === 0) { + break; + + case 'Sql': $params['db'] = $injector->getInstance('Horde_Db_Adapter'); + break; } + $storage = $this->_getStorage($driver, $params); + if (!empty($GLOBALS['conf']['cache']['use_memorycache']) && - ((strcasecmp($driver, 'Sql') === 0) || - (strcasecmp($driver, 'File') === 0))) { + in_array($lc_driver, array('File', 'Sql'))) { if (strcasecmp($GLOBALS['conf']['cache']['use_memorycache'], 'Memcache') === 0) { - $base_params['memcache'] = $injector->getInstance('Horde_Memcache'); + $params['memcache'] = $injector->getInstance('Horde_Memcache'); } - $class1 = $this->_driverToClassname($GLOBALS['conf']['cache']['use_memorycache']); - $class2 = $this->_driverToClassname($driver); - $params = array( + $cname = $this->_driverToClassname($GLOBALS['conf']['cache']['use_memorycache']); + $storage = new Horde_Cache_Storage_Stack(array( 'stack' => array( - new $class1($base_params), - new $class2($params), + $this->_getStorage($GLOBALS['conf']['cache']['use_memorycache'], $params), + $storage ) - ); - $driver = 'Stack'; + )); } - $classname = $this->_driverToClassname($driver); - return new $classname($params); + return new Horde_Cache($storage, $params); } /** */ - protected function _driverToClassname($driver) + protected function _getStorage($driver, $params) { $driver = ucfirst(basename($driver)); - $classname = 'Horde_Cache_' . $driver; + $classname = 'Horde_Cache_Storage_' . $driver; + if (!class_exists($classname)) { - $classname = 'Horde_Cache_Null'; + $classname = 'Horde_Cache_Storage_Null'; } - return $classname; + return new $classname($params); } } -- 2.11.0