Skip to content

Commit cf2e0b7

Browse files
committed
Support docker cluster
1 parent 042e447 commit cf2e0b7

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

publish/redis.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
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),
1819
'db' => (int) env('REDIS_DB', 0),
1920
'timeout' => 0.0,
2021
'reserved' => null,

src/RedisConnection.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class RedisConnection extends BaseConnection implements ConnectionInterface
3232
'host' => 'localhost',
3333
'port' => 6379,
3434
'auth' => null,
35+
'cluster' => false,
3536
'db' => 0,
3637
'timeout' => 0.0,
3738
'options' => [],
@@ -74,12 +75,24 @@ public function reconnect(): bool
7475
$host = $this->config['host'];
7576
$port = $this->config['port'];
7677
$auth = $this->config['auth'];
78+
$cluster = $this->config['cluster'];
7779
$db = $this->config['db'];
7880
$timeout = $this->config['timeout'];
7981

80-
$redis = new \Redis();
81-
if (! $redis->connect($host, $port, $timeout)) {
82-
throw new ConnectionException('Connection reconnect failed.');
82+
$redis = null;
83+
if ($cluster !== true) {
84+
// Normal Redis (Non-cluster)
85+
$redis = new \Redis();
86+
if (! $redis->connect($host, $port, $timeout)) {
87+
throw new ConnectionException('Connection reconnect failed.');
88+
}
89+
} 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+
}
8396
}
8497

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

0 commit comments

Comments
 (0)