Add option to transparently compress cache data using lzf.
authorMichael M Slusarz <slusarz@curecanti.org>
Mon, 4 Oct 2010 18:51:23 +0000 (12:51 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Mon, 4 Oct 2010 19:11:11 +0000 (13:11 -0600)
13 files changed:
framework/Cache/lib/Horde/Cache.php
framework/Cache/lib/Horde/Cache/Apc.php
framework/Cache/lib/Horde/Cache/Eaccelerator.php
framework/Cache/lib/Horde/Cache/Exception.php
framework/Cache/lib/Horde/Cache/File.php
framework/Cache/lib/Horde/Cache/Memcache.php
framework/Cache/lib/Horde/Cache/Mock.php
framework/Cache/lib/Horde/Cache/Null.php
framework/Cache/lib/Horde/Cache/Session.php
framework/Cache/lib/Horde/Cache/Sql.php
framework/Cache/lib/Horde/Cache/Stack.php
framework/Cache/lib/Horde/Cache/Xcache.php
framework/Cache/package.xml

index 6cbc889..b443fed 100644 (file)
@@ -22,6 +22,7 @@ abstract class Horde_Cache
      * @var array
      */
     protected $_params = array(
+        'compress' => false,
         'lifetime' => 86400,
         'prefix' => '',
     );
@@ -38,6 +39,9 @@ abstract class Horde_Cache
      *
      * @param array $params  Parameter array:
      * <pre>
+     * 'compress' - (boolean) Compress data? Requires the 'lzf' PECL
+     *              extension.
+     *              DEFAULT: false
      * 'lifetime' - (integer) Lifetime of data, in seconds.
      *              DEFAULT: 86400 seconds
      * 'logger' - (Horde_Log_Logger) Log object to use for log/debug messages.
@@ -50,6 +54,10 @@ abstract class Horde_Cache
             unset($params['logger']);
         }
 
+        if (!empty($params['compress']) && !extension_loaded('lzf')) {
+            unset($params['compress']);
+        }
+
         $this->_params = array_merge($this->_params, $params);
     }
 
@@ -73,18 +81,30 @@ abstract class Horde_Cache
     }
 
     /**
-     * Attempts to retrieve a cached object and return it to the
-     * caller.
+     * 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 = 1);
+    public function get($key, $lifetime = 1)
+    {
+        $res = $this->_get($key, $lifetime);
+
+        return ($this->_params['compress'] && ($res !== false))
+            // lzf_decompress() returns false on error
+            ? lzf_decompress($res)
+            : $res;
+    }
 
     /**
-     * Attempts to store an object in the 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.
@@ -95,7 +115,23 @@ abstract class Horde_Cache
      *
      * @throws Horde_Cache_Exception
      */
-    abstract public function set($key, $data, $lifetime = null);
+    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);
+        }
+
+        $res = $this->_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
@@ -128,4 +164,5 @@ abstract class Horde_Cache
     {
         return is_null($lifetime) ? $this->_params['lifetime'] : $lifetime;
     }
+
 }
