Use Horde_Cache.
authorJan Schneider <jan@horde.org>
Mon, 20 Sep 2010 22:36:50 +0000 (00:36 +0200)
committerJan Schneider <jan@horde.org>
Mon, 20 Sep 2010 22:38:05 +0000 (00:38 +0200)
framework/Core/lib/Horde/Core/Factory/Ldap.php
framework/Ldap/lib/Horde/Ldap.php
framework/Ldap/lib/Horde/Ldap/SchemaCache.interface.php [deleted file]
framework/Ldap/lib/Horde/Ldap/Util.php

index 11a983d..aab4c57 100644 (file)
@@ -81,6 +81,7 @@ class Horde_Core_Factory_Ldap
         if (!isset($config['user'])) {
             $config['user'] = $config;
         }
+        $config['cache'] = $GLOBALS['injector']->getInstance('Horde_Cache');
 
         try {
             $this->_instances[$sig] = new Horde_Ldap($config);
index e02b94b..93d6136 100644 (file)
@@ -42,6 +42,7 @@ class Horde_Ldap
      * - min_backoff:    minimum reconnection delay period (in seconds).
      * - current_backof: initial reconnection delay period (in seconds).
      * - max_backoff:    maximum reconnection delay period (in seconds).
+     * - cache           a Horde_Cache instance for caching schema requests.
      *
      * @var array
      */
@@ -60,7 +61,8 @@ class Horde_Ldap
         'auto_reconnect'  => false,
         'min_backoff'     => 1,
         'current_backoff' => 1,
-        'max_backoff'     => 32);
+        'max_backoff'     => 32,
+        'cache'           => null);
 
     /**
      * List of hosts we try to establish a connection to.
@@ -1273,86 +1275,34 @@ class Horde_Ldap
      */
     public function schema($dn = null)
     {
-        /* If a schema caching object is registered, we use that to fetch
-         * a schema object.
-         * See registerSchemaCache() for more info on this.
-         * FIXME: Convert to Horde_Cache */
-        if ($this->_schema === null) {
-            if ($this->_schemaCache) {
-               $cached_schema = $this->_schemaCache->loadSchema();
-               if ($cached_schema instanceof Horde_Ldap_Schema) {
-                   $this->_schema = $cached_schema;
-               }
-            }
+        /* If a schema caching object is registered, we use that to fetch a
+         * schema object. */
+        $key = 'Horde_Ldap_Schema_' . md5(serialize(array($this->_config['hostspec'], $this->_config['port'], $dn)));
+        if (!$this->_schema && $this->_config['cache']) {
+            $this->_schema = $this->_config['cache']->get($key);
         }
 
-        /* Fetch schema, if not tried before and no cached version
-         * available.  If we are already fetching the schema, we will
-         * skip fetching. */
-        if ($this->_schema === null) {
-            /* Store a temporary error message so subsequent calls to
-             * schema() can detect that we are fetching the schema
-             * already. Otherwise we will get an infinite loop at
-             * Horde_Ldap_Schema. */
+        /* Fetch schema, if not tried before and no cached version available.
+         * If we are already fetching the schema, we will skip fetching. */
+        if (!$this->_schema) {
+            /* Store a temporary error message so subsequent calls to schema()
+             * can detect that we are fetching the schema already. Otherwise we
+             * will get an infinite loop at Horde_Ldap_Schema. */
             $this->_schema = new Horde_Ldap_Exception('Schema not initialized');
             $this->_schema = new Horde_Ldap_Schema($this, $dn);
 
-            /* If schema caching is active, advise the cache to store
-             * the schema. */
-            if ($this->_schemaCache) {
-                $this->_schemaCache->storeSchema($this->_schema);
+            /* If schema caching is active, advise the cache to store the
+             * schema. */
+            if ($this->_config['cache']) {
+                $this->_config['cache']->set($key, $this->_schema);
             }
         }
 
         if ($this->_schema instanceof Horde_Ldap_Exception) {
             throw $this->_schema;
         }
-        return $this->_schema;
-    }
 
-    /**
-     * Enable/disable persistent schema caching
-     *
-     * Sometimes it might be useful to allow your scripts to cache
-     * the schema information on disk, so the schema is not fetched
-     * every time the script runs which could make your scripts run
-     * faster.
-     *
-     * This method allows you to register a custom object that
-     * implements your schema cache. Please see the SchemaCache interface
-     * (SchemaCache.interface.php) for informations on how to implement this.
-     * To unregister the cache, pass null as $cache parameter.
-     *
-     * For ease of use, Horde_Ldap provides a simple file based cache
-     * which is used in the example below. You may use this, for example,
-     * to store the schema in a linux tmpfs which results in the schema
-     * beeing cached inside the RAM which allows nearly instant access.
-     * <code>
-     *    // Create the simple file cache object that comes along with Horde_Ldap
-     *    $mySchemaCache_cfg = array(
-     *      'path'    =>  '/tmp/Horde_Ldap_Schema.cache',
-     *      'max_age' =>  86400   // max age is 24 hours (in seconds)
-     *    );
-     *    $mySchemaCache = new Horde_Ldap_SimpleFileSchemaCache($mySchemaCache_cfg);
-     *    $ldap = new Horde_Ldap::connect(...);
-     *    $ldap->registerSchemaCache($mySchemaCache); // enable caching
-     *    // now each call to $ldap->schema() will get the schema from disk!
-     * </code>
-     *
-     * @param Horde_Ldap_SchemaCache|null $cache Object implementing the Horde_Ldap_SchemaCache interface
-     *
-     * @return true|Horde_Ldap_Error
-     * FIXME: Convert to Horde_Cache
-     */
-    public function registerSchemaCache($cache) {
-        if (is_null($cache)
-        || (is_object($cache) && in_array('Horde_Ldap_SchemaCache', class_implements($cache))) ) {
-            $this->_schemaCache = $cache;
-            return true;
-        } else {
-            throw new Horde_Ldap_Exception('Custom schema caching object is either no '.
-                'valid object or does not implement the Horde_Ldap_SchemaCache interface!');
-        }
+        return $this->_schema;
     }
 
     /**
diff --git a/framework/Ldap/lib/Horde/Ldap/SchemaCache.interface.php b/framework/Ldap/lib/Horde/Ldap/SchemaCache.interface.php
deleted file mode 100644 (file)
index e0c3094..0000000
+++ /dev/null
@@ -1,59 +0,0 @@
-<?php
-/* vim: set expandtab tabstop=4 shiftwidth=4: */
-/**
-* File containing the Net_LDAP2_SchemaCache interface class.
-*
-* PHP version 5
-*
-* @category  Net
-* @package   Net_LDAP2
-* @author    Benedikt Hallinger <beni@php.net>
-* @copyright 2009 Benedikt Hallinger
-* @license   http://www.gnu.org/licenses/lgpl-3.0.txt LGPLv3
-* @version   SVN: $Id: SchemaCache.interface.php 286718 2009-08-03 07:30:49Z beni $
-* @link      http://pear.php.net/package/Net_LDAP2/
-*/
-
-/**
-* Interface describing a custom schema cache object
-*
-* To implement a custom schema cache, one must implement this interface and
-* pass the instanciated object to Net_LDAP2s registerSchemaCache() method.
-*/
-interface Net_LDAP2_SchemaCache
-{
-    /**
-    * Return the schema object from the cache
-    *
-    * Net_LDAP2 will consider anything returned invalid, except
-    * a valid Net_LDAP2_Schema object.
-    * In case you return a Net_LDAP2_Error, this error will be routed
-    * to the return of the $ldap->schema() call.
-    * If you return something else, Net_LDAP2 will
-    * fetch a fresh Schema object from the LDAP server.
-    *
-    * You may want to implement a cache aging mechanism here too.
-    *
-    * @return Net_LDAP2_Schema|Net_LDAP2_Error|false
-    */
-    public function loadSchema();
-
-    /**
-    * Store a schema object in the cache
-    *
-    * This method will be called, if Net_LDAP2 has fetched a fresh
-    * schema object from LDAP and wants to init or refresh the cache.
-    *
-    * In case of errors you may return a Net_LDAP2_Error which will
-    * be routet to the client.
-    * Note that doing this prevents, that the schema object fetched from LDAP
-    * will be given back to the client, so only return errors if storing
-    * of the cache is something crucial (e.g. for doing something else with it).
-    * Normaly you dont want to give back errors in which case Net_LDAP2 needs to
-    * fetch the schema once per script run and instead use the error
-    * returned from loadSchema().
-    *
-    * @return true|Net_LDAP2_Error
-    */
-    public function storeSchema($schema);
-}
index c41cc2c..5855bf7 100644 (file)
@@ -251,7 +251,7 @@ class Horde_Ldap_Util
      *   the attribute type names are in alphabetical order.
      *
      * $options is a list of name/value pairs, valid options are:
-
+     *
      * - casefold:  Controls case folding of attribute type names. Attribute
      *              values are not affected by this option. The default is to
      *              uppercase. Valid values are: