Implement Horde_Cache::factory(), Horde_Cache_Exception::.
phpdoc
* @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);
}
* @package Cache
*/
abstract class Horde_Cache_Base
-implements Horde_Cache
{
/**
* Cache parameters.
}
/**
+ * 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.
* 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);
--- /dev/null
+<?php
+/**
+ * Exception handler for the Horde_Cache package.
+ *
+ * Copyright 2010 The Horde Project (http://www.horde.org/)
+ *
+ * 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 <slusarz@horde.org>
+ * @category Horde
+ * @package Cache
+ */
+class Horde_Cache_Exception extends Horde_Exception_Prior
+{
+}
* The Horde_Cache_File:: class provides a filesystem implementation of the
* Horde caching system.
*
- * Optional parameters:<pre>
- * '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).</pre>
- *
* Copyright 1999-2010 The Horde Project (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL). If you
/**
* Construct a new Horde_Cache_File object.
*
- * @param array $params Parameter array.
+ * @param array $params Optional parameters:
+ * <pre>
+ * '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
+ * </pre>
*/
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);
try {
$this->_gcDir($this->_dir, $excepts);
- } catch (Horde_Exception $e) {}
+ } catch (Horde_Cache_Exception $e) {}
$out = '';
foreach ($excepts as $key => $val) {
/**
* 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();
* 'write_db' - (DB) The write DB instance.
* </pre>
*
- * @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'];
*
* @param array $params Configuration parameters:
* <pre>
- * 'prefix' - (string) TODO
+ * 'prefix' - (string) Key prefix.
* </pre>
*/
public function __construct($params = array())
<api>beta</api>
</stability>
<license uri="http://www.gnu.org/copyleft/lesser.html">LGPL</license>
- <notes>* Removed dependency on Horde Core.
+ <notes>* Added Horde_Cache_Exception::.
+ * Removed dependency on Horde Core.
* Initial Horde 4 package.</notes>
<contents>
<dir name="/">
<file name="Apc.php" role="php" />
<file name="Base.php" role="php" />
<file name="Eaccelerator.php" role="php" />
+ <file name="Exception.php" role="php" />
<file name="File.php" role="php" />
<file name="Memcache.php" role="php" />
<file name="Mock.php" role="php" />
<min>5.2.0</min>
</php>
<pearinstaller>
- <min>1.5.0</min>
+ <min>1.7.0</min>
</pearinstaller>
<package>
<name>Util</name>
</required>
<optional>
<package>
+ <name>DB</name>
+ <channel>pear.php.net</channel>
+ </package>
+ <package>
<name>Log</name>
<channel>pear.horde.org</channel>
</package>
<install name="lib/Horde/Cache/Apc.php" as="Horde/Cache/Apc.php" />
<install name="lib/Horde/Cache/Base.php" as="Horde/Cache/Base.php" />
<install name="lib/Horde/Cache/Eaccelerator.php" as="Horde/Cache/Eaccelerator.php" />
+ <install name="lib/Horde/Cache/Exception.php" as="Horde/Cache/Exception.php" />
<install name="lib/Horde/Cache/File.php" as="Horde/Cache/File.php" />
<install name="lib/Horde/Cache/Memcache.php" as="Horde/Cache/Memcache.php" />
<install name="lib/Horde/Cache/Mock.php" as="Horde/Cache/Mock.php" />
$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)