XOOPS 2.5.6  Final
 All Classes Namespaces Files Functions Variables Pages
xoopscache.php
Go to the documentation of this file.
1 <?php
20 defined('XOOPS_ROOT_PATH') or die('Restricted access');
21 
29 {
36  var $engine = null;
37  // static $engine = null;
38 
39 
46  var $configs = array();
47 
54  var $name = null;
55 
59  function __construct()
60  {
61  }
62 
69  static function &getInstance()
70  {
71  static $instance;
72  if (!isset($instance)) {
73  $class = __CLASS__;
74  $instance = new $class();
75  }
76  return $instance;
77  }
78 
86  function loadEngine($name)
87  {
88  if (!class_exists('XoopsCache' . ucfirst($name))) {
89  if (file_exists($file = dirname(__FILE__) . '/' . strtolower($name) . '.php')) {
90  include $file;
91  } else {
92  trigger_error('File :' . $file . ' not found in file : ' . __FILE__ . ' at line: ' . __LINE__, E_USER_WARNING);
93  return false;
94  }
95  }
96  return true;
97  }
98 
107  function config($name = 'default', $settings = array())
108  {
109  $_this =& XoopsCache::getInstance();
110  if (is_array($name)) {
111  extract($name);
112  }
113 
114  if (isset($_this->configs[$name])) {
115  $settings = array_merge($_this->configs[$name], $settings);
116  } else if (!empty($settings)) {
117  $_this->configs[$name] = $settings;
118  } else if ($_this->configs !== null && isset($_this->configs[$_this->name])) {
119  $name = $_this->name;
120  $settings = $_this->configs[$_this->name];
121  } else {
122  $name = 'default';
123  if (!empty($_this->configs['default'])) {
124  $settings = $_this->configs['default'];
125  } else {
126  $settings = array(
127  'engine' => 'file');
128  }
129  }
130  $engine = 'file';
131  if (!empty($settings['engine'])) {
132  $engine = $settings['engine'];
133  }
134 
135  if ($name !== $_this->name) {
136  if ($_this->engine($engine, $settings) === false) {
137  trigger_error("Cache Engine {$engine} is not set", E_USER_WARNING);
138  return false;
139  }
140  $_this->name = $name;
141  $_this->configs[$name] = $_this->settings($engine);
142  }
143 
144  $settings = $_this->configs[$name];
145  return compact('engine', 'settings');
146  }
147 
156  function engine($name = 'file', $settings = array())
157  {
158  if (!$name) {
159  return false;
160  }
161 
162  $cacheClass = 'XoopsCache' . ucfirst($name);
163  $_this =& XoopsCache::getInstance();
164  if (!isset($_this->engine[$name])) {
165  if ($_this->loadEngine($name) === false) {
166  trigger_error("Cache Engine {$name} is not loaded", E_USER_WARNING);
167  return false;
168  }
169  $_this->engine[$name] = new $cacheClass();
170  }
171 
172  if ($_this->engine[$name]->init($settings)) {
173  if (time() % $_this->engine[$name]->settings['probability'] == 0) {
174  $_this->engine[$name]->gc();
175  }
176  return true;
177  }
178  $_this->engine[$name] = null;
179  trigger_error("Cache Engine {$name} is not initialized", E_USER_WARNING);
180  return false;
181  }
182 
190  function gc()
191  {
192  $_this =& XoopsCache::getInstance();
193  $config = $_this->config();
194  extract($config);
195  $_this->engine[$engine]->gc();
196  }
197 
208  function write($key, $value, $duration = null)
209  {
210  $key = substr(md5(XOOPS_URL), 0, 8) . '_' . $key;
211  $_this =& XoopsCache::getInstance();
212  $config = null;
213  if (is_array($duration)) {
214  extract($duration);
215  } else if (isset($_this->configs[$duration])) {
216  $config = $duration;
217  $duration = null;
218  }
219  $config = $_this->config($config);
220 
221  if (!is_array($config)) {
222  return null;
223  }
224  extract($config);
225 
226  if (!$_this->isInitialized($engine)) {
227  trigger_error('Cache write not initialized: ' . $engine);
228  return false;
229  }
230 
231  if (!$key = $_this->key($key)) {
232  return false;
233  }
234 
235  if (is_resource($value)) {
236  return false;
237  }
238 
239  if (!$duration) {
240  $duration = $settings['duration'];
241  }
242  $duration = is_numeric($duration) ? intval($duration) : strtotime($duration) - time();
243 
244  if ($duration < 1) {
245  return false;
246  }
247  $_this->engine[$engine]->init($settings);
248  $success = $_this->engine[$engine]->write($key, $value, $duration);
249  return $success;
250  }
251 
260  static function read($key, $config = null)
261  {
262  $key = substr(md5(XOOPS_URL), 0, 8) . '_' . $key;
263  $_this =& XoopsCache::getInstance();
264  $config = $_this->config($config);
265 
266  if (!is_array($config)) {
267  return null;
268  }
269 
270  extract($config);
271 
272  if (!$_this->isInitialized($engine)) {
273  return false;
274  }
275  if (!$key = $_this->key($key)) {
276  return false;
277  }
278  $_this->engine[$engine]->init($settings);
279  $success = $_this->engine[$engine]->read($key);
280  return $success;
281  }
282 
291  function delete($key, $config = null)
292  {
293  $key = substr(md5(XOOPS_URL), 0, 8) . '_' . $key;
294  $_this =& XoopsCache::getInstance();
295 
296  $config = $_this->config($config);
297  extract($config);
298 
299  if (!$_this->isInitialized($engine)) {
300  return false;
301  }
302 
303  if (!$key = $_this->key($key)) {
304  return false;
305  }
306 
307  $_this->engine[$engine]->init($settings);
308  $success = $_this->engine[$engine]->delete($key);
309  return $success;
310  }
311 
320  function clear($check = false, $config = null)
321  {
322  $_this =& XoopsCache::getInstance();
323  $config = $_this->config($config);
324  extract($config);
325 
326  if (!$_this->isInitialized($engine)) {
327  return false;
328  }
329  $success = $_this->engine[$engine]->clear($check);
330  $_this->engine[$engine]->init($settings);
331  return $success;
332  }
333 
342  function isInitialized($engine = null)
343  {
344  $_this =& XoopsCache::getInstance();
345  if (!$engine && isset($_this->configs[$_this->name]['engine'])) {
346  $engine = $_this->configs[$_this->name]['engine'];
347  }
348 
349  return isset($_this->engine[$engine]);
350  }
351 
359  function settings($engine = null)
360  {
361  $_this =& XoopsCache::getInstance();
362  if (!$engine && isset($_this->configs[$_this->name]['engine'])) {
363  $engine = $_this->configs[$_this->name]['engine'];
364  }
365  if (isset($_this->engine[$engine]) && ! is_null($_this->engine[$engine])) {
366  return $_this->engine[$engine]->settings();
367  }
368  return array();
369  }
370 
378  function key($key)
379  {
380  if (empty($key)) {
381  return false;
382  }
383  $key = str_replace(array('/', '.'), '_', strval($key));
384  return $key;
385  }
386 }
387 
395 {
403 
413  function init($settings = array())
414  {
415  $this->settings = array_merge(array(
416  'duration' => 31556926 ,
417  'probability' => 100), $settings);
418  return true;
419  }
420 
428  function gc()
429  {
430  }
431 
441  function write($key, $value = null, $duration = null)
442  {
443  trigger_error(sprintf(__('Method write() not implemented in %s', true), get_class($this)), E_USER_ERROR);
444  }
445 
453  function read($key)
454  {
455  trigger_error(sprintf(__('Method read() not implemented in %s', true), get_class($this)), E_USER_ERROR);
456  }
457 
465  function delete($key)
466  {
467  }
468 
476  function clear($check)
477  {
478  }
479 
486  function settings()
487  {
488  return $this->settings;
489  }
490 }
491 
492 ?>