Ticket #8023: Add transparent lzf compression to Horde_Session.
authorMichael M Slusarz <slusarz@curecanti.org>
Fri, 8 Oct 2010 21:34:12 +0000 (15:34 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Fri, 8 Oct 2010 23:05:11 +0000 (17:05 -0600)
Only compress arrays and objects at this time.

framework/Core/lib/Horde/Session.php
framework/Core/package.xml

index eacaaf9..b32b46b 100644 (file)
@@ -30,12 +30,23 @@ class Horde_Session implements ArrayAccess
     private $_cleansession = false;
 
     /**
+     * Use LZF compression?
+     * We use LZF compression on arrays and objects. Compressing numbers and
+     * most strings is not enought of an benefit for the overhead.
+     *
+     * @var boolean
+     */
+    private $_lzf = false;
+
+    /**
      * Constructor.
      *
      * @param boolean $start  Initiate the session?
      */
     public function __construct($start = true)
     {
+        $this->_lzf = Horde_Util::extensionExists('lzf');
+
         $this->setup($start);
     }
 
@@ -155,13 +166,19 @@ class Horde_Session implements ArrayAccess
             }
         }
 
-        if (isset($_SESSION['_s'][$ob->key])) {
-            return ($_SESSION['_s'][$ob->key] == 's')
-                ? @unserialize($_SESSION[$ob->app][$ob->name])
-                : json_decode($_SESSION[$ob->app][$ob->name], true);
+        $data = $_SESSION[$ob->app][$ob->name];
+
+        if (!isset($_SESSION['_s'][$ob->key])) {
+            return $data;
         }
 
-        return $_SESSION[$ob->app][$ob->name];
+        if ($this->_lzf) {
+            $data = lzf_decompress($data);
+        }
+
+        return ($_SESSION['_s'][$ob->key] == 's')
+            ? @unserialize($data)
+            : json_decode($data, true);
     }
 
     /**
@@ -175,14 +192,20 @@ class Horde_Session implements ArrayAccess
          * always convert to string representations so that the object/array
          * does not need to be rebuilt every time the session is reloaded. */
         if (is_object($value)) {
-            $_SESSION[$ob->app][$ob->name] = serialize($value);
+            $value = serialize($value);
+            if ($this->_lzf) {
+                $value = lzf_compress($value);
+            }
             $_SESSION['_s'][$ob->key] = 's';
         } elseif (is_array($value)) {
-            $_SESSION[$ob->app][$ob->name] = json_encode($value);
+            $value = json_encode($value);
+            if ($this->_lzf) {
+                $value = lzf_compress($value);
+            }
             $_SESSION['_s'][$ob->key] = 'j';
-        } else {
-            $_SESSION[$ob->app][$ob->name] = $value;
         }
+
+        $_SESSION[$ob->app][$ob->name] = $value;
     }
 
     /**
index 146103e..693a68c 100644 (file)
@@ -364,6 +364,11 @@ Application Framework.</description>
     <name>Tree</name>
     <channel>pear.horde.org</channel>
    </package>
+   <package>
+    <name>lzf</name>
+    <channel>pecl.php.net</channel>
+    <recommended>1.5.2</recommended>
+   </package>
   </optional>
  </dependencies>
  <usesrole>