22 protected string $sid =
'BX';
26 protected bool $old =
false;
32 abstract public function set($key, $ttl, $value);
33 abstract public function get($key);
34 abstract public function del($key);
37 abstract public function addToSet($key, $value);
38 abstract public function getSet($key) : array;
50 if (self::$engine ==
null)
58 $this->connect($config);
61 protected function connect($config)
68 self::$engine = $engineConnection->getResource();
69 self::$isConnected = $engineConnection->isConnected();
75 $cacheConfig = Config\Configuration::getValue(
'cache');
77 if (!$cacheConfig || !is_array($cacheConfig))
82 if (isset($options[
'type']))
84 $type = $options[
'type'];
88 if (is_array($cacheConfig[
'type']) && is_set($cacheConfig[
'type'][
'extension']))
90 $type = $cacheConfig[
'type'][
'extension'];
94 $type = $cacheConfig[
'type'];
98 $config[
'type'] = $type;
99 $config[
'className'] = static::getConnectionClass();
101 if (!isset($config[
'servers']) || !is_array($config[
'servers']))
103 $config[
'servers'] = [];
106 if (isset($cacheConfig[$type]) && is_array($cacheConfig[$type]) && !empty($cacheConfig[$type][
'host']))
108 $config[
'servers'][] = [
109 'host' => $cacheConfig[$type][
'host'],
110 'port' => (int) ($cacheConfig[$type][
'port'] ?? 0)
115 if (isset($cacheConfig[
'servers']) && is_array($cacheConfig[
'servers']))
117 $config[
'servers'] = array_merge($config[
'servers'], $cacheConfig[
'servers']);
121 if (isset($options[
'servers']) && is_array($options[
'servers']))
123 $config[
'servers'] = array_merge($config[
'servers'], $options[
'servers']);
126 if (isset($cacheConfig[
'use_lock']))
128 $this->useLock = (bool) $cacheConfig[
'use_lock'];
131 if (isset($cacheConfig[
'sid']) && ($cacheConfig[
'sid'] !=
''))
133 $this->sid = $cacheConfig[
'sid'];
137 if (isset($cacheConfig[
'serializer']))
139 $config[
'serializer'] = (int) $cacheConfig[
'serializer'];
142 $config[
'persistent'] =
true;
143 if (isset($cacheConfig[
'persistent']) && $cacheConfig[
'persistent'] == 0)
145 $config[
'persistent'] =
false;
148 if (isset($cacheConfig[
'actual_data']))
150 $this->useLock = !$cacheConfig[
'actual_data'];
155 $this->ttlMultiplier = 1;
160 $this->ttlMultiplier = (int) $cacheConfig[
'ttl_multiplier'];
161 if ($this->ttlMultiplier < 1)
163 $this->ttlMultiplier = 1;
167 if (isset($cacheConfig[
'full_clean']))
169 $this->fullClean = (bool) $cacheConfig[
'full_clean'];
184 protected function lock(
string $key =
'',
int $ttl = 0) : bool
192 if (isset(self::$locks[$key]))
200 self::$locks[$key] =
true;
216 protected function unlock(
string $key =
'',
int $ttl = 0) : void
224 $this->
set($key, $ttl, 1);
231 unset(self::$locks[$key]);
241 if (self::$engine !=
null)
243 self::$engine->close();
244 self::$engine =
null;
270 return '|' . substr(sha1($key), 0, 2) .
'|';
275 return $this->sid .
'|' . $this->
getBaseDirVersion($baseDir) .
'|init_dir|' . sha1($initDir);
290 if (!array_key_exists($key, static::$initDirVersion))
292 static::$initDirVersion[$key] = $this->
get($key);
296 static::$initDirVersion[$key] ===
false
297 || (static::$initDirVersion[$key] ==
'' && $generateNew)
302 static::$initDirVersion[$key] = sha1(mt_rand() .
'|' . microtime());
303 $this->
set($key, 0, static::$initDirVersion[$key]);
307 static::$initDirVersion[$key] =
'';
311 return static::$initDirVersion[$key];
322 $baseDirHash = sha1($baseDir);
323 $key = $this->sid .
'|base_dir|' . $baseDirHash;
325 if (!isset(static::$baseDirVersion[$key]))
327 static::$baseDirVersion[$key] = $this->
get($key);
330 if (static::$baseDirVersion[$key] ===
false)
332 static::$baseDirVersion[$key] = sha1($baseDirHash .
'|' . mt_rand() .
'|' . microtime());
333 $this->
set($key, 0, static::$baseDirVersion[$key]);
336 return static::$baseDirVersion[$key];
350 public function read(&$vars, $baseDir, $initDir, $filename, $ttl)
363 $cachedData = $this->
get($key);
365 if (!is_array($cachedData))
367 $cachedData = $this->
get($key .
'|old');
368 if (is_array($cachedData))
374 if (!is_array($cachedData))
379 if ($this->
lock($key, $ttl))
381 if ($this->old || $cachedData[
'datecreate'] < (time() - $ttl))
387 $vars = $cachedData[
'content'];
391 $vars = $this->
get($key);
394 return $vars !==
false;
408 public function write($vars, $baseDir, $initDir, $filename, $ttl)
414 $key = $this->sid.
'|' . $dir .
'|' . $filename;
415 $exp = $this->ttlMultiplier * (int) $ttl;
419 $this->
set($key, $exp, [
'datecreate' => time(),
'content' => $vars]);
420 $this->
del($key .
'|old');
421 $this->
unlock($key, $ttl);
425 $this->
set($key, $exp, $vars);
431 $initListKeyPartition = $initListKey . $initPartition;
433 $this->
addToSet($initListKeyPartition, $filename);
434 $this->
addToSet($initListKey, $initPartition);
436 if ($this->fullClean)
439 $baseListKeyPartition = $this->
getPartition($initListKeyPartition);
440 $this->
addToSet($baseListKey . $baseListKeyPartition, $initListKeyPartition);
441 $this->
addToSet($baseListKey, $baseListKeyPartition);
454 public function clean($baseDir, $initDir =
false, $filename =
false)
456 if (!self::isAvailable())
467 if ($this->fullClean)
474 $key = $this->sid .
'|' .$dir .
'|' . $filename;
477 if ($this->useLock && $cachedData = $this->
get($key))
488 elseif ($initDir !=
'')
490 $keyPrefix = $this->sid .
'|' .$dir .
'|';
492 $this->
del($initDirKey);
493 unset(static::$initDirVersion[$initDirKey]);
495 $partitionKeys = $this->
getSet($initListKey);
496 foreach ($partitionKeys as $partition)
498 $delKey = $initListKey . $partition;
501 if ($this->fullClean)
506 $this->
del($initListKey);
510 $baseDirKey = $this->sid .
'|base_dir|' . sha1 ($baseDir);
511 $this->
del($baseDirKey);
512 unset(static::$baseDirVersion[$baseDirKey]);
514 if ($this->fullClean)
516 $keyPrefix = $this->sid .
'|' .$dir .
'|';
517 $partitionKeys = $this->
getSet($baseListKey);
519 foreach ($partitionKeys as $partition)
521 $baseListKeyPartition = $baseListKey . $partition;
522 $keys = $this->
getSet($baseListKeyPartition);
524 foreach ($keys as $initKey)
527 $this->
del($initKey);
530 $this->
del($baseListKeyPartition);
534 $this->
del($baseListKey);
write($vars, $baseDir, $initDir, $filename, $ttl)
static array $initDirVersion
__construct(array $options=[])
getBaseDirVersion($baseDir)
deleteBySet($key, $prefix='')
static getConnectionClass()
setNotExists($key, $ttl, $value)
delFromSet($key, $member)
getInitDirKey($baseDir, $initDir=false)
unlock(string $key='', int $ttl=0)
lock(string $key='', int $ttl=0)
read(&$vars, $baseDir, $initDir, $filename, $ttl)
getInitDirVersion($baseDir, $initDir=false, $generateNew=true)
clean($baseDir, $initDir=false, $filename=false)
static array $baseDirVersion