forked from PHPSocialNetwork/phpfastcache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDriver.php
155 lines (137 loc) · 4.83 KB
/
Driver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
/**
*
* This file is part of Phpfastcache.
*
* @license MIT License (MIT)
*
* For full copyright and license information, please see the docs/CREDITS.txt and LICENCE files.
*
* @author Georges.L (Geolim4) <contact@geolim4.com>
* @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors
*/
declare(strict_types=1);
namespace Phpfastcache\Drivers\Rediscluster;
use DateTime;
use Phpfastcache\Cluster\AggregatablePoolInterface;
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
use Phpfastcache\Core\Pool\TaggableCacheItemPoolTrait;
use Phpfastcache\Drivers\Redis\RedisDriverTrait;
use Phpfastcache\Entities\DriverStatistic;
use Phpfastcache\Exceptions\PhpfastcacheLogicException;
use RedisCluster;
/**
* @property RedisCluster $instance
* @method Config getConfig()
*/
class Driver implements AggregatablePoolInterface
{
use RedisDriverTrait, TaggableCacheItemPoolTrait {
RedisDriverTrait::driverReadMultiple insteadof TaggableCacheItemPoolTrait;
RedisDriverTrait::driverDeleteMultiple insteadof TaggableCacheItemPoolTrait;
}
/**
* @return bool
*/
public function driverCheck(): bool
{
return extension_loaded('Redis') && class_exists(RedisCluster::class);
}
/**
* @return DriverStatistic
*/
public function getStats(): DriverStatistic
{
$masters = $this->instance->_masters();
$infos = array_map(fn($cluster) => $this->instance->info($cluster), $masters);
$date = (new DateTime())->setTimestamp(time() - min(array_column($infos, 'uptime_in_seconds')));
return (new DriverStatistic())
->setData(implode(', ', array_keys($this->itemInstances)))
->setRawData($infos)
->setSize(array_sum(array_column($infos, 'used_memory_dataset')))
->setInfo(
sprintf(
trim(<<<EOF
Redis Cluster version v%s, php-ext v%s with %d master nodes and %d slaves connected are up since %s.
For more information see RawData.
EOF
),
implode(', ', array_unique(array_column($infos, 'redis_version'))),
\phpversion("redis"),
count($masters),
array_sum(array_column($infos, 'connected_slaves')),
$date->format(DATE_RFC2822)
)
);
}
/**
* @return bool
* @throws PhpfastcacheLogicException
*/
protected function driverConnect(): bool
{
if (isset($this->instance) && $this->instance instanceof RedisCluster) {
throw new PhpfastcacheLogicException('Already connected to Redis server');
}
/**
* In case of a user-provided
* Redis client just return here
*/
if ($this->getConfig()->getRedisClusterClient() instanceof RedisCluster) {
/**
* Unlike Predis, we can't test if we're connected
* or not, so let's just assume that we are
*/
$this->instance = $this->getConfig()->getRedisClusterClient();
return true;
}
$this->instance = $this->instance ?? new RedisCluster(
null,
$this->getConfig()->getClusters(),
$this->getConfig()->getTimeout(),
$this->getConfig()->getReadTimeout(),
true,
$this->getConfig()->getPassword()
);
$this->instance->setOption(RedisCluster::OPT_SCAN, RedisCluster::SCAN_RETRY);
if ($this->getConfig()->getOptPrefix()) {
$this->instance->setOption(RedisCluster::OPT_PREFIX, $this->getConfig()->getOptPrefix());
}
if ($this->getConfig()->getSlaveFailover()) {
$this->instance->setOption(RedisCluster::OPT_SLAVE_FAILOVER, $this->getConfig()->getSlaveFailover());
}
return true;
}
/**
* @return array<int, string>
*/
protected function driverReadAllKeys(string $pattern = '*'): iterable
{
$result = [[]];
foreach ($this->instance->_masters() as $master) {
$i = 0;
$pattern = $pattern === '' ? '*' : $pattern;
do {
$result[] = $this->instance->scan(
$i,
$master,
$pattern,
10);
if (\count($result) > ExtendedCacheItemPoolInterface::MAX_ALL_KEYS_COUNT) {
break;
}
} while ($i > 0);
}
return array_unique(\array_merge(...$result));
}
/**
* @return bool
*/
protected function driverClear(): bool
{
foreach ($this->instance->_masters() as $nodeMaster) {
$this->instance->flushDb($nodeMaster);
}
return true;
}
}