[dev] Caching of Ldap groups

Lukas Macura macura at opf.slu.cz
Mon Apr 28 08:46:34 UTC 2014


Hi to all,

we are implementing horde in campus and we use LDAP groups. There are 
big problems with speed, especially in kronolith because of
  ListGroups function which is called many times and each time it 
searches in LDAP.

We made small patch for ./framework/Group/lib/Horde/Group/Ldap.php which 
uses memcache for listGroups.  It is much more proof of concept, but it 
helped a lot. Before this patch, kronolith events loaded after 12seconds 
and it took about 50 LDAP searches. Now, everything loads up to 2 seconds.

Feel free to use/discuss...
Lukas Macura

diff -ruN ./framework/Group/lib/Horde/Group/Ldap.php /home/horde/pear/php/Horde/Group/Ldap.php
--- ./framework/Group/lib/Horde/Group/Ldap.php	2014-04-28 10:33:28.767236625 +0200
+++ /home/horde/pear/php/Horde/Group/Ldap.php	2014-04-25 15:45:21.326724539 +0200
@@ -2,7 +2,7 @@
  /**
   * This class provides an LDAP driver for the Horde group system.
   *
- * Copyright 2005-2013 Horde LLC (http://www.horde.org/)
+ * Copyright 2005-2014 Horde LLC (http://www.horde.org/)
   *
   * See the enclosed file COPYING for license information (LGPL). If you
   * did not receive this file, see http://www.horde.org/licenses/lgpl21.
@@ -35,6 +35,8 @@
       * @var Horde_Ldap_Filter
       */
      protected $_filter;
+
+    protected $_memcache;
  
      /**
       * Constructor.
@@ -82,6 +84,11 @@
          }
  
          $this->_params = $params;
+
+        $params = Horde::getDriverConfig('hashtable', $driver);
+        $this->_memcache = new Horde_HashTable_Memcache(array(
+                    'memcache' => new Horde_Memcache($params)));
+        unset($params['hashtable']);
      }
  
      /**
@@ -370,6 +377,33 @@
              throw new Horde_Group_Exception($e);
          }
      }
+
+    /**
+     */
+    public function read($id)
+    {
+        //$this->_memcache->lock($id);
+        $result = $this->_memcache->get($id);
+
+        //if ($result === false) {
+        //        $this->_memcache->unlock($id);
+        //    }
+
+        return $result;
+    }
+
+    /**
+     */
+    public function write($id, $data)
+    {
+
+        if (!$this->_memcache->set($id, $data)) {
+            return false;
+        }
+
+        return true;
+    }
+
  
      /**
       * Returns a list of groups a user belongs to.
@@ -381,6 +415,12 @@
       */
      public function listGroups($user)
      {
+        $id="listGroup.$user";
+
+        $result=$this->read($id);
+        if ($result) {
+            return($result);
+        }
          $attr = $this->_params['gid'];
          try {
              if (!empty($this->_params['attrisdn'])) {
@@ -398,6 +438,7 @@
          foreach ($search->sortedAsArray(array($attr)) as $entry) {
              $entries[$entry['dn']] = $entry[$attr][0];
          }
+        $this->write($id,$entries,7200);
          return $entries;
      }



More information about the dev mailing list