From: Michael M Slusarz Date: Mon, 4 Oct 2010 18:51:23 +0000 (-0600) Subject: Add option to transparently compress cache data using lzf. X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=b560a1fecfa8994f68a65a5dabd0291dd345e170;p=horde.git Add option to transparently compress cache data using lzf. --- diff --git a/framework/Cache/lib/Horde/Cache.php b/framework/Cache/lib/Horde/Cache.php index 6cbc88908..b443fed15 100644 --- a/framework/Cache/lib/Horde/Cache.php +++ b/framework/Cache/lib/Horde/Cache.php @@ -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: *
+     * '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;
     }
+
 }
diff --git a/framework/Cache/lib/Horde/Cache/Apc.php b/framework/Cache/lib/Horde/Cache/Apc.php
index 84b41e3d9..324ceb4b6 100644
--- a/framework/Cache/lib/Horde/Cache/Apc.php
+++ b/framework/Cache/lib/Horde/Cache/Apc.php
@@ -15,15 +15,8 @@
 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');
         }
     }
+
 }
diff --git a/framework/Cache/lib/Horde/Cache/Eaccelerator.php b/framework/Cache/lib/Horde/Cache/Eaccelerator.php
index 6e1501efb..2a911f8ad 100644
--- a/framework/Cache/lib/Horde/Cache/Eaccelerator.php
+++ b/framework/Cache/lib/Horde/Cache/Eaccelerator.php
@@ -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');
         }
     }
+
 }
diff --git a/framework/Cache/lib/Horde/Cache/Exception.php b/framework/Cache/lib/Horde/Cache/Exception.php
index 577d4aece..189a0d878 100644
--- a/framework/Cache/lib/Horde/Cache/Exception.php
+++ b/framework/Cache/lib/Horde/Cache/Exception.php
@@ -1,6 +1,6 @@
 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();
     }
+
 }
diff --git a/framework/Cache/lib/Horde/Cache/Memcache.php b/framework/Cache/lib/Horde/Cache/Memcache.php
index 198fc2f86..bb90bc82b 100644
--- a/framework/Cache/lib/Horde/Cache/Memcache.php
+++ b/framework/Cache/lib/Horde/Cache/Memcache.php
@@ -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);
     }
+
 }
diff --git a/framework/Cache/lib/Horde/Cache/Mock.php b/framework/Cache/lib/Horde/Cache/Mock.php
index 8d256ab7f..234997d4d 100644
--- a/framework/Cache/lib/Horde/Cache/Mock.php
+++ b/framework/Cache/lib/Horde/Cache/Mock.php
@@ -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 
+ * @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]);
     }
+
 }
diff --git a/framework/Cache/lib/Horde/Cache/Null.php b/framework/Cache/lib/Horde/Cache/Null.php
index 93af1647c..a332f858d 100644
--- a/framework/Cache/lib/Horde/Cache/Null.php
+++ b/framework/Cache/lib/Horde/Cache/Null.php
@@ -15,33 +15,16 @@
 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;
     }
+
 }
diff --git a/framework/Cache/lib/Horde/Cache/Session.php b/framework/Cache/lib/Horde/Cache/Session.php
index ea8c8663f..6aeb6b5c8 100644
--- a/framework/Cache/lib/Horde/Cache/Session.php
+++ b/framework/Cache/lib/Horde/Cache/Session.php
@@ -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;
     }
+
 }
diff --git a/framework/Cache/lib/Horde/Cache/Sql.php b/framework/Cache/lib/Horde/Cache/Sql.php
index a907855d5..ead301c0d 100644
--- a/framework/Cache/lib/Horde/Cache/Sql.php
+++ b/framework/Cache/lib/Horde/Cache/Sql.php
@@ -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;
     }
+
 }
diff --git a/framework/Cache/lib/Horde/Cache/Stack.php b/framework/Cache/lib/Horde/Cache/Stack.php
index 2385a96c3..55bc6bac5 100644
--- a/framework/Cache/lib/Horde/Cache/Stack.php
+++ b/framework/Cache/lib/Horde/Cache/Stack.php
@@ -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;
     }
+
 }
diff --git a/framework/Cache/lib/Horde/Cache/Xcache.php b/framework/Cache/lib/Horde/Cache/Xcache.php
index b4635c916..2366bfa81 100644
--- a/framework/Cache/lib/Horde/Cache/Xcache.php
+++ b/framework/Cache/lib/Horde/Cache/Xcache.php
@@ -15,14 +15,8 @@
 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);
         }
     }
+
 }
diff --git a/framework/Cache/package.xml b/framework/Cache/package.xml
index 7b622dfc4..4cf25aa64 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
- * Added Horde_Cache_Session::.
+ * 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's content cache), memcached, or an SQL table.
     Memcache
     pear.horde.org
    
+   
+    lzf
+    pecl.php.net
+    1.5.2
+   
    
     apc