index 84b41e3..324ceb4 100644 (file)
 class Horde_Cache_Apc extends Horde_Cache
 {
     /**
-     * Attempts to retrieve a piece of cached data and return it to
-     * the caller.
-     *
-     * @param string $key        Cache key to fetch.
-     * @param integer $lifetime  Lifetime of the key in seconds.
-     *
-     * @return mixed  Cached data, or false if none was found.
      */
-    public function get($key, $lifetime = 1)
+    protected function _get($key, $lifetime)
     {
         $key = $this->_params['prefix'] . $key;
         $this->_setExpire($key, $lifetime);
@@ -31,19 +24,9 @@ class Horde_Cache_Apc extends Horde_Cache
     }
 
     /**
-     * Attempts to store an object to the cache.
-     *
-     * @param string $key        Cache key (identifier).
-     * @param string $data       Data to store in the cache.
-     * @param integer $lifetime  Data lifetime.
-     *
-     * @throws Horde_Cache_Exception
      */
-    public function set($key, $data, $lifetime = null)
+    protected function _set($key, $data, $lifetime)
     {
-        if (!is_string($data)) {
-            throw new Horde_Cache_Exception('Data must be a string.');
-        }
         $key = $this->_params['prefix'] . $key;
         $lifetime = $this->_getLifetime($lifetime);
         if (apc_store($key . '_expire', time(), $lifetime)) {
@@ -64,7 +47,7 @@ class Horde_Cache_Apc extends Horde_Cache
     {
         $key = $this->_params['prefix'] . $key;
         $this->_setExpire($key, $lifetime);
-        return (apc_fetch($key) === false) ? false : true;
+        return (apc_fetch($key) !== false);
     }
 
     /**
@@ -104,4 +87,5 @@ class Horde_Cache_Apc extends Horde_Cache
             apc_delete($key . '_expire');
         }
     }
+
 }
index 6e1501e..2a911f8 100644 (file)
@@ -18,6 +18,7 @@ class Horde_Cache_Eaccelerator extends Horde_Cache
      * Construct a new Horde_Cache object.
      *
      * @param array $params  Parameter array.
+     *
      * @throws Horde_Cache_Exception
      */
     public function __construct($params = array())
@@ -30,14 +31,8 @@ class Horde_Cache_Eaccelerator extends Horde_Cache
     }
 
     /**
-     * Attempts to retrieve a piece of cached data and return it to the caller.
-     *
-     * @param string $key        Cache key to fetch.
-     * @param integer $lifetime  Lifetime of the key in seconds.
-     *
-     * @return mixed  Cached data, or false if none was found.
      */
-    public function get($key, $lifetime = 1)
+    protected function _get($key, $lifetime)
     {
         $key = $this->_params['prefix'] . $key;
         $this->_setExpire($key, $lifetime);
@@ -45,19 +40,9 @@ class Horde_Cache_Eaccelerator extends Horde_Cache
     }
 
     /**
-     * Attempts to store an object to the cache.
-     *
-     * @param string $key        Cache key (identifier).
-     * @param string $data       Data to store in the cache.
-     * @param integer $lifetime  Data lifetime.
-     *
-     * @throws Horde_Cache_Exception
      */
-    public function set($key, $data, $lifetime = null)
+    protected function _set($key, $data, $lifetime)
     {
-        if (!is_string($data)) {
-            throw new Horde_Cache_Exception('Data must be a string.');
-        }
         $key = $this->_params['prefix'] . $key;
         $lifetime = $this->_getLifetime($lifetime);
         if (eaccelerator_put($key . '_expire', time(), $lifetime)) {
@@ -119,4 +104,5 @@ class Horde_Cache_Eaccelerator extends Horde_Cache
             eaccelerator_rm($key . '_expire');
         }
     }
+
 }
index 577d4ae..189a0d8 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * Exception handler for the Horde_Cache package.
+ * Exception handler for the horde/Cache package.
  *
  * Copyright 2010 The Horde Project (http://www.horde.org/)
  *
@@ -11,6 +11,4 @@
  * @category Horde
  * @package  Cache
  */
-class Horde_Cache_Exception extends Horde_Exception_Prior
-{
-}
+class Horde_Cache_Exception extends Horde_Exception_Prior {}
index b7022a5..7eef0b9 100644 (file)
@@ -103,15 +103,8 @@ class Horde_Cache_File extends Horde_Cache
     }
 
     /**
-     * Attempts to retrieve cached data from the filesystem and return it to
-     * the caller.
-     *
-     * @param string $key        Cache key to fetch.
-     * @param integer $lifetime  Lifetime of the data in seconds.
-     *
-     * @return mixed  Cached data, or false if none was found.
      */
-    public function get($key, $lifetime = 1)
+    protected function _get($key, $lifetime)
     {
         if (!$this->exists($key, $lifetime)) {
             /* Nothing cached, return failure. */
@@ -123,28 +116,14 @@ class Horde_Cache_File extends Horde_Cache
         if (!$size) {
             return '';
         }
-        $old_error = error_reporting(0);
-        $data = file_get_contents($filename);
-        error_reporting($old_error);
 
-        return $data;
+        return @file_get_contents($filename);
     }
 
     /**
-     * Attempts to store data to the filesystem.
-     *
-     * @param string $key        Cache key.
-     * @param string $data       Data to store in the cache.
-     * @param integer $lifetime  Data lifetime.
-     *
-     * @throws Horde_Cache_Exception
      */
-    public function set($key, $data, $lifetime = null)
+    protected function _set($key, $data, $lifetime)
     {
-        if (!is_string($data)) {
-            throw new Horde_Cache_Exception('Data must be a string.');
-        }
-
         $filename = $this->_keyToFile($key, true);
         $tmp_file = Horde_Util::getTempFile('HordeCache', true, $this->_dir);
         if (isset($this->_params['umask'])) {
@@ -185,18 +164,15 @@ class Horde_Cache_File extends Horde_Cache
 
         /* Key exists in the cache */
         if (file_exists($filename)) {
-            /* 0 means no expire. */
-            if ($lifetime == 0) {
-                return true;
-            }
-
-            /* If the file was been created after the supplied value,
+            /* 0 means no expire.
+             * Also, If the file was been created after the supplied value,
              * the data is valid (fresh). */
-            if (time() - $lifetime <= filemtime($filename)) {
+            if (($lifetime == 0) ||
+                (time() - $lifetime <= filemtime($filename))) {
                 return true;
-            } else {
-                @unlink($filename);
             }
+
+            @unlink($filename);
         }
 
         return false;
@@ -299,4 +275,5 @@ class Horde_Cache_File extends Horde_Cache
         }
         $d->close();
     }
+
 }
index 198fc2f..bb90bc8 100644 (file)
@@ -60,15 +60,8 @@ class Horde_Cache_Memcache extends Horde_Cache
     }
 
     /**
-     * Attempts to retrieve cached data from the memcache and return it to
-     * the caller.
-     *
-     * @param string $key        Cache key to fetch.
-     * @param integer $lifetime  Lifetime of the data in seconds.
-     *
-     * @return mixed  Cached data, or false if none was found.
      */
-    public function get($key, $lifetime = 1)
+    protected function _get($key, $lifetime)
     {
         $key = $this->_params['prefix'] . $key;
         if (isset($this->_expirecache[$key])) {
@@ -99,20 +92,9 @@ class Horde_Cache_Memcache extends Horde_Cache
     }
 
     /**
-     * Attempts to store data to the memcache.
-     *
-     * @param string $key        Cache key.
-     * @param mixed $data        Data to store in the cache.
-     * @param integer $lifetime  Data lifetime.
-     *
-     * @throws Horde_Cache_Exception
      */
-    public function set($key, $data, $lifetime = null)
+    protected function _set($key, $data, $lifetime)
     {
-        if (!is_string($data)) {
-            throw new Horde_Cache_Exception('Data must be a string.');
-        }
-
         $key = $this->_params['prefix'] . $key;
         $lifetime = $this->_getLifetime($lifetime);
 
@@ -151,4 +133,5 @@ class Horde_Cache_Memcache extends Horde_Cache
 
         return $this->_memcache->delete($key);
     }
+
 }
index 8d256ab..234997d 100644 (file)
@@ -9,11 +9,11 @@
  * See the enclosed file COPYING for license information (LGPL). If you
  * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
  *
- * @category Horde
- * @package  Cache
  * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @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
 {
@@ -35,14 +35,8 @@ class Horde_Cache_Mock extends Horde_Cache
     }
 
     /**
-     * Attempts to retrieve a piece of cached data and return it to the caller.
-     *
-     * @param string $key        Cache key to fetch.
-     * @param integer $lifetime  Lifetime of the key in seconds.
-     *
-     * @return mixed  Cached data, or false if none was found.
      */
-    public function get($key, $lifetime = 1)
+    protected function _get($key, $lifetime)
     {
         return isset($this->_cache[$key])
             ? $this->_cache[$key]
@@ -50,19 +44,9 @@ class Horde_Cache_Mock extends Horde_Cache
     }
 
     /**
-     * Attempts to store an object to the cache.
-     *
-     * @param string $key        Cache key (identifier).
-     * @param string $data       Data to store in the cache.
-     * @param integer $lifetime  Data lifetime.
-     *
-     * @throws Horde_Cache_Exception
      */
-    public function set($key, $data, $lifetime = null)
+    protected function _set($key, $data, $lifetime)
     {
-        if (!is_string($data)) {
-            throw new Horde_Cache_Exception('Data must be a string.');
-        }
         $this->_cache[$key] = $data;
     }
 
@@ -91,4 +75,5 @@ class Horde_Cache_Mock extends Horde_Cache
     {
         unset($this->_cache[$key]);
     }
+
 }
