20 defined(
'XOOPS_ROOT_PATH') or die('Restricted access');
59 function __construct()
69 static function &getInstance()
72 if (!isset($instance)) {
74 $instance =
new $class();
86 function loadEngine($name)
88 if (!class_exists(
'XoopsCache' . ucfirst($name))) {
89 if (file_exists(
$file = dirname(__FILE__) .
'/' . strtolower($name) .
'.php')) {
92 trigger_error(
'File :' .
$file .
' not found in file : ' . __FILE__ .
' at line: ' . __LINE__, E_USER_WARNING);
107 function config($name =
'default', $settings = array())
110 if (is_array($name)) {
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];
123 if (!empty($_this->configs[
'default'])) {
124 $settings = $_this->configs[
'default'];
131 if (!empty($settings[
'engine'])) {
132 $engine = $settings[
'engine'];
135 if ($name !== $_this->name) {
136 if ($_this->engine($engine, $settings) ===
false) {
137 trigger_error(
"Cache Engine {$engine} is not set", E_USER_WARNING);
140 $_this->name = $name;
141 $_this->configs[$name] = $_this->settings($engine);
144 $settings = $_this->configs[$name];
145 return compact(
'engine',
'settings');
156 function engine($name =
'file', $settings = array())
162 $cacheClass =
'XoopsCache' . ucfirst($name);
164 if (!isset($_this->engine[$name])) {
165 if ($_this->loadEngine($name) ===
false) {
166 trigger_error(
"Cache Engine {$name} is not loaded", E_USER_WARNING);
169 $_this->engine[$name] =
new $cacheClass();
172 if ($_this->engine[$name]->init($settings)) {
173 if (time() % $_this->engine[$name]->settings[
'probability'] == 0) {
174 $_this->engine[$name]->gc();
178 $_this->engine[$name] = null;
179 trigger_error(
"Cache Engine {$name} is not initialized", E_USER_WARNING);
195 $_this->engine[$engine]->gc();
208 function write($key, $value, $duration = null)
210 $key = substr(md5(XOOPS_URL), 0, 8) .
'_' . $key;
213 if (is_array($duration)) {
215 }
else if (isset($_this->configs[$duration])) {
226 if (!$_this->isInitialized($engine)) {
227 trigger_error(
'Cache write not initialized: ' . $engine);
231 if (!$key = $_this->key($key)) {
235 if (is_resource($value)) {
240 $duration = $settings[
'duration'];
242 $duration = is_numeric($duration) ? intval($duration) : strtotime($duration) - time();
247 $_this->engine[$engine]->init($settings);
248 $success = $_this->engine[$engine]->write($key, $value, $duration);
262 $key = substr(md5(XOOPS_URL), 0, 8) .
'_' . $key;
272 if (!$_this->isInitialized($engine)) {
275 if (!$key = $_this->key($key)) {
278 $_this->engine[$engine]->init($settings);
279 $success = $_this->engine[$engine]->read($key);
293 $key = substr(md5(XOOPS_URL), 0, 8) .
'_' . $key;
296 $config = $_this->config($config);
299 if (!$_this->isInitialized($engine)) {
303 if (!$key = $_this->key($key)) {
307 $_this->engine[$engine]->init($settings);
308 $success = $_this->engine[$engine]->delete($key);
320 function clear($check =
false, $config = null)
323 $config = $_this->config($config);
326 if (!$_this->isInitialized($engine)) {
329 $success = $_this->engine[$engine]->clear($check);
330 $_this->engine[$engine]->init($settings);
342 function isInitialized($engine = null)
345 if (!$engine && isset($_this->configs[$_this->name][
'engine'])) {
346 $engine = $_this->configs[$_this->name][
'engine'];
349 return isset($_this->engine[$engine]);
359 function settings($engine = null)
362 if (!$engine && isset($_this->configs[$_this->name][
'engine'])) {
363 $engine = $_this->configs[$_this->name][
'engine'];
365 if (isset($_this->engine[$engine]) && ! is_null($_this->engine[$engine])) {
366 return $_this->engine[$engine]->settings();
383 $key = str_replace(array(
'/',
'.'),
'_', strval($key));
415 $this->
settings = array_merge(array(
416 'duration' => 31556926 ,
441 function write($key, $value = null, $duration = null)
443 trigger_error(sprintf(__(
'Method write() not implemented in %s',
true), get_class($this)), E_USER_ERROR);
455 trigger_error(sprintf(__(
'Method read() not implemented in %s',
true), get_class($this)), E_USER_ERROR);
465 function delete($key)