Skip to content

Commit 4bd3f47

Browse files
committed
Optimized code.
1 parent c078080 commit 4bd3f47

File tree

9 files changed

+36
-25
lines changed

9 files changed

+36
-25
lines changed

publish/redis.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@
1515
'host' => env('REDIS_HOST', 'localhost'),
1616
'auth' => env('REDIS_AUTH', null),
1717
'port' => (int) env('REDIS_PORT', 6379),
18-
'cluster' => env('REDIS_CLUSTER', false),
1918
'db' => (int) env('REDIS_DB', 0),
2019
'timeout' => 0.0,
2120
'reserved' => null,
2221
'retry_interval' => 0,
22+
'cluster' => [
23+
'enable' => (bool) env('REDIS_ENABLE_CLUSTER', false),
24+
'name' => null,
25+
'seeds' => [],
26+
],
2327
'pool' => [
2428
'min_connections' => 1,
2529
'max_connections' => 10,

src/Pool/PoolFactory.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
use Hyperf\Di\Container;
1616
use Psr\Container\ContainerInterface;
17-
use Swoole\Coroutine\Channel;
1817

1918
class PoolFactory
2019
{

src/Pool/RedisPool.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,6 @@ public function __construct(ContainerInterface $container, string $name)
4949
parent::__construct($container, $options);
5050
}
5151

52-
/**
53-
* @return string
54-
*/
5552
public function getName(): string
5653
{
5754
return $this->name;

src/RedisConnection.php

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
use Hyperf\Pool\Pool;
1919
use Psr\Container\ContainerInterface;
2020

21+
/**
22+
* @method select(int $db)
23+
*/
2124
class RedisConnection extends BaseConnection implements ConnectionInterface
2225
{
2326
/**
@@ -32,9 +35,13 @@ class RedisConnection extends BaseConnection implements ConnectionInterface
3235
'host' => 'localhost',
3336
'port' => 6379,
3437
'auth' => null,
35-
'cluster' => false,
3638
'db' => 0,
3739
'timeout' => 0.0,
40+
'cluster' => [
41+
'enable' => false,
42+
'name' => null,
43+
'seeds' => [],
44+
],
3845
'options' => [],
3946
];
4047

@@ -75,24 +82,18 @@ public function reconnect(): bool
7582
$host = $this->config['host'];
7683
$port = $this->config['port'];
7784
$auth = $this->config['auth'];
78-
$cluster = $this->config['cluster'];
7985
$db = $this->config['db'];
8086
$timeout = $this->config['timeout'];
87+
$cluster = $this->config['cluster']['enable'] ?? false;
8188

8289
$redis = null;
8390
if ($cluster !== true) {
84-
// Normal Redis (Non-cluster)
8591
$redis = new \Redis();
8692
if (! $redis->connect($host, $port, $timeout)) {
8793
throw new ConnectionException('Connection reconnect failed.');
8894
}
8995
} else {
90-
// Redis Cluster
91-
try {
92-
$redis = new \RedisCluster(null, [$host . ':' . $port], $timeout);
93-
} catch (\Throwable $e) {
94-
throw new ConnectionException('Connection reconnect failed. ' . $e->getMessage());
95-
}
96+
$redis = $this->createRedisCluster();
9697
}
9798

9899
$options = $this->config['options'] ?? [];
@@ -138,4 +139,19 @@ public function setDatabase(?int $database): void
138139
{
139140
$this->database = $database;
140141
}
142+
143+
protected function createRedisCluster()
144+
{
145+
try {
146+
$seeds = $this->config['cluster']['seeds'] ?? [];
147+
$name = $this->config['cluster']['name'] ?? null;
148+
$timeout = $this->config['timeout'] ?? null;
149+
150+
$redis = new \RedisCluster($name, $seeds, $timeout);
151+
} catch (\Throwable $e) {
152+
throw new ConnectionException('Connection reconnect failed. ' . $e->getMessage());
153+
}
154+
155+
return $redis;
156+
}
141157
}

src/RedisFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function __construct(ConfigInterface $config)
3232
}
3333

3434
/**
35-
* @return \Redis
35+
* @return \Redis|RedisProxy
3636
*/
3737
public function get(string $poolName)
3838
{

tests/RedisConnectionTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,13 @@ public function testRedisConnectionConfig()
4545
'host' => 'redis',
4646
'port' => 16379,
4747
'auth' => 'redis',
48-
'cluster' => false,
4948
'db' => 0,
5049
'timeout' => 0.0,
50+
'cluster' => [
51+
'enable' => false,
52+
'name' => null,
53+
'seeds' => [],
54+
],
5155
'options' => [],
5256
'pool' => [
5357
'min_connections' => 1,
@@ -108,7 +112,6 @@ private function getRedisPool()
108112
'host' => 'redis',
109113
'auth' => 'redis',
110114
'port' => 16379,
111-
'cluster' => false,
112115
'pool' => [
113116
'min_connections' => 1,
114117
'max_connections' => 30,

tests/RedisProxyTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ private function getRedis($optinos = [])
7878
'host' => 'localhost',
7979
'auth' => null,
8080
'port' => 6379,
81-
'cluster' => false,
8281
'db' => 0,
8382
'options' => $optinos,
8483
'pool' => [

tests/RedisTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ private function getRedis()
7777
'host' => 'localhost',
7878
'auth' => null,
7979
'port' => 6379,
80-
'cluster' => false,
8180
'db' => 0,
8281
'pool' => [
8382
'min_connections' => 1,

tests/Stub/RedisConnectionStub.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,11 @@ public function select($db)
4747
$this->db = $db;
4848
}
4949

50-
/**
51-
* @return array
52-
*/
5350
public function getConfig(): array
5451
{
5552
return $this->config;
5653
}
5754

58-
/**
59-
* @return null|int
60-
*/
6155
public function getDatabase(): ?int
6256
{
6357
return $this->database;

0 commit comments

Comments
 (0)