cvs: pear /Net_LDAP2 LDAP2.php package2.xml /Net_LDAP2/LDAP2 SchemaCache.interface.php SimpleFileSchemaCache.php
| From: | Benedikt Hallinger | Date: | Thu, 30 Apr 2009 10:57:34 +0000 |
| Subject: | cvs: pear /Net_LDAP2 LDAP2.php package2.xml /Net_LDAP2/LDAP2 SchemaCache.interface.php SimpleFileSchemaCache.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-56018@lists.php.net to get a copy of this message | ||
beni Thu Apr 30 10:57:34 2009 UTC
Added files:
/pear/Net_LDAP2/LDAP2 SchemaCache.interface.php
SimpleFileSchemaCache.php
Modified files:
/pear/Net_LDAP2 LDAP2.php package2.xml
Log:
* Added a schema caching facility toghether with some basic default implementation.
This is experimantal currently and should be testet.
http://cvs.php.net/viewvc.cgi/pear/Net_LDAP2/LDAP2.php?r1=1.17&r2=1.18&diff_format=u Index: pear/Net_LDAP2/LDAP2.php diff -u pear/Net_LDAP2/LDAP2.php:1.17 pear/Net_LDAP2/LDAP2.php:1.18 --- pear/Net_LDAP2/LDAP2.php:1.17 Thu Apr 30 09:11:23 2009 +++ pear/Net_LDAP2/LDAP2.php Thu Apr 30 10:57:33 2009 @@ -13,7 +13,7 @@ * @author Benedikt Hallinger <beni@php.net> * @copyright 2003-2007 Tarjej Huse, Jan Wagner, Del Elson, Benedikt Hallinger * @license http://www.gnu.org/copyleft/lesser.html LGPL -* @version CVS: $Id: LDAP2.php,v 1.17 2009/04/30 09:11:23 beni Exp $ +* @version CVS: $Id: LDAP2.php,v 1.18 2009/04/30 10:57:33 beni Exp $ * @link http://pear.php.net/package/Net_LDAP2/ */ @@ -28,6 +28,8 @@ require_once 'Net/LDAP2/Util.php'; require_once 'Net/LDAP2/Filter.php'; require_once 'Net/LDAP2/LDIF.php'; +require_once 'Net/LDAP2/SchemaCache.interface.php'; +require_once 'Net/LDAP2/SimpleFileSchemaCache.php'; /** * Error constants for errors that are not LDAP errors. @@ -133,6 +135,14 @@ protected $_schema = null; /** + * Schema cacher function callback + * + * @see registerSchemaCache() + * @var string + */ + protected $_schema_cache = ''; + + /** * Cache for attribute encoding checks * * @access protected @@ -474,7 +484,9 @@ } /** - * Reconnect to the ldap-server. In case the connection to the LDAP + * Reconnect to the ldap-server. + * + * In case the connection to the LDAP * service has dropped out for some reason, this function will reconnect, * and re-bind if a bind has been attempted in the past. It is probably * most useful when the server list provided to the new() or connect() @@ -1465,21 +1477,85 @@ */ public function &schema($dn = null) { - // Fetch schema, if not tried before. + // Schema caching by Knut-Olav Hoven + // If a schema caching object is registered, we use that to fetch + // a schema object. + // See registerSchemaCache() for more info on this. + if ($this->_schema_cache) { + $cached_schema = $this->_schema_cache->loadSchema(); + if ($cached_schema instanceof Net_LDAP2_Error) { + return $cached_schema; // route error to client + } else { + if ($cached_schema instanceof Net_LDAP2_Schema) { + $this->_schema = $cached_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 === null) { if (!$this->_schema instanceof Net_LDAP2_Schema) { // store a temporary error message so subsequent calls to schema() can // detect, that we are fetching the schema already. // Otherwise we will get a infinite loop at Net_LDAP2_Schema::fetch() - $this->_schema = new PEAR_Error('Schema not initialized'); + $this->_schema = new Net_LDAP2_Error('Schema not initialized'); $this->_schema = Net_LDAP2_Schema::fetch($this, $dn); + + // If schema caching is active, advise the cache to store the schema + if ($this->_schema_cache) { + $caching_result = $this->_schema_cache->storeSchema($schema); + if ($caching_result instanceof Net_LDAP2_Error) { + return $caching_result; // route error to client + } + } } } return $this->_schema; } /** + * Enable 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. + * + * For ease of use, Net_LDAP2 provides a simple file based cache + * which is used in the example below. + * <code> + * // Create the simple file cache object that comes along with Net_LDAP2 + * $mySchemaCache_cfg = array( + * 'path' => '/tmp/Net_LDAP2_Schema.cache', + * 'max_age' => 86400 // max age is 24 hours (in seconds) + * ); + * $mySchemaCache = new Net_LDAP2_SimpleFileSchemaCache($mySchemaCache_cfg); + * $ldap = new Net_LDAP2::connect(...); + * $ldap->registerSchemaCache($mySchemaCache); // enable caching + * // now each call to $ldap->schema() will get the schema from disk! + * </code> + * + * @param string $cache Name of the class implementing the Net_LDAP2_SchemaCache interface + * + * @return true|Net_LDAP2_Error + */ + public function registerSchemaCache($cache) { + if (!is_object($cache) || !in_array('Net_LDAP2_SchemaCache', class_implements($cache))) { + return new Net_LDAP2_Error('Custom schema caching object is either no '. + 'valid object or does not implement the Net_LDAP2_SchemaCache interface!'); + } else { + $this->_schema_cache = $cache; + return true; + } + } + + + /** * Checks if phps ldap-extension is loaded * * If it is not loaded, it tries to load it manually using PHPs dl(). http://cvs.php.net/viewvc.cgi/pear/Net_LDAP2/package2.xml?r1=1.31&r2=1.32&diff_format=u Index: pear/Net_LDAP2/package2.xml diff -u pear/Net_LDAP2/package2.xml:1.31 pear/Net_LDAP2/package2.xml:1.32 --- pear/Net_LDAP2/package2.xml:1.31 Thu Apr 30 09:19:48 2009 +++ pear/Net_LDAP2/package2.xml Thu Apr 30 10:57:33 2009 @@ -52,6 +52,8 @@ <file baseinstalldir="Net" name="Search.php" role="php" /> <file baseinstalldir="Net" name="Util.php" role="php" /> <file baseinstalldir="Net" name="LDIF.php" role="php" /> + <file baseinstalldir="Net" name="SchemaCache.interface.php" role="php" /> + <file baseinstalldir="Net" name="SimpleFileSchemaCache.php" role="php" /> </dir> <!-- /LDAP2 --> <dir name="doc"> <file baseinstalldir="Net" name="manual.html" role="doc" /> http://cvs.php.net/viewvc.cgi/pear/Net_LDAP2/LDAP2/SchemaCache.interface.php?view=markup&rev=1.1 Index: pear/Net_LDAP2/LDAP2/SchemaCache.interface.php +++ pear/Net_LDAP2/LDAP2/SchemaCache.interface.php <?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/copyleft/lesser.html LGPL * @version CVS: $Id: SchemaCache.interface.php,v 1.1 2009/04/30 10:57:33 beni Exp $ * @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_LDAP needs to * fetch the schema once per script run. * * @return true|Net_LDAP2_Error */ public function storeSchema($schema); } http://cvs.php.net/viewvc.cgi/pear/Net_LDAP2/LDAP2/SimpleFileSchemaCache.php?view=markup&rev=1.1 Index: pear/Net_LDAP2/LDAP2/SimpleFileSchemaCache.php +++ pear/Net_LDAP2/LDAP2/SimpleFileSchemaCache.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ /** * File containing the example simple file based Schema Caching class. * * PHP version 5 * * @category Net * @package Net_LDAP2 * @author Benedikt Hallinger <beni@php.net> * @copyright 2009 Benedikt Hallinger * @license http://www.gnu.org/copyleft/lesser.html LGPL * @version CVS: $Id: SimpleFileSchemaCache.php,v 1.1 2009/04/30 10:57:33 beni Exp $ * @link http://pear.php.net/package/Net_LDAP2/ */ /** * A simple file based schema cacher with cache aging. * * Once the cache is too old, the loadSchema() method will return false, so * Net_LDAP2 will fetch a fresh object from the LDAP server that will * overwrite the current (outdated) old cache. */ class Net_LDAP2_SimpleFileSchemaCache implements Net_LDAP2_SchemaCache { /** * Internal config of this cache * * @see Net_LDAP2_SimpleFileSchemaCache() * @var array */ protected $config = array( 'path' => '/tmp/Net_LDAP_Schema.cache', 'max_age' => 1200 ); /** * Initialize the simple cache * * Config is as following: * path Complete path to the cache file. * max_age Maximum age of cache in seconds, 0 means "endlessly". * * @param array $cfg Config array */ public function Net_LDAP2_SimpleFileSchemaCache($cfg) { foreach ($cfg as $key => $value) { if (array_key_exists($key, $this->config)) { if (gettype($this->parameter[$key]) != gettype($value)) { $this->getCore()->dropFatalError(__CLASS__.": Could not set config! Key $key does not match type ".gettype($this->parameter[$key])."!"); } $this->parameter[$key] = $value; } else { $this->getCore()->dropFatalError(__CLASS__.": Could not set config! Key $key is not defined!"); } } } /** * Return the schema object from the cache * * If file is existent and cache has not expired yet, * then the cache is deserialized and returned. * * @return Net_LDAP2_Schema|Net_LDAP2_Error|false */ public function loadSchema() { $return = false; // Net_LDAP2 will load schema from LDAP if (file_exists($this->config['path'])) { $cache_maxage = filemtime($this->config['path']) + $this->config['max_age']; if (time() <= $cache_maxage) { $return = unserialize(file_get_contents($this->config['path'])); } } return $return; } /** * 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. * * @return true|Net_LDAP2_Error|false */ public function storeSchema($schema) { file_put_contents($this->config['path'], serialize($schema)); return true; } }
http://cvs.php.net/viewvc.cgi/pear/Net_LDAP2/LDAP2.php?r1=1.17&r2=1.18&diff_format=u Index: pear/Net_LDAP2/LDAP2.php diff -u pear/Net_LDAP2/LDAP2.php:1.17 pear/Net_LDAP2/LDAP2.php:1.18 --- pear/Net_LDAP2/LDAP2.php:1.17 Thu Apr 30 09:11:23 2009 +++ pear/Net_LDAP2/LDAP2.php Thu Apr 30 10:57:33 2009 @@ -13,7 +13,7 @@ * @author Benedikt Hallinger <beni@php.net> * @copyright 2003-2007 Tarjej Huse, Jan Wagner, Del Elson, Benedikt Hallinger * @license http://www.gnu.org/copyleft/lesser.html LGPL -* @version CVS: $Id: LDAP2.php,v 1.17 2009/04/30 09:11:23 beni Exp $ +* @version CVS: $Id: LDAP2.php,v 1.18 2009/04/30 10:57:33 beni Exp $ * @link http://pear.php.net/package/Net_LDAP2/ */ @@ -28,6 +28,8 @@ require_once 'Net/LDAP2/Util.php'; require_once 'Net/LDAP2/Filter.php'; require_once 'Net/LDAP2/LDIF.php'; +require_once 'Net/LDAP2/SchemaCache.interface.php'; +require_once 'Net/LDAP2/SimpleFileSchemaCache.php'; /** * Error constants for errors that are not LDAP errors. @@ -133,6 +135,14 @@ protected $_schema = null; /** + * Schema cacher function callback + * + * @see registerSchemaCache() + * @var string + */ + protected $_schema_cache = ''; + + /** * Cache for attribute encoding checks * * @access protected @@ -474,7 +484,9 @@ } /** - * Reconnect to the ldap-server. In case the connection to the LDAP + * Reconnect to the ldap-server. + * + * In case the connection to the LDAP * service has dropped out for some reason, this function will reconnect, * and re-bind if a bind has been attempted in the past. It is probably * most useful when the server list provided to the new() or connect() @@ -1465,21 +1477,85 @@ */ public function &schema($dn = null) { - // Fetch schema, if not tried before. + // Schema caching by Knut-Olav Hoven + // If a schema caching object is registered, we use that to fetch + // a schema object. + // See registerSchemaCache() for more info on this. + if ($this->_schema_cache) { + $cached_schema = $this->_schema_cache->loadSchema(); + if ($cached_schema instanceof Net_LDAP2_Error) { + return $cached_schema; // route error to client + } else { + if ($cached_schema instanceof Net_LDAP2_Schema) { + $this->_schema = $cached_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 === null) { if (!$this->_schema instanceof Net_LDAP2_Schema) { // store a temporary error message so subsequent calls to schema() can // detect, that we are fetching the schema already. // Otherwise we will get a infinite loop at Net_LDAP2_Schema::fetch() - $this->_schema = new PEAR_Error('Schema not initialized'); + $this->_schema = new Net_LDAP2_Error('Schema not initialized'); $this->_schema = Net_LDAP2_Schema::fetch($this, $dn); + + // If schema caching is active, advise the cache to store the schema + if ($this->_schema_cache) { + $caching_result = $this->_schema_cache->storeSchema($schema); + if ($caching_result instanceof Net_LDAP2_Error) { + return $caching_result; // route error to client + } + } } } return $this->_schema; } /** + * Enable 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. + * + * For ease of use, Net_LDAP2 provides a simple file based cache + * which is used in the example below. + * <code> + * // Create the simple file cache object that comes along with Net_LDAP2 + * $mySchemaCache_cfg = array( + * 'path' => '/tmp/Net_LDAP2_Schema.cache', + * 'max_age' => 86400 // max age is 24 hours (in seconds) + * ); + * $mySchemaCache = new Net_LDAP2_SimpleFileSchemaCache($mySchemaCache_cfg); + * $ldap = new Net_LDAP2::connect(...); + * $ldap->registerSchemaCache($mySchemaCache); // enable caching + * // now each call to $ldap->schema() will get the schema from disk! + * </code> + * + * @param string $cache Name of the class implementing the Net_LDAP2_SchemaCache interface + * + * @return true|Net_LDAP2_Error + */ + public function registerSchemaCache($cache) { + if (!is_object($cache) || !in_array('Net_LDAP2_SchemaCache', class_implements($cache))) { + return new Net_LDAP2_Error('Custom schema caching object is either no '. + 'valid object or does not implement the Net_LDAP2_SchemaCache interface!'); + } else { + $this->_schema_cache = $cache; + return true; + } + } + + + /** * Checks if phps ldap-extension is loaded * * If it is not loaded, it tries to load it manually using PHPs dl(). http://cvs.php.net/viewvc.cgi/pear/Net_LDAP2/package2.xml?r1=1.31&r2=1.32&diff_format=u Index: pear/Net_LDAP2/package2.xml diff -u pear/Net_LDAP2/package2.xml:1.31 pear/Net_LDAP2/package2.xml:1.32 --- pear/Net_LDAP2/package2.xml:1.31 Thu Apr 30 09:19:48 2009 +++ pear/Net_LDAP2/package2.xml Thu Apr 30 10:57:33 2009 @@ -52,6 +52,8 @@ <file baseinstalldir="Net" name="Search.php" role="php" /> <file baseinstalldir="Net" name="Util.php" role="php" /> <file baseinstalldir="Net" name="LDIF.php" role="php" /> + <file baseinstalldir="Net" name="SchemaCache.interface.php" role="php" /> + <file baseinstalldir="Net" name="SimpleFileSchemaCache.php" role="php" /> </dir> <!-- /LDAP2 --> <dir name="doc"> <file baseinstalldir="Net" name="manual.html" role="doc" /> http://cvs.php.net/viewvc.cgi/pear/Net_LDAP2/LDAP2/SchemaCache.interface.php?view=markup&rev=1.1 Index: pear/Net_LDAP2/LDAP2/SchemaCache.interface.php +++ pear/Net_LDAP2/LDAP2/SchemaCache.interface.php <?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/copyleft/lesser.html LGPL * @version CVS: $Id: SchemaCache.interface.php,v 1.1 2009/04/30 10:57:33 beni Exp $ * @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_LDAP needs to * fetch the schema once per script run. * * @return true|Net_LDAP2_Error */ public function storeSchema($schema); } http://cvs.php.net/viewvc.cgi/pear/Net_LDAP2/LDAP2/SimpleFileSchemaCache.php?view=markup&rev=1.1 Index: pear/Net_LDAP2/LDAP2/SimpleFileSchemaCache.php +++ pear/Net_LDAP2/LDAP2/SimpleFileSchemaCache.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ /** * File containing the example simple file based Schema Caching class. * * PHP version 5 * * @category Net * @package Net_LDAP2 * @author Benedikt Hallinger <beni@php.net> * @copyright 2009 Benedikt Hallinger * @license http://www.gnu.org/copyleft/lesser.html LGPL * @version CVS: $Id: SimpleFileSchemaCache.php,v 1.1 2009/04/30 10:57:33 beni Exp $ * @link http://pear.php.net/package/Net_LDAP2/ */ /** * A simple file based schema cacher with cache aging. * * Once the cache is too old, the loadSchema() method will return false, so * Net_LDAP2 will fetch a fresh object from the LDAP server that will * overwrite the current (outdated) old cache. */ class Net_LDAP2_SimpleFileSchemaCache implements Net_LDAP2_SchemaCache { /** * Internal config of this cache * * @see Net_LDAP2_SimpleFileSchemaCache() * @var array */ protected $config = array( 'path' => '/tmp/Net_LDAP_Schema.cache', 'max_age' => 1200 ); /** * Initialize the simple cache * * Config is as following: * path Complete path to the cache file. * max_age Maximum age of cache in seconds, 0 means "endlessly". * * @param array $cfg Config array */ public function Net_LDAP2_SimpleFileSchemaCache($cfg) { foreach ($cfg as $key => $value) { if (array_key_exists($key, $this->config)) { if (gettype($this->parameter[$key]) != gettype($value)) { $this->getCore()->dropFatalError(__CLASS__.": Could not set config! Key $key does not match type ".gettype($this->parameter[$key])."!"); } $this->parameter[$key] = $value; } else { $this->getCore()->dropFatalError(__CLASS__.": Could not set config! Key $key is not defined!"); } } } /** * Return the schema object from the cache * * If file is existent and cache has not expired yet, * then the cache is deserialized and returned. * * @return Net_LDAP2_Schema|Net_LDAP2_Error|false */ public function loadSchema() { $return = false; // Net_LDAP2 will load schema from LDAP if (file_exists($this->config['path'])) { $cache_maxage = filemtime($this->config['path']) + $this->config['max_age']; if (time() <= $cache_maxage) { $return = unserialize(file_get_contents($this->config['path'])); } } return $return; } /** * 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. * * @return true|Net_LDAP2_Error|false */ public function storeSchema($schema) { file_put_contents($this->config['path'], serialize($schema)); return true; } }