Horde_Cache:: cleanups.
authorMichael M Slusarz <slusarz@curecanti.org>
Thu, 13 May 2010 08:49:09 +0000 (02:49 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Thu, 13 May 2010 18:02:17 +0000 (12:02 -0600)
Implement Horde_Cache::factory(), Horde_Cache_Exception::.
phpdoc

framework/Cache/lib/Horde/Cache.php
framework/Cache/lib/Horde/Cache/Base.php
framework/Cache/lib/Horde/Cache/Eaccelerator.php
framework/Cache/lib/Horde/Cache/Exception.php [new file with mode: 0644]
framework/Cache/lib/Horde/Cache/File.php
framework/Cache/lib/Horde/Cache/Sql.php
framework/Cache/lib/Horde/Cache/Xcache.php
framework/Cache/package.xml
framework/Core/lib/Horde/Core/Binder/Cache.php

index b2a1b9f..5141a4c 100644 (file)
  * @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);
 }
index 01c1505..9b3188c 100644 (file)
@@ -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.
index e177d95..2f65f06 100644 (file)
@@ -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 (file)
index 0000000..577d4ae
--- /dev/null
@@ -0,0 +1,16 @@
+<?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
+{
+}
index 7da3938..5b65370 100644 (file)
@@ -3,12 +3,6 @@
  * 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
@@ -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:
+     * <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);
@@ -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();
index 1b11b33..5e0c769 100644 (file)
@@ -65,12 +65,12 @@ class Horde_Cache_Sql extends Horde_Cache_Base
      * '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'];
 
index cec76ab..c5bb546 100644 (file)
@@ -19,7 +19,7 @@ class Horde_Cache_Xcache extends Horde_Cache_Base
      *
      * @param array $params  Configuration parameters:
      * <pre>
-     * 'prefix' - (string) TODO
+     * 'prefix' - (string) Key prefix.
      * </pre>
      */
     public function __construct($params = array())
index 04ae2fd..6ca6be4 100644 (file)
@@ -33,7 +33,8 @@ Performance Suite&apos;s content cache), memcached, or an SQL table.
   <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="/">
@@ -46,6 +47,7 @@ Performance Suite&apos;s content cache), memcached, or an SQL table.
       <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" />
@@ -65,7 +67,7 @@ Performance Suite&apos;s content cache), memcached, or an SQL table.
     <min>5.2.0</min>
    </php>
    <pearinstaller>
-    <min>1.5.0</min>
+    <min>1.7.0</min>
    </pearinstaller>
    <package>
     <name>Util</name>
@@ -74,6 +76,10 @@ Performance Suite&apos;s content cache), memcached, or an SQL table.
   </required>
   <optional>
    <package>
+    <name>DB</name>
+    <channel>pear.php.net</channel>
+   </package>
+   <package>
     <name>Log</name>
     <channel>pear.horde.org</channel>
    </package>
@@ -93,6 +99,7 @@ Performance Suite&apos;s content cache), memcached, or an SQL table.
    <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" />
index bfcd001..bc850d1 100644 (file)
@@ -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)