1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
memcachedconnectionconfigurator.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
16 public function __construct(array $config)
17 {
18 if (!extension_loaded('memcached'))
19 {
20 throw new NotSupportedException('memcached extension is not loaded.');
21 }
22
23 $this->config = $config;
24 $this->addServers($this->getConfig());
25 }
26
28 {
29 $servers = $config['servers'] ?? [];
30
31 if (isset($config['host'], $config['port']))
32 {
33 array_unshift($servers, [
34 'host' => $config['host'],
35 'port' => $config['port'],
36 ]);
37 }
38
39 foreach ($servers as $server)
40 {
41 if (!isset($server['weight']) || $server['weight'] <= 0)
42 {
43 $server['weight'] = 1;
44 }
45
46 $this->servers[] = [
47 $server['host'] ?? 'localhost',
48 $server['port'] ?? '11211',
49 $server['weight']
50 ];
51 }
52
53 return $this;
54 }
55
56 public function getConfig(): array
57 {
58 return $this->config;
59 }
60
61 public function createConnection(): ?\Memcached
62 {
63 if (!$this->servers)
64 {
65 throw new NotSupportedException('Empty server list to memcache connection.');
66 }
67
68 $persistent = $this->getConfig()['persistent'] ?? true;
69
70 $connection = new \Memcached($persistent ? 'bx_cache' : '');
71 $connection->setOption(\Memcached::OPT_CONNECT_TIMEOUT, $this->getConfig()['connectionTimeout'] ?? 1000);
72 $connection->setOption(\Memcached::OPT_SERIALIZER, $this->getConfig()['serializer'] ?? \Memcached::SERIALIZER_PHP);
73
74 $result = false;
75 if (!empty($this->servers))
76 {
77 foreach ($this->servers as $server)
78 {
79 $success = $connection->addServer(
80 $server['host'],
81 $server['port'],
82 $server['weight']
83 );
84
85 if ($success)
86 {
88 }
89 }
90
91 $error = error_get_last();
92 if (isset($error['type']) && $error['type'] === E_WARNING)
93 {
94 $exception = new \ErrorException($error['message'], 0, $error['type'], $error['file'], $error['line']);
96 $exceptionHandler = $application->getExceptionHandler();
97 $exceptionHandler->writeToLog($exception);
98 }
99 }
100
101 return $result? $connection : null;
102 }
103}
$connection
Определения actionsdefinitions.php:38
static getInstance()
Определения application.php:98
</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
$success
Определения mail_entry.php:69
$application
Определения bitrix.php:23
$error
Определения subscription_card_product.php:20