Skip to content

Commit 691fb7e

Browse files
authored
Upgrade the minimum php version to 8.0 for connection pool. (#4219)
1 parent 07cf82e commit 691fb7e

File tree

2 files changed

+24
-48
lines changed

2 files changed

+24
-48
lines changed

src/Pool/RedisPool.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,10 @@
2121

2222
class RedisPool extends Pool
2323
{
24-
/**
25-
* @var string
26-
*/
27-
protected $name;
24+
protected array $config;
2825

29-
/**
30-
* @var array
31-
*/
32-
protected $config;
33-
34-
public function __construct(ContainerInterface $container, string $name)
26+
public function __construct(ContainerInterface $container, protected string $name)
3527
{
36-
$this->name = $name;
3728
$config = $container->get(ConfigInterface::class);
3829
$key = sprintf('redis.%s', $this->name);
3930
if (! $config->has($key)) {

src/RedisConnection.php

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
namespace Hyperf\Redis;
1313

1414
use Hyperf\Contract\ConnectionInterface;
15+
use Hyperf\Contract\PoolInterface;
1516
use Hyperf\Contract\StdoutLoggerInterface;
1617
use Hyperf\Pool\Connection as BaseConnection;
1718
use Hyperf\Pool\Exception\ConnectionException;
18-
use Hyperf\Pool\Pool;
1919
use Psr\Container\ContainerInterface;
2020

2121
/**
@@ -25,15 +25,9 @@ class RedisConnection extends BaseConnection implements ConnectionInterface
2525
{
2626
use ScanCaller;
2727

28-
/**
29-
* @var \Redis
30-
*/
31-
protected $connection;
28+
protected \Redis|\RedisCluster|null $connection = null;
3229

33-
/**
34-
* @var array
35-
*/
36-
protected $config = [
30+
protected array $config = [
3731
'host' => 'localhost',
3832
'port' => 6379,
3933
'auth' => null,
@@ -58,11 +52,10 @@ class RedisConnection extends BaseConnection implements ConnectionInterface
5852

5953
/**
6054
* Current redis database.
61-
* @var null|int
6255
*/
63-
protected $database;
56+
protected ?int $database = null;
6457

65-
public function __construct(ContainerInterface $container, Pool $pool, array $config)
58+
public function __construct(ContainerInterface $container, PoolInterface $pool, array $config)
6659
{
6760
parent::__construct($container, $pool);
6861
$this->config = array_replace_recursive($this->config, $config);
@@ -104,18 +97,11 @@ public function reconnect(): bool
10497
$cluster = $this->config['cluster']['enable'] ?? false;
10598
$sentinel = $this->config['sentinel']['enable'] ?? false;
10699

107-
$redis = null;
108-
switch (true) {
109-
case $cluster:
110-
$redis = $this->createRedisCluster();
111-
break;
112-
case $sentinel:
113-
$redis = $this->createRedisSentinel();
114-
break;
115-
default:
116-
$redis = $this->createRedis($host, $port, $timeout);
117-
break;
118-
}
100+
$redis = match (true) {
101+
$cluster => $this->createRedisCluster(),
102+
$sentinel => $this->createRedisSentinel(),
103+
default => $this->createRedis($host, $port, $timeout),
104+
};
119105

120106
$options = $this->config['options'] ?? [];
121107

@@ -161,20 +147,20 @@ public function setDatabase(?int $database): void
161147
$this->database = $database;
162148
}
163149

164-
protected function createRedisCluster()
150+
protected function createRedisCluster(): \RedisCluster
165151
{
166152
try {
167-
$paramaters = [];
168-
$paramaters[] = $this->config['cluster']['name'] ?? null;
169-
$paramaters[] = $this->config['cluster']['seeds'] ?? [];
170-
$paramaters[] = $this->config['timeout'] ?? 0.0;
171-
$paramaters[] = $this->config['cluster']['read_timeout'] ?? 0.0;
172-
$paramaters[] = $this->config['cluster']['persistent'] ?? false;
153+
$parameters = [];
154+
$parameters[] = $this->config['cluster']['name'] ?? null;
155+
$parameters[] = $this->config['cluster']['seeds'] ?? [];
156+
$parameters[] = $this->config['timeout'] ?? 0.0;
157+
$parameters[] = $this->config['cluster']['read_timeout'] ?? 0.0;
158+
$parameters[] = $this->config['cluster']['persistent'] ?? false;
173159
if (isset($this->config['auth'])) {
174-
$paramaters[] = $this->config['auth'];
160+
$parameters[] = $this->config['auth'];
175161
}
176162

177-
$redis = new \RedisCluster(...$paramaters);
163+
$redis = new \RedisCluster(...$parameters);
178164
} catch (\Throwable $e) {
179165
throw new ConnectionException('Connection reconnect failed ' . $e->getMessage());
180166
}
@@ -185,7 +171,7 @@ protected function createRedisCluster()
185171
protected function retry($name, $arguments, \Throwable $exception)
186172
{
187173
$logger = $this->container->get(StdoutLoggerInterface::class);
188-
$logger->warning(sprintf('Redis::__call failed, because ' . $exception->getMessage()));
174+
$logger->warning('Redis::__call failed, because ' . $exception->getMessage());
189175

190176
try {
191177
$this->reconnect();
@@ -198,7 +184,7 @@ protected function retry($name, $arguments, \Throwable $exception)
198184
return $result;
199185
}
200186

201-
protected function createRedisSentinel()
187+
protected function createRedisSentinel(): \Redis
202188
{
203189
try {
204190
$nodes = $this->config['sentinel']['nodes'] ?? [];
@@ -238,9 +224,8 @@ protected function createRedisSentinel()
238224
* @param string $host
239225
* @param int $port
240226
* @param float $timeout
241-
* @return \Redis
242227
*/
243-
protected function createRedis($host, $port, $timeout)
228+
protected function createRedis($host, $port, $timeout): \Redis
244229
{
245230
$redis = new \Redis();
246231
if (! $redis->connect((string) $host, (int) $port, $timeout)) {

0 commit comments

Comments
 (0)