index 93af164..a332f85 100644 (file)
 class Horde_Cache_Null extends Horde_Cache
 {
     /**
-     * Attempts to retrieve a piece of cached data and return it to
-     * the caller.
-     *
-     * @param string $key        Cache key to fetch.
-     * @param integer $lifetime  Lifetime of the key in seconds.
-     *
-     * @return mixed  Cached data, or false if none was found.
      */
-    public function get($key, $lifetime = 1)
+    protected function _get($key, $lifetime)
     {
         return false;
     }
 
     /**
-     * Attempts to store an object to the cache.
-     *
-     * @param string $key        Cache key (identifier).
-     * @param string $data       Data to store in the cache.
-     * @param integer $lifetime  Data lifetime.
-     *
-     * @throws Horde_Cache_Exception
      */
-    public function set($key, $data, $lifetime = null)
+    protected function _set($key, $data, $lifetime)
     {
-        if (!is_string($data)) {
-            throw new Horde_Cache_Exception('Data must be a string.');
-        }
     }
 
     /**
@@ -69,4 +52,5 @@ class Horde_Cache_Null extends Horde_Cache
     {
         return false;
     }
+
 }
index ea8c866..6aeb6b5 100644 (file)
@@ -46,37 +46,18 @@ class Horde_Cache_Session extends Horde_Cache
     }
 
     /**
-     * Attempts to retrieve a piece of cached data and return it to
-     * the caller.
-     *
-     * @param string $key        Cache key to fetch.
-     * @param integer $lifetime  Lifetime of the key in seconds.
-     *
-     * @return mixed  Cached data, or false if none was found.
      */
-    public function get($key, $lifetime = 1)
+    protected function _get($key, $lifetime)
     {
-        if ($this->exists($key, $lifetime)) {
-            return $this->_sess[$key]['d'];
-        }
-
-        return false;
+        return $this->exists($key, $lifetime)
+            ? $this->_sess[$key]['d']
+            : false;
     }
 
     /**
-     * Attempts to store an object to the cache.
-     *
-     * @param string $key        Cache key (identifier).
-     * @param string $data       Data to store in the cache.
-     * @param integer $lifetime  Data lifetime.
-     *
-     * @throws Horde_Cache_Exception
      */
-    public function set($key, $data, $lifetime = null)
+    protected function _set($key, $data, $lifetime)
     {
-        if (!is_string($data)) {
-            throw new Horde_Cache_Exception('Data must be a string.');
-        }
         $this->_sess[$key] = array(
             'd' => $data,
             'l' => $this->_getLifetime($lifetime)
@@ -123,4 +104,5 @@ class Horde_Cache_Session extends Horde_Cache
 
         return false;
     }
+
 }
index a907855..ead301c 100644 (file)
@@ -84,15 +84,8 @@ class Horde_Cache_Sql extends Horde_Cache
     }
 
     /**
-     * Attempts to retrieve cached data.
-     *
-     * @param string $key        Cache key to fetch.
-     * @param integer $lifetime  Maximum age of the data in seconds or
-     *                           0 for any object.
-     *
-     * @return mixed  Cached data, or false if none was found.
      */
