24 protected string $sid =
'BX';
28 protected bool $old =
false;
40 abstract public function set(
$key, $ttl, $value);
41 abstract public function get(
$key);
61 static::$clusterGroup = (defined(
'BX_CLUSTER_GROUP') ? (int)constant(
'BX_CLUSTER_GROUP') : 0);
63 if (self::$isConnected)
65 if ($this->
lock($this->sid .
'|cacheClean', 10))
72 protected function connect(
$config)
74 if (!self::$isConnected)
82 self::$engine = $engineConnection->getResource();
83 self::$isConnected = $engineConnection->isConnected();
92 if (!$cacheConfig || !is_array($cacheConfig))
103 if (is_array($cacheConfig[
'type']) && isset($cacheConfig[
'type'][
'extension']))
105 $type = $cacheConfig[
'type'][
'extension'];
109 $type = $cacheConfig[
'type'];
114 $config[
'className'] = static::getConnectionClass();
117 if (!empty($cacheConfig[
$type][
'host']))
120 'host' => $cacheConfig[
$type][
'host'],
121 'port' => (int)($cacheConfig[
$type][
'port'] ?? 0)
126 if (isset($cacheConfig[
'servers']) && is_array($cacheConfig[
'servers']))
128 $config[
'servers'] = array_merge(
$config[
'servers'], $cacheConfig[
'servers']);
139 $cacheConfig[
'actual_data'] =
$options[
'actual_data'];
142 if (isset($cacheConfig[
'use_lock']))
144 $this->useLock = (bool)$cacheConfig[
'use_lock'];
151 elseif (!empty($cacheConfig[
'sid']))
153 $this->sid = $cacheConfig[
'sid'];
155 $this->sid .=
'|v1.1';
157 if (isset($cacheConfig[
'actual_data']) && !$this->useLock)
159 $this->useLock = !$cacheConfig[
'actual_data'];
164 $this->ttlMultiplier = 1;
167 if (isset($cacheConfig[
'ttl_multiplier']) && $this->useLock)
169 $this->ttlMultiplier = (int)$cacheConfig[
'ttl_multiplier'];
170 if ($this->ttlMultiplier < 1)
172 $this->ttlMultiplier = 1;
176 if (isset($cacheConfig[
'full_clean']))
178 $this->fullClean = (bool)$cacheConfig[
'full_clean'];
181 if (isset($cacheConfig[
'ttlOld']) && (
int)$cacheConfig[
'ttlOld'] > 0)
183 $this->ttlOld = (int)$cacheConfig[
'ttlOld'];
222 protected function lock(
string $key =
'',
int $ttl = 0): bool
230 if (isset(self::$locks[
$key]))
238 self::$locks[
$key] =
true;
256 unset(self::$locks[
$key]);
266 if (self::$engine !=
null)
268 self::$engine->close();
269 self::$engine =
null;
278 return self::$isConnected;
293 return substr(sha1(
$key), 0, 4);
298 return $this->sid .
'|BDV:' .
$baseDirVersion .
'|IDH:' . sha1($baseDir .
'|' . $initDir);
303 return $this->sid .
'|BDV:' . sha1($baseDir);
308 return $this->sid .
'|' . sha1(
$baseDirVersion .
'|' . $initDirVersion);
322 $initDirHash = sha1($baseDir .
'|' . $initDir);
325 $initDirVersion = $this->
get(
$key);
327 if ($initDirVersion ==
'' && $create)
329 $initDirVersion = sha1($initDirHash .
'|' . mt_rand() .
'|' . microtime());
330 $this->
set(
$key, 0, $initDirVersion);
333 return $initDirVersion;
346 if (!isset(static::$baseDirVersion[
$key]))
348 static::$baseDirVersion[
$key] = $this->
get($key);
351 if (static::$baseDirVersion[
$key] ==
'')
353 static::$baseDirVersion[
$key] = sha1(sha1($baseDir) .
'|' . mt_rand() .
'|' . microtime());
354 $this->
set($key, 0, static::$baseDirVersion[
$key]);
357 return static::$baseDirVersion[
$key];
368 if ($initDirVersion ==
'')
372 $initDirVersion = $this->
get($this->
getInitDirKey($baseDirVersion, $baseDir, $initDir) .
'~');
373 if ($initDirVersion ==
'')
394 $cachedData = $this->
get(
$key);
396 if (!is_array($cachedData))
398 $cachedData = $this->
get(
$key .
'|old');
400 if (is_array($cachedData))
406 if (!is_array($cachedData))
411 if (($cachedData[
'expire'] < time() || $this->old) && $this->
lock(
$key, $ttl))
416 $vars = $cachedData[
'content'];
420 $vars = $this->
get(
$key);
423 if (Cache::getShowCacheStat())
425 $this->
read = strlen(serialize($vars));
429 return $vars !==
false;
440 $keyPrefix = $this->
getKeyPrefix($baseDirVersion, $initDirVersion);
443 $exp = $this->ttlMultiplier * (int) $ttl;
447 $this->
set(
$key, $exp, [
'expire' => time() + $ttl,
'content' => $vars]);
453 $this->
set(
$key, $exp, $vars);
456 $initListKey = $keyPrefix .
'|' . self::BX_INIT_DIR_LIST;
458 $initListKeyPartition = $initListKey .
'|' . $initPartition;
461 if (empty(static::$initDirPartitions[$initListKey][$initPartition]))
463 static::$initDirPartitions[$initListKey][$initPartition] =
true;
464 $this->
addToSet($initListKey, $initPartition);
467 if ($this->fullClean)
469 $baseListKey = $this->sid .
'|' .
$baseDirVersion .
'|' . self::BX_BASE_LIST;
470 $baseListKeyPartition = $this->
getPartition($initListKeyPartition);
471 $this->
addToSet($baseListKey . $baseListKeyPartition, $keyPrefix);
472 $this->
addToSet($baseListKey, $baseListKeyPartition);
475 if (Cache::getShowCacheStat())
477 $this->written = strlen(serialize($vars));
487 if (!self::isAvailable())
495 if ($initDirVersion ==
'' && $initDir !=
'')
500 $keyPrefix = $this->
getKeyPrefix($baseDirVersion, $initDirVersion);
501 $initListKey = $keyPrefix .
'|' . self::BX_INIT_DIR_LIST;
508 if ($this->useLock && $cachedData = $this->
get(
$key))
527 $cleanFrom = (
new DateTime())->add(
'+' . $this->ttlOld .
' seconds');
535 'PREFIX' => $keyPrefix,
536 'CLEAN_FROM' => $cleanFrom,
537 'CLUSTER_GROUP' => static::$clusterGroup
540 $this->
set($this->sid .
'|needClean', 3600,
'Y');
544 if ($this->fullClean)
547 $this->useLock =
false;
550 $baseListKey = $this->sid .
'|' .
$baseDirVersion .
'|' . self::BX_BASE_LIST;
552 $partitionKeys = $this->
getSet($baseListKey);
553 foreach ($partitionKeys as $partition)
555 $baseListKeyPartition = $baseListKey . $partition;
562 'CLUSTER_GROUP' => static::$clusterGroup
569 $this->
set($this->sid .
'|needClean', 3600,
'Y');
570 $this->
del($baseListKey);
575 $this->
del($baseDirKey);
576 unset(static::$baseDirVersion[$baseDirKey]);
582 CacheCleanPathTable::add(
$data);
590 $needClean = $this->
get($this->sid .
'|needClean');
591 if ($needClean !==
'Y')
593 $this->
unlock($this->sid .
'|cacheClean');
597 $count = (int) $this->
get($this->sid .
'|delCount');
603 $paths = CacheCleanPathTable::query()
604 ->setSelect([
'ID',
'PREFIX'])
605 ->where(
'CLEAN_FROM',
'<=',
new DateTime())
606 ->where(
'CLUSTER_GROUP', static::$clusterGroup)
613 $partitionKeys = $this->
getSet($path[
'PREFIX'] .
'|' . static::BX_INIT_DIR_LIST);
614 foreach ($partitionKeys as $partition)
622 $delKey =
$path[
'PREFIX'] .
'|' . static::BX_INIT_DIR_LIST .
'|' . $partition;
623 $keys = $this->
getSet($delKey);
632 CacheCleanPathTable::delete(
$path[
'ID']);
643 $this->
set($this->sid .
'|delCount', 604800, $deleted);
645 elseif ($deleted < $count && $count > 1)
647 $this->
set($this->sid .
'|delCount', 604800, --
$count);
652 $this->
set($this->sid .
'|needClean', 3600,
'N');
655 $this->
unlock($this->sid .
'|cacheClean');
write($vars, $baseDir, $initDir, $filename, $ttl)
static array $initDirPartitions
getKeyPrefix($baseDirVersion, $initDirVersion)
__construct(array $options=[])
getBaseDirVersion($baseDir)
deleteBySet($key, $prefix='')
static getConnectionClass()
setNotExists($key, $ttl, $value)
addCleanPath(array $data)
delFromSet($key, $member)
getInitDirKey($baseDirVersion, $baseDir, $initDir)
lock(string $key='', int $ttl=0)
read(&$vars, $baseDir, $initDir, $filename, $ttl)
clean($baseDir, $initDir=false, $filename=false)
getInitDirVersion($baseDir, $initDir=false, bool $create=true)
static array $baseDirVersion
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
if(empty($signedUserToken)) $key