1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
clustercacheconfig.php
См. документацию.
1<?php
2
3namespace Bitrix\Cluster;
4
5use Bitrix\Main\Config\Configuration;
6
7class ClusterCacheConfig
8{
9 private static array $instance;
10 protected string $type = 'memcache';
11 protected string $constName = 'BX_MEMCACHE_CLUSTER';
12 protected static array|null $servers = null;
13
14 private function __construct(string $type = 'memcache')
15 {
16 switch ($type)
17 {
18 case 'memcache':
19 $this->type = $type;
20 $this->constName = 'BX_MEMCACHE_CLUSTER';
21 break;
22 case 'memcached':
23 $this->type = $type;
24 $this->constName = 'BX_MEMCACHED_CLUSTER';
25 break;
26 case 'redis':
27 $this->type = $type;
28 $this->constName = 'BX_REDIS_CLUSTER';
29 break;
30 }
31 }
32
33 private function __clone()
34 {
35 // You can't clone it
36 }
37
38 public static function getInstance(string $type = 'memcache'): ClusterCacheConfig
39 {
40 if (!isset(self::$instance[$type]))
41 {
42 self::$instance[$type] = new ClusterCacheConfig($type);
43 }
44 return self::$instance[$type];
45 }
46
47 public function getConfig(bool $onlyOnline = false, array &$otherGroups = []): array
48 {
49 if (self::$servers === null || !isset(self::$servers[$this->type]))
50 {
51 $arList = false;
52 $configFile = $_SERVER['DOCUMENT_ROOT'] . BX_ROOT . '/modules/cluster/' . $this->type . '.php';
53
54 if (file_exists($configFile))
55 {
56 include $configFile;
57 }
58
59 if (defined($this->constName) && is_array($arList))
60 {
61 self::$servers[$this->type] = $arList;
62 }
63 else
64 {
65 self::$servers[$this->type] = [];
66 }
67 }
68
69 if ($onlyOnline && defined($this->constName) && is_array($arList))
70 {
71 $result = [];
72 foreach (self::$servers[$this->type] as $server)
73 {
74 if ($server['STATUS'] !== 'ONLINE')
75 {
76 continue;
77 }
78
79 if (defined('BX_CLUSTER_GROUP') && ($server['GROUP_ID'] !== constant('BX_CLUSTER_GROUP')))
80 {
81 $otherGroups[$server['GROUP_ID']] = true;
82 continue;
83 }
84
85 $result[$server['ID']] = [
86 'host' => $server['HOST'],
87 'port' => $server['PORT'],
88 'weight' => (int) $server['WEIGHT'] ?? 100
89 ];
90 }
91
92 return $result;
93 }
94
95 return self::$servers[$this->type];
96 }
97
98 public function saveConfig(array $servers, array $cacheConfig, string $className): bool
99 {
100 $online = false;
101 $result = false;
102 self::$servers[$this->type] = null;
103
104 $content = "<?php\n"
105 . 'if (!defined(\'' . $this->constName . '\'))'
106 . "\n{\n"
107 . "\t" . 'define(\'' . $this->constName . '\', \'' . EscapePHPString(\CMain::GetServerUniqID()) . '\');'
108 . "\n}\n\n" . '$arList = [' . "\n";
109
110 $groups = [];
111 $defaultGroup = 1;
112 $clusterGroups = \CClusterGroup::GetList(['ID' => 'DESC']);
113 while ($group = $clusterGroups->Fetch())
114 {
115 $defaultGroup = $groups[$group['ID']] = (int) $group['ID'];
116 }
117
118 foreach ($servers as $i => $server)
119 {
120 $serverID = (int) $server['ID'];
121 $groupID = (int) $server['GROUP_ID'];
122
123 if ($server['STATUS'] == 'ONLINE')
124 {
125 $online = true;
126 }
127
128 if (!array_key_exists($server['GROUP_ID'], $groups))
129 {
130 $groupID = $defaultGroup;
131 }
132
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'])
140 {
141 'ONLINE' => "\t\t'STATUS' => 'ONLINE',\n",
142 'OFFLINE' => "\t\t'STATUS' => 'OFFLINE',\n",
143 default => "\t\t'STATUS' => 'READY',\n",
144 };
145
146 $content .= "\t\t'MODE' => '" . $server['MODE'] . "',\n";
147 $content .= "\t\t'ROLE' => '" . $server['ROLE'] . "',\n";
148 $content .= "\t],\n";
149 }
150
151 $content .= "];\n";
152
153 $serverListFile = $_SERVER['DOCUMENT_ROOT'] . BX_ROOT . '/modules/cluster/' . $this->type . '.php';
154 file_put_contents($serverListFile, $content);
155 \Bitrix\Main\Application::resetAccelerator($serverListFile);
156
157 $cache = Configuration::getValue('cache');
158 if ($online)
159 {
160 if (
161 !is_array($cache)
162 || !isset($cache['type'])
163 || !is_array($cache['type'])
164 || !isset($cache['type']['class_name'])
165 || !($cache['type']['class_name'] === $className)
166 )
167 {
168 Configuration::setValue('cache', $cacheConfig);
169 $result = true;
170 }
171 }
172 else
173 {
174 if (
175 is_array($cache)
176 && isset($cache['type'])
177 && is_array($cache['type'])
178 && isset($cache['type']['class_name'])
179 && ($cache['type']['class_name'] === $className)
180 )
181 {
182 Configuration::setValue('cache', null);
183 }
184 }
185
186 return $result;
187 }
188
189 public function getServers(): array
190 {
191 $editPage = match ($this->type)
192 {
193 'memcached' => 'cluster_memcached_edit',
194 'redis' => 'cluster_redis_edit',
195 default => 'cluster_memcache_edit',
196 };
197
198 $result = [];
199 foreach (ClusterCacheConfig::getInstance($this->type)->getConfig() as $data)
200 {
201 $host = ($data['HOST'] === '127.0.0.1' || $data['HOST'] === 'localhost') ? '' : $data['HOST'];
202 $result[] = [
203 'ID' => $data['ID'],
204 'GROUP_ID' => $data['GROUP_ID'],
205 'SERVER_TYPE' => $this->type,
206 'ROLE_ID' => '',
207 'HOST' => $host,
208 'DEDICATED' => 'Y',
209 'EDIT_URL' => '/bitrix/admin/' . $editPage . '.php?lang=' . LANGUAGE_ID . '&group_id=' . $data['GROUP_ID'] . '&ID=' . $data['ID'],
210 ];
211 }
212 return $result;
213 }
214}
$type
Определения options.php:106
const BX_ROOT
Определения bx_root.php:3
$content
Определения commerceml.php:144
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$_SERVER["DOCUMENT_ROOT"]
Определения cron_frame.php:9
EscapePHPString($str, $encloser='"')
Определения tools.php:4917
__construct(?int $storeId, int $productId, string $barcode, int $userId)
Определения basestorebarcodeaction.php:38
$instance
Определения ps_b24_final.php:14
lang
Определения options.php:182