2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
4 * File containing the example simple file based Schema Caching class.
10 * @author Benedikt Hallinger <beni@php.net>
11 * @copyright 2009 Benedikt Hallinger
12 * @license http://www.gnu.org/licenses/lgpl-3.0.txt LGPLv3
13 * @version SVN: $Id: SimpleFileSchemaCache.php 286718 2009-08-03 07:30:49Z beni $
14 * @link http://pear.php.net/package/Net_LDAP2/
18 * A simple file based schema cacher with cache aging.
20 * Once the cache is too old, the loadSchema() method will return false, so
21 * Net_LDAP2 will fetch a fresh object from the LDAP server that will
22 * overwrite the current (outdated) old cache.
24 class Net_LDAP2_SimpleFileSchemaCache implements Net_LDAP2_SchemaCache
27 * Internal config of this cache
29 * @see Net_LDAP2_SimpleFileSchemaCache()
32 protected $config = array(
33 'path' => '/tmp/Net_LDAP_Schema.cache',
38 * Initialize the simple cache
40 * Config is as following:
41 * path Complete path to the cache file.
42 * max_age Maximum age of cache in seconds, 0 means "endlessly".
44 * @param array $cfg Config array
46 public function Net_LDAP2_SimpleFileSchemaCache($cfg)
48 foreach ($cfg as $key => $value) {
49 if (array_key_exists($key, $this->config)) {
50 if (gettype($this->config[$key]) != gettype($value)) {
51 $this->getCore()->dropFatalError(__CLASS__.": Could not set config! Key $key does not match type ".gettype($this->config[$key])."!");
53 $this->config[$key] = $value;
55 $this->getCore()->dropFatalError(__CLASS__.": Could not set config! Key $key is not defined!");
61 * Return the schema object from the cache
63 * If file is existent and cache has not expired yet,
64 * then the cache is deserialized and returned.
66 * @return Net_LDAP2_Schema|Net_LDAP2_Error|false
68 public function loadSchema()
70 $return = false; // Net_LDAP2 will load schema from LDAP
71 if (file_exists($this->config['path'])) {
72 $cache_maxage = filemtime($this->config['path']) + $this->config['max_age'];
73 if (time() <= $cache_maxage || $this->config['max_age'] == 0) {
74 $return = unserialize(file_get_contents($this->config['path']));
81 * Store a schema object in the cache
83 * This method will be called, if Net_LDAP2 has fetched a fresh
84 * schema object from LDAP and wants to init or refresh the cache.
86 * To invalidate the cache and cause Net_LDAP2 to refresh the cache,
87 * you can call this method with null or false as value.
88 * The next call to $ldap->schema() will then refresh the caches object.
90 * @param mixed $schema The object that should be cached
91 * @return true|Net_LDAP2_Error|false
93 public function storeSchema($schema) {
94 file_put_contents($this->config['path'], serialize($schema));