1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
redisconnectionconfigurator.php
См. документацию.
1<?php
2
3namespace Bitrix\Main\Data\Configurator;
4
5use Bitrix\Main\Application;
6use Bitrix\Main\NotSupportedException;
7
9{
10 protected array $config;
11 protected array $servers = [];
12
13 public function __construct($config)
14 {
15 if (!extension_loaded('redis'))
16 {
17 throw new NotSupportedException('redis extension is not loaded.');
18 }
19
20 $this->config = $config;
21 $this->addServers($this->getConfig());
22 }
23
24 protected function addServers($config)
25 {
26 $servers = $config['servers'] ?? [];
27
28 if (empty($servers) && isset($config['host'], $config['port']))
29 {
30 array_unshift($servers, [
31 'host' => $config['host'],
32 'port' => $config['port'],
33 ]);
34 }
35
36 foreach ($servers as $server)
37 {
38 $this->servers[] = [
39 'host' => $server['host'] ?? 'localhost',
40 'port' => $server['port'] ?? '6379',
41 ];
42 }
43
44 return $this;
45 }
46
47 public function getConfig(): array
48 {
49 return $this->config;
50 }
51
52 protected function configureConnection(\RedisCluster|\Redis $connection): void
53 {
54 $config = $this->getConfig();
55
56 if ($connection instanceof \Redis)
57 {
58 if (isset($config['compression']) || defined('\Redis::COMPRESSION_LZ4'))
59 {
60 $connection->setOption(\Redis::OPT_COMPRESSION, $config['compression'] ?? \Redis::COMPRESSION_LZ4);
61 $connection->setOption(\Redis::OPT_COMPRESSION_LEVEL, $config['compression_level'] ?? \Redis::COMPRESSION_ZSTD_MAX);
62 }
63
64 if (isset($config['serializer']) || defined('\Redis::SERIALIZER_IGBINARY'))
65 {
66 $connection->setOption(\Redis::OPT_SERIALIZER, $config['serializer'] ?? \Redis::SERIALIZER_IGBINARY);
67 }
68 }
69 else
70 {
71 if (isset($config['serializer']))
72 {
73 $connection->setOption(\RedisCluster::OPT_SERIALIZER, $config['serializer']);
74 }
75 elseif (defined('\RedisCluster::SERIALIZER_IGBINARY'))
76 {
77 $connection->setOption(\RedisCluster::OPT_SERIALIZER, \RedisCluster::SERIALIZER_IGBINARY);
78 }
79
80 if (count($this->servers) > 1)
81 {
82 $connection->setOption(\RedisCluster::OPT_SLAVE_FAILOVER, $config['failover'] ?? \RedisCluster::FAILOVER_NONE);
83 }
84 }
85 }
86
87 public function createConnection()
88 {
89 $config = $this->getConfig();
90 if (!$this->servers)
91 {
92 throw new NotSupportedException('Empty server list to redis connection.');
93 }
94
95 if (count($this->servers) === 1)
96 {
97 ['host' => $host, 'port' => $port] = $this->servers[0];
98 $connection = new \Redis();
99
100 $params = [
101 $host,
102 $port,
103 $config['timeout'] ?? 0,
104 null,
105 0,
106 $config['readTimeout'] ?? 0
107 ];
108
109 if ($config['persistent'])
110 {
111 $result = $connection->pconnect(...$params);
112 }
113 else
114 {
115 $result = $connection->connect(...$params);
116 }
117 }
118 else
119 {
120 $connections = [];
121 foreach ($this->servers as $server)
122 {
123 $connections[] = $server['host'] . ':' . $server['port'];
124 }
125
126 $connection = new \RedisCluster(
127 null,
128 $connections,
129 $config['timeout'] ?? null,
130 $config['readTimeout'] ?? null,
131 $config['persistent'] ?? true
132 );
133 $result = true;
134 }
135
136 if ($result)
137 {
139 }
140 else
141 {
142 $error = error_get_last();
143 if (isset($error["type"]) && $error["type"] === E_WARNING)
144 {
145 $exception = new \ErrorException($error['message'], 0, $error['type'], $error['file'], $error['line']);
147 $exceptionHandler = $application->getExceptionHandler();
148 $exceptionHandler->writeToLog($exception);
149 }
150 }
151
152 return $result? $connection : null;
153 }
154}
$connection
Определения actionsdefinitions.php:38
static getInstance()
Определения application.php:98
configureConnection(\RedisCluster|\Redis $connection)
Определения redisconnectionconfigurator.php:52
</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
$application
Определения bitrix.php:23
$host
Определения mysql_to_pgsql.php:32
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799
$error
Определения subscription_card_product.php:20