SimpleFileSchemaCache.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. /**
  4. * File containing the example simple file based Schema Caching class.
  5. *
  6. * PHP version 5
  7. *
  8. * @category Net
  9. * @package Net_LDAP2
  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$
  14. * @link http://pear.php.net/package/Net_LDAP2/
  15. */
  16. /**
  17. * A simple file based schema cacher with cache aging.
  18. *
  19. * Once the cache is too old, the loadSchema() method will return false, so
  20. * Net_LDAP2 will fetch a fresh object from the LDAP server that will
  21. * overwrite the current (outdated) old cache.
  22. */
  23. class Net_LDAP2_SimpleFileSchemaCache implements Net_LDAP2_SchemaCache
  24. {
  25. /**
  26. * Internal config of this cache
  27. *
  28. * @see Net_LDAP2_SimpleFileSchemaCache()
  29. * @var array
  30. */
  31. protected $config = array(
  32. 'path' => '/tmp/Net_LDAP_Schema.cache',
  33. 'max_age' => 1200
  34. );
  35. /**
  36. * Initialize the simple cache
  37. *
  38. * Config is as following:
  39. * path Complete path to the cache file.
  40. * max_age Maximum age of cache in seconds, 0 means "endlessly".
  41. *
  42. * @param array $cfg Config array
  43. */
  44. public function __construct($cfg)
  45. {
  46. foreach ($cfg as $key => $value) {
  47. if (array_key_exists($key, $this->config)) {
  48. if (gettype($this->config[$key]) != gettype($value)) {
  49. $this->getCore()->dropFatalError(__CLASS__.": Could not set config! Key $key does not match type ".gettype($this->config[$key])."!");
  50. }
  51. $this->config[$key] = $value;
  52. } else {
  53. $this->getCore()->dropFatalError(__CLASS__.": Could not set config! Key $key is not defined!");
  54. }
  55. }
  56. }
  57. /**
  58. * Return the schema object from the cache
  59. *
  60. * If file is existent and cache has not expired yet,
  61. * then the cache is deserialized and returned.
  62. *
  63. * @return Net_LDAP2_Schema|Net_LDAP2_Error|false
  64. */
  65. public function loadSchema()
  66. {
  67. $return = false; // Net_LDAP2 will load schema from LDAP
  68. if (file_exists($this->config['path'])) {
  69. $cache_maxage = filemtime($this->config['path']) + $this->config['max_age'];
  70. if (time() <= $cache_maxage || $this->config['max_age'] == 0) {
  71. $return = unserialize(file_get_contents($this->config['path']));
  72. }
  73. }
  74. return $return;
  75. }
  76. /**
  77. * Store a schema object in the cache
  78. *
  79. * This method will be called, if Net_LDAP2 has fetched a fresh
  80. * schema object from LDAP and wants to init or refresh the cache.
  81. *
  82. * To invalidate the cache and cause Net_LDAP2 to refresh the cache,
  83. * you can call this method with null or false as value.
  84. * The next call to $ldap->schema() will then refresh the caches object.
  85. *
  86. * @param mixed $schema The object that should be cached
  87. * @return true|Net_LDAP2_Error|false
  88. */
  89. public function storeSchema($schema) {
  90. file_put_contents($this->config['path'], serialize($schema));
  91. return true;
  92. }
  93. }