-    public function get($key, $lifetime = 1)
+    protected function _get($key, $lifetime)
     {
         $okey = $key;
         $key = hash('md5', $key);
@@ -133,21 +126,9 @@ class Horde_Cache_Sql extends Horde_Cache
     }
 
     /**
-     * Attempts to store data.
-     *
-     * @param string $key        Cache key.
-     * @param string $data       Data to store in the cache.
-     * @param integer $lifetime  Maximum data life span or 0 for a
-     *                           non-expiring object.
-     *
-     * @throws Horde_Cache_Exception
      */
-    public function set($key, $data, $lifetime = null)
+    protected function _set($key, $data, $lifetime)
     {
-        if (!is_string($data)) {
-            throw new Horde_Cache_Exception('Data must be a string.');
-        }
-
         $okey = $key;
         $key = hash('md5', $key);
 
@@ -252,4 +233,5 @@ class Horde_Cache_Sql extends Horde_Cache
 
         return true;
     }
+
 }
index 2385a96..55bc6ba 100644 (file)
@@ -46,44 +46,23 @@ class Horde_Cache_Stack extends 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.
      */
-    public function get($key, $lifetime = 1)
+    protected function _get($key, $lifetime)
     {
         foreach ($this->_stack as $val) {
             $result = $val->get($key, $lifetime);
             if ($result !== false) {
-                break;
+                return $result;
             }
         }
 
-        return $result;
+        return false;
     }
 
     /**
-     * Attempts to store an object in the cache.
-     *
-     * @param string $key        Object ID used as the caching key.
-     * @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)
+    protected function _set($key, $data, $lifetime)
     {
-        if (!is_string($data)) {
-            throw new Horde_Cache_Exception('Data must be a string.');
-        }
-
         /* Do writes in *reverse* order - it is OK if a write to one of the
          * non-master backends fails. */
         $master = true;
