Source for file schema_cache.php
Documentation is available at schema_cache.php
* This is a short example on how to use the schema cache facility.
* Schema caching allows you to store the fetched schema on disk
* (or wherever you want, depending on the cache class used) so
* initialisation of Net_LDAP2 becomes a little faster.
* Two examples will be showed here:
* 1. how to use the packaged file based cache
* 2. how to write a custom cache class
// Class includes; this assumes Net_LDAP2 installed in PHPs include path
// or under subfolder "Net" in the local directory.
require_once 'Net/LDAP2.php';
// host can be a single server (string) or multiple ones - if we define more
// servers here (array), we can implement a basic fail over scenario.
// If no credentials (binddn and bindpw) are given, Net_LDAP2 establishes
// See the documentation for more information on the configuration items!
// 'host' => 'ldap.example.org',
'host' => array ('ldap1.example.org', 'ldap2.example.org'),
// 'binddn' => 'cn=admin,o=example,dc=org',
// 'bindpw' => 'your-secret-password',
'base' => 'o=example,dc=org',
* EXAMPLE 1: How to use the packaged file based cach
* This cache class stores the schema object on disk once Net_LDAP2
* initially fetched it from the LDAP server. This will make Net_LDAP2
* use the disk version instead of loading the schema from LDAP
* unless the schema object becomes too old.
// Configuring the schema cacher
// see the source code of SimpleFileSchemaCache.php for config options
// An interesting idea is, to store the file in some tmpfs mount, which will
// result in storing the schema cache in memory instead of disk.
$mySchemaCache_cfg = array (
'path' => '/tmp/Net_LDAP2_Schema.cache', // place to put cache file
'max_age' => 86400 // max age is 24 hours (in seconds)
// Initialize cache with the config
$mySchemaCache = new Net_LDAP2_SimpleFileSchemaCache ($mySchemaCache_cfg);
// As usual, connect to configured ldap server
die ('BIND FAILED: '. $ldap->getMessage ());
// and finally register our initialized cache object
$res = $ldap->registerSchemaCache ($mySchemaCache);
die ('REGISTER CACHE FAILED: '. $res->getMessage ());
// Here we go, Net_LDAP2 will fetch the schema once and then use the disk version.
* EXAMPLE 2: How to write a custom cache class
* Writing a custom cache class is easy. You just have to wipe out a
* class that implements the SchemaCache interface.
* How a cache class must look like is documented in the interface
* definition file: SchemaCache.interface.php
* Here we will write a small hacky cache that stores the schema
* in the php session. This gives us a nice per-user cache that
* survives for the php session. This cache will obviously not
* be so performant as the SimpleFileSchemaCache but may be
* useful for other purposes.
// Firstly, we need our custom schema class...
class MySessionSchemaCache implements Net_LDAP2_SchemaCache {
* Here we do nothing. You can use the class constructor for everything you
* want, but typically it is used to configure the caches config.
public function MySessionSchemaCache () {
// nothing to see here, move along...
* Load schema from session
* For the sake of simplicity we dont implement a cache aging here.
* This is not a big problem, since php sessions shouldnt last endlessly.
* @return Net_LDAP2_Schema|Net_LDAP2_Error|false
public function loadSchema () {
// Lets see if we have a session, otherwise we cant use this cache
// and drop some error that will be returned by Net_LDAP2->schema().
// Minor errors should be indicated by returning false, so Net_LDAP2
// can continue its work. This will result in the same behavior as
// no schema cache would have been registered.
" This cache needs an open PHP session.");
// Here the session is valid, so we return the stores schema.
// If we cant find the schema (because cahce is empty),w e return
// false to inidicate a minor error to Net_LDAP2.
// This causes it to fetch a fresh object from LDAP.
&& $_SESSION[__CLASS__ ] instanceof Net_LDAP2_SchemaCache ) {
return $_SESSION[__CLASS__ ];
* Store the schema object in session
* @return true|Net_LDAP2_Error
public function storeSchema ($schema) {
// Just dump the given object into the session
// unless in loadSchema(), it is important to only return
// Net_LDAP2_Error objects if something crucial went wrong.
// If you feel that you want to return an error object, be sure
// that you have read the comments in Net_LDAP2_SchemaCache.interface.php
// or you will seriously hurt the performance of your application!!!!
$_SESSION[__CLASS__ ] = $schema;
// Ok, now we have our finished cache object. Now initialize and register it
$mySchemaCache = new MySessionSchemaCache ();
die ('BIND FAILED: '. $ldap->getMessage ());
$res = $ldap->registerSchemaCache ($mySchemaCache);
die ('REGISTER CACHE FAILED: '. $res->getMessage ());
// Now, the Schema is cached in the PHP session :)
Documentation generated on Mon, 11 Mar 2019 16:03:55 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|