From 40e6e5beecbd4b69b590afaecbde1adf662ea3dc Mon Sep 17 00:00:00 2001 From: Michael M Slusarz Date: Thu, 13 May 2010 02:49:09 -0600 Subject: [PATCH] Horde_Cache:: cleanups. Implement Horde_Cache::factory(), Horde_Cache_Exception::. phpdoc --- framework/Cache/lib/Horde/Cache.php | 57 ++++++++---------------- framework/Cache/lib/Horde/Cache/Base.php | 46 ++++++++++++++++++- framework/Cache/lib/Horde/Cache/Eaccelerator.php | 4 +- framework/Cache/lib/Horde/Cache/Exception.php | 16 +++++++ framework/Cache/lib/Horde/Cache/File.php | 40 +++++++++-------- framework/Cache/lib/Horde/Cache/Sql.php | 4 +- framework/Cache/lib/Horde/Cache/Xcache.php | 2 +- framework/Cache/package.xml | 11 ++++- framework/Core/lib/Horde/Core/Binder/Cache.php | 7 +-- 9 files changed, 116 insertions(+), 71 deletions(-) create mode 100644 framework/Cache/lib/Horde/Cache/Exception.php diff --git a/framework/Cache/lib/Horde/Cache.php b/framework/Cache/lib/Horde/Cache.php index b2a1b9f95..5141a4cca 100644 --- a/framework/Cache/lib/Horde/Cache.php +++ b/framework/Cache/lib/Horde/Cache.php @@ -14,50 +14,31 @@ * @category Horde * @package Cache */ -interface Horde_Cache +class Horde_Cache { /** - * Attempts to retrieve a cached object and return it to the - * caller. + * Attempts to return a concrete instance based on $driver. * - * @param string $key Object ID to query. - * @param integer $lifetime Lifetime of the object in seconds. + * @param string $driver The type of concrete subclass to + * return. The class name is based on the storage + * driver ($driver). The code is dynamically + * included. + * @param array $params A hash containing any additional configuration + * or connection parameters a subclass might need. * - * @return mixed Cached data, or false if none was found. + * @return Horde_Cache_Base The newly created concrete instance. + * @throws Horde_Cache_Exception */ - public function get($key, $lifetime = 1); + static public function factory($driver, array $params = array()) + { + $driver = ucfirst(basename($driver)); + $class = __CLASS__ . '_' . $driver; - /** - * Attempts to 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 null use the default Horde GC - * time. If 0 will not be GC'd. - * - * @return boolean True on success, false on failure. - */ - public function set($key, $data, $lifetime = null); + if (!class_exists($class)) { + $class = __CLASS__ . '_Null'; + } - /** - * 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 new $class($params); + } - /** - * 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/Base.php b/framework/Cache/lib/Horde/Cache/Base.php index 01c1505e2..9b3188c60 100644 --- a/framework/Cache/lib/Horde/Cache/Base.php +++ b/framework/Cache/lib/Horde/Cache/Base.php @@ -15,7 +15,6 @@ * @package Cache */ abstract class Horde_Cache_Base -implements Horde_Cache { /** * Cache parameters. @@ -73,6 +72,51 @@ implements Horde_Cache } /** + * Attempts to retrieve a cached object and return it to the + * caller. + * + * @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 = 1); + + /** + * Attempts to 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 null use the default Horde GC + * time. If 0 will not be GC'd. + * + * @return boolean True on success, false on failure. + */ + abstract public function set($key, $data, $lifetime = null); + + /** + * 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 = 1); + + /** + * 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); + + /** * Determine the default lifetime for data. * * @param mixed $lifetime The lifetime to use or null for default. diff --git a/framework/Cache/lib/Horde/Cache/Eaccelerator.php b/framework/Cache/lib/Horde/Cache/Eaccelerator.php index e177d952b..2f65f06e5 100644 --- a/framework/Cache/lib/Horde/Cache/Eaccelerator.php +++ b/framework/Cache/lib/Horde/Cache/Eaccelerator.php @@ -18,12 +18,12 @@ class Horde_Cache_Eaccelerator extends Horde_Cache_Base * Construct a new Horde_Cache object. * * @param array $params Parameter array. - * @throws Horde_Exception + * @throws Horde_Cache_Exception */ public function __construct($params = array()) { if (!function_exists('eaccelerator_gc')) { - throw new Horde_Exception('eAccelerator must be compiled with support for shared memory to use as caching backend.'); + throw new Horde_Cache_Exception('eAccelerator must be compiled with support for shared memory to use as caching backend.'); } parent::__construct($params); diff --git a/framework/Cache/lib/Horde/Cache/Exception.php b/framework/Cache/lib/Horde/Cache/Exception.php new file mode 100644 index 000000000..577d4aece --- /dev/null +++ b/framework/Cache/lib/Horde/Cache/Exception.php @@ -0,0 +1,16 @@ + + * @category Horde + * @package Cache + */ +class Horde_Cache_Exception extends Horde_Exception_Prior +{ +} diff --git a/framework/Cache/lib/Horde/Cache/File.php b/framework/Cache/lib/Horde/Cache/File.php index 7da3938e8..5b653700e 100644 --- a/framework/Cache/lib/Horde/Cache/File.php +++ b/framework/Cache/lib/Horde/Cache/File.php @@ -3,12 +3,6 @@ * The Horde_Cache_File:: class provides a filesystem implementation of the * Horde caching system. * - * Optional parameters:
- *   'dir'     The base directory to store the cache files in.
- *   'prefix'  The filename prefix to use for the cache files.
- *   'sub'     An integer. If non-zero, the number of subdirectories to
- *             create to store the file (i.e. PHP's session.save_path).
- * * Copyright 1999-2010 The Horde Project (http://www.horde.org/) * * See the enclosed file COPYING for license information (LGPL). If you @@ -52,21 +46,29 @@ class Horde_Cache_File extends Horde_Cache_Base /** * Construct a new Horde_Cache_File object. * - * @param array $params Parameter array. + * @param array $params Optional parameters: + *
+     * 'dir' - (string) The base directory to store the cache files in.
+     *         DEFAULT: System default
+     * 'prefix' - (string) The filename prefix to use for the cache files.
+     *            DEFAULT: 'cache_'
+     * 'sub' - (integer) If non-zero, the number of subdirectories to create
+     *         to store the file (i.e. PHP's session.save_path).
+     *         DEFAULT: 0
+     * 
*/ public function __construct($params = array()) { - if (!empty($params['dir']) && @is_dir($params['dir'])) { - $this->_dir = $params['dir']; - } else { - $this->_dir = Horde_Util::getTempDir(); + $this->_dir = (!empty($params['dir']) && @is_dir($params['dir'])) + ? $params['dir'] + : Horde_Util::getTempDir(); + + if (isset($params['prefix'])) { + $this->_prefix = $params['prefix']; } - foreach (array('prefix', 'sub') as $val) { - if (isset($params[$val])) { - $name = '_' . $val; - $this->$name = $params[$val]; - } + if (isset($params['sub'])) { + $this->_prefix = intval($params['sub']); } parent::__construct($params); @@ -104,7 +106,7 @@ class Horde_Cache_File extends Horde_Cache_Base try { $this->_gcDir($this->_dir, $excepts); - } catch (Horde_Exception $e) {} + } catch (Horde_Cache_Exception $e) {} $out = ''; foreach ($excepts as $key => $val) { @@ -277,13 +279,13 @@ class Horde_Cache_File extends Horde_Cache_Base /** * TODO * - * @throws Horde_Exception + * @throws Horde_Cache_Exception */ protected function _gcDir($dir, &$excepts) { $d = @dir($dir); if (!$d) { - throw new Horde_Exception('Permission denied to ' . $dir); + throw new Horde_Cache_Exception('Permission denied to ' . $dir); } $c_time = time(); diff --git a/framework/Cache/lib/Horde/Cache/Sql.php b/framework/Cache/lib/Horde/Cache/Sql.php index 1b11b333c..5e0c76940 100644 --- a/framework/Cache/lib/Horde/Cache/Sql.php +++ b/framework/Cache/lib/Horde/Cache/Sql.php @@ -65,12 +65,12 @@ class Horde_Cache_Sql extends Horde_Cache_Base * 'write_db' - (DB) The write DB instance. * * - * @throws Horde_Exception + * @throws Horde_Cache_Exception */ public function __construct($params = array()) { if (!isset($params['db'])) { - throw new Horde_Exception('Missing db parameter.'); + throw new Horde_Cache_Exception('Missing db parameter.'); } $this->_db = $params['db']; diff --git a/framework/Cache/lib/Horde/Cache/Xcache.php b/framework/Cache/lib/Horde/Cache/Xcache.php index cec76ab9e..c5bb54613 100644 --- a/framework/Cache/lib/Horde/Cache/Xcache.php +++ b/framework/Cache/lib/Horde/Cache/Xcache.php @@ -19,7 +19,7 @@ class Horde_Cache_Xcache extends Horde_Cache_Base * * @param array $params Configuration parameters: *
-     * 'prefix' - (string) TODO
+     * 'prefix' - (string) Key prefix.
      * 
*/ public function __construct($params = array()) diff --git a/framework/Cache/package.xml b/framework/Cache/package.xml index 04ae2fd7a..6ca6be4e7 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 - * Removed dependency on Horde Core. + * Added Horde_Cache_Exception::. + * Removed dependency on Horde Core. * Initial Horde 4 package. @@ -46,6 +47,7 @@ Performance Suite's content cache), memcached, or an SQL table. + @@ -65,7 +67,7 @@ Performance Suite's content cache), memcached, or an SQL table. 5.2.0 - 1.5.0 + 1.7.0 Util @@ -74,6 +76,10 @@ Performance Suite's content cache), memcached, or an SQL table. + DB + pear.php.net + + Log pear.horde.org @@ -93,6 +99,7 @@ Performance Suite's content cache), memcached, or an SQL table. + diff --git a/framework/Core/lib/Horde/Core/Binder/Cache.php b/framework/Core/lib/Horde/Core/Binder/Cache.php index bfcd0012b..bc850d112 100644 --- a/framework/Core/lib/Horde/Core/Binder/Cache.php +++ b/framework/Core/lib/Horde/Core/Binder/Cache.php @@ -43,12 +43,7 @@ class Horde_Core_Binder_Cache implements Horde_Injector_Binder $params['logger'] = $injector->getInstance('Horde_Log_Logger'); - $class = 'Horde_Cache_' . ucfirst(basename($driver)); - if (class_exists($class)) { - return new $class($params); - } - - throw new Horde_Exception('Class definition of ' . $class . ' not found.'); + return Horde_Cache::factory($driver, $params); } public function equals(Horde_Injector_Binder $binder) -- 2.11.0