3namespace Bitrix\Cluster;
5use Bitrix\Main\Config\Configuration;
7class ClusterCacheConfig
10 protected string $type =
'memcache';
11 protected string $constName =
'BX_MEMCACHE_CLUSTER';
12 protected static array|
null $servers =
null;
20 $this->constName =
'BX_MEMCACHE_CLUSTER';
24 $this->constName =
'BX_MEMCACHED_CLUSTER';
28 $this->constName =
'BX_REDIS_CLUSTER';
38 public static function getInstance(
string $type =
'memcache'): ClusterCacheConfig
40 if (!isset(self::$instance[
$type]))
42 self::$instance[
$type] =
new ClusterCacheConfig(
$type);
44 return self::$instance[
$type];
47 public function getConfig(
bool $onlyOnline =
false,
array &$otherGroups = []):
array
49 if (self::$servers ===
null || !isset(self::$servers[$this->type]))
52 $configFile =
$_SERVER[
'DOCUMENT_ROOT'] .
BX_ROOT .
'/modules/cluster/' . $this->type .
'.php';
54 if (file_exists($configFile))
59 if (defined($this->constName) && is_array($arList))
69 if ($onlyOnline && defined($this->constName) && is_array($arList))
72 foreach (self::$servers[$this->type] as $server)
74 if ($server[
'STATUS'] !==
'ONLINE')
79 if (defined(
'BX_CLUSTER_GROUP') && ($server[
'GROUP_ID'] !== constant(
'BX_CLUSTER_GROUP')))
81 $otherGroups[$server[
'GROUP_ID']] =
true;
86 'host' => $server[
'HOST'],
87 'port' => $server[
'PORT'],
88 'weight' => (int) $server[
'WEIGHT'] ?? 100
98 public function saveConfig(
array $servers,
array $cacheConfig,
string $className): bool
105 .
'if (!defined(\'' . $this->constName .
'\'))
'
107 . "\t" . 'define(\
'' . $this->constName .
'\', \
'' .
EscapePHPString(\CMain::GetServerUniqID()) .
'\');
'
108 . "\n}\n\n" . '$arList = [
' . "\n";
112 $clusterGroups = \CClusterGroup::GetList(['ID
' => 'DESC
']);
113 while ($group = $clusterGroups->Fetch())
115 $defaultGroup = $groups[$group['ID
']] = (int) $group['ID
'];
118 foreach ($servers as $i => $server)
120 $serverID = (int) $server['ID
'];
121 $groupID = (int) $server['GROUP_ID
'];
123 if ($server['STATUS
'] == 'ONLINE
')
128 if (!array_key_exists($server['GROUP_ID
'], $groups))
130 $groupID = $defaultGroup;
133 $content .= "\t{$serverID} => [\n";
134 $content .= "\t\t'ID
' => {$serverID},\n";
135 $content .= "\t\t'GROUP_ID
' => {$groupID},\n";
136 $content .= "\t\t'HOST
' => '" . EscapePHPString($server['HOST']) . "',\n";
137 $content .= "\t\t'PORT
' => " . intval($server['PORT
']) . ",\n";
138 $content .= "\t\t'WEIGHT
' => " . intval($server['WEIGHT
']) . ",\n";
139 $content .= match ($server['STATUS
'])
141 'ONLINE
' => "\t\t'STATUS
' => 'ONLINE
',\n",
142 'OFFLINE
' => "\t\t'STATUS
' => 'OFFLINE
',\n",
143 default => "\t\t'STATUS
' => 'READY
',\n",
146 $content .= "\t\t'MODE
' => '" . $server['MODE'] . "',\n";
147 $content .= "\t\t'ROLE
' => '" . $server['ROLE'] . "',\n";
148 $content .= "\t],\n";
153 $serverListFile = $_SERVER['DOCUMENT_ROOT
'] . BX_ROOT . '/modules/cluster/
' . $this->type . '.php
';
154 file_put_contents($serverListFile, $content);
155 \Bitrix\Main\Application::resetAccelerator($serverListFile);
157 $cache = Configuration::getValue('cache
');
162 || !isset($cache['type
'])
163 || !is_array($cache['type
'])
164 || !isset($cache['type
']['class_name
'])
165 || !($cache['type
']['class_name
'] === $className)
168 Configuration::setValue('cache
', $cacheConfig);
176 && isset($cache['type
'])
177 && is_array($cache['type
'])
178 && isset($cache['type
']['class_name
'])
179 && ($cache['type
']['class_name
'] === $className)
182 Configuration::setValue('cache
', null);
189 public function getServers(): array
191 $editPage = match ($this->type)
193 'memcached
' => 'cluster_memcached_edit
',
194 'redis
' => 'cluster_redis_edit
',
195 default => 'cluster_memcache_edit
',
199 foreach (ClusterCacheConfig::getInstance($this->type)->getConfig() as $data)
201 $host = ($data['HOST
'] === '127.0.0.1
' || $data['HOST
'] === 'localhost
') ? '' : $data['HOST
'];
204 'GROUP_ID
' => $data['GROUP_ID
'],
205 'SERVER_TYPE
' => $this->type,
209 'EDIT_URL
' => '/bitrix/admin/
' . $editPage . '.php?
lang=
' . LANGUAGE_ID . '&group_id=
' . $data['GROUP_ID
'] . '&ID=
' . $data['ID
'],
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
$_SERVER["DOCUMENT_ROOT"]
EscapePHPString($str, $encloser='"')
__construct(?int $storeId, int $productId, string $barcode, int $userId)