@@ -145,4 +124,5 @@ class Horde_Cache_Stack extends Horde_Cache
 
         return $success;
     }
+
 }
index b4635c9..2366bfa 100644 (file)
 class Horde_Cache_Xcache extends Horde_Cache
 {
     /**
-     * Attempts to retrieve a piece of cached data and return it to the caller.
-     *
-     * @param string $key        Cache key to fetch.
-     * @param integer $lifetime  Lifetime of the key in seconds.
-     *
-     * @return mixed  Cached data, or false if none was found.
      */
-    public function get($key, $lifetime = 1)
+    protected function _get($key, $lifetime)
     {
         $key = $this->_params['prefix'] . $key;
         $this->_setExpire($key, $lifetime);
@@ -34,19 +28,9 @@ class Horde_Cache_Xcache extends Horde_Cache
     }
 
     /**
-     * Attempts to store an object to the cache.
-     *
-     * @param string $key        Cache key (identifier).
-     * @param string $data       Data to store in the cache.
-     * @param integer $lifetime  Data lifetime.
-     *
-     * @throws Horde_Cache_Exception
      */
-    public function set($key, $data, $lifetime = null)
+    protected function _set($key, $data, $lifetime)
     {
-        if (!is_string($data)) {
-            throw new Horde_Cache_Exception('Data must be a string.');
-        }
         $key = $this->_params['prefix'] . $key;
         $lifetime = $this->_getLifetime($lifetime);
         if (xcache_set($key . '_expire', time(), $lifetime)) {
@@ -106,4 +90,5 @@ class Horde_Cache_Xcache extends Horde_Cache
             xcache_unset($key);
         }
     }
+
 }
index 7b622df..4cf25aa 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>* Added Horde_Cache_Session::.
+ <notes>* 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::.
  * Removed dependency on Horde Core.
@@ -93,6 +94,11 @@ Performance Suite&apos;s content cache), memcached, or an SQL table.
     <name>Memcache</name>
     <channel>pear.horde.org</channel>
    </package>
+   <package>
+    <name>lzf</name>
+    <channel>pecl.php.net</channel>
+    <recommended>1.5.2</recommended>
+   </package>
    <extension>
     <name>apc</name>
    </extension>