Skip to content

Commit f4e8d5c

Browse files
committed
support flush db on clusters
1 parent f7caed6 commit f4e8d5c

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

src/Illuminate/Redis/Connections/PhpRedisConnection.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Redis;
66
use Closure;
7+
use RedisCluster;
78
use Illuminate\Contracts\Redis\Connection as ConnectionContract;
89

910
/**
@@ -388,6 +389,22 @@ public function createSubscription($channels, Closure $callback, $method = 'subs
388389
//
389390
}
390391

392+
/**
393+
* Flush the selected Redis database.
394+
*
395+
* @return void
396+
*/
397+
public function flushdb()
398+
{
399+
if (! $this->client instanceof RedisCluster) {
400+
return $this->command('flushdb');
401+
}
402+
403+
foreach ($this->client->_masters() as [$host, $port]) {
404+
tap(new Redis)->connect($host, $port)->flushDb();
405+
}
406+
}
407+
391408
/**
392409
* Execute a raw command.
393410
*

src/Illuminate/Redis/Connections/PredisConnection.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Illuminate\Redis\Connections;
44

55
use Closure;
6+
use Predis\Command\ServerFlushDatabase;
7+
use Predis\Connection\Aggregate\PredisCluster;
68
use Illuminate\Contracts\Redis\Connection as ConnectionContract;
79

810
/**
@@ -43,4 +45,20 @@ public function createSubscription($channels, Closure $callback, $method = 'subs
4345

4446
unset($loop);
4547
}
48+
49+
/**
50+
* Flush the selected Redis database.
51+
*
52+
* @return void
53+
*/
54+
public function flushdb()
55+
{
56+
if (! $this->client->getConnection() instanceof PredisCluster) {
57+
return $this->command('flushdb');
58+
}
59+
60+
foreach ($this->getConnection() as $node) {
61+
$node->executeCommand(new ServerFlushDatabase);
62+
}
63+
}
4664
}

0 commit comments

Comments
 (0)