Skip to content

Commit

Permalink
Update: 实现 flushdb, flushall 增强
Browse files Browse the repository at this point in the history
  • Loading branch information
NHZEX committed Mar 13, 2024
1 parent 69c02a4 commit 50b3b21
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 56 deletions.
2 changes: 1 addition & 1 deletion src/Components/redis/src/Handler/AbstractRedisHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Imi\Redis\Connector\RedisDriverConfig;

abstract class AbstractRedisHandler implements IRedisScanMethod, IRedisHandler
abstract class AbstractRedisHandler implements IRedisHandler
{
protected RedisDriverConfig $config;

Expand Down
12 changes: 12 additions & 0 deletions src/Components/redis/src/Handler/IRedisHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ public function isCluster(): bool;

public function isSupportSerialize(): bool;

public function flushdbEx(): bool;

public function flushallEx(): bool;

public function scanEach(?string $pattern = null, int $count = 0): \Generator;

public function hscanEach(string $key, ?string $pattern = null, int $count = 0): \Generator;

public function sscanEach(string $key, ?string $pattern = null, int $count = 0): \Generator;

public function zscanEach(string $key, ?string $pattern = null, int $count = 0): \Generator;

public function _serialize(mixed $value): ?string;

public function _unserialize(?string $value): mixed;
Expand Down
16 changes: 0 additions & 16 deletions src/Components/redis/src/Handler/IRedisScanMethod.php

This file was deleted.

20 changes: 20 additions & 0 deletions src/Components/redis/src/Handler/PhpRedisClusterHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,26 @@ public function isConnected(): bool
return true;
}

public function flushdbEx(): bool
{
foreach ($this->getNodes() as $node)
{
$this->client->flushDB($node);
}

return true;
}

public function flushallEx(): bool
{
foreach ($this->getNodes() as $node)
{
$this->client->flushAll($node);
}

return true;
}

public function scan(?int &$iterator, array|string $node, ?string $pattern = null, int $count = 0): array|false
{
// @phpstan-ignore-next-line
Expand Down
10 changes: 10 additions & 0 deletions src/Components/redis/src/Handler/PhpRedisHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ public function isConnected(): bool
return true === $result || '+PONG' === $result;
}

public function flushdbEx(): bool
{
return $this->client->flushDB();
}

public function flushallEx(): bool
{
return $this->client->flushAll();
}

public function scan(?int &$iterator, ?string $pattern = null, int $count = 0, ?string $type = null): mixed
{
$args = [];
Expand Down
26 changes: 26 additions & 0 deletions src/Components/redis/src/Handler/PredisClusterHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,32 @@ public function isConnected(): bool
return true;
}

public function flushdbEx(): bool
{
foreach ($this->getNodes() as $node)
{
$client = $this->client->getClientBy('id', "{$node[0]}:{$node[1]}");
if ('OK' !== (string) $client->flushdb())
{
return false;
}
}
return true;
}

public function flushallEx(): bool
{
foreach ($this->getNodes() as $node)
{
$client = $this->client->getClientBy('id', "{$node[0]}:{$node[1]}");
if ('OK' !== (string) $client->flushall())
{
return false;
}
}
return true;
}

public function __call(string $name, array $arguments): mixed
{
$result = $this->client->{$name}(...$arguments);
Expand Down
10 changes: 10 additions & 0 deletions src/Components/redis/src/Handler/PredisHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ public function isConnected(): bool
return 'PONG' === (string) $this->client->ping();
}

public function flushdbEx(): bool
{
return 'OK' === (string) $this->client->flushdb();
}

public function flushallEx(): bool
{
return 'OK' === (string) $this->client->flushall();
}

public function __call(string $name, array $arguments): mixed
{
$result = $this->client->{$name}(...$arguments);
Expand Down
14 changes: 1 addition & 13 deletions src/Components/redis/tests/Tests/PhpRedisClusterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,11 @@ public function testGetDrive(): IRedisHandler
self::assertInstanceOf(PhpRedisClusterHandler::class, $redisClient);
self::assertInstanceOf(\RedisCluster::class, $redisClient->getInstance());

$this->flush($redisClient);
self::assertTrue($redisClient->flushdbEx());

return $redisClient;
}

/**
* @phpstan-param T $redis
*/
protected function flush(IRedisHandler $redis): void
{
// 清空数据
foreach ($redis->getNodes() as $node)
{
self::assertTrue($redis->flushdb($node, false));
}
}

/**
* @phpstan-param T $redis
*/
Expand Down
10 changes: 1 addition & 9 deletions src/Components/redis/tests/Tests/PhpRedisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,11 @@ public function testGetDrive(): IRedisHandler
self::assertInstanceOf(\Redis::class, $redisClient->getInstance());

// 清空数据
$this->flush($redisClient);
self::assertTrue($redisClient->flushdbEx());

return $redisClient;
}

/**
* @phpstan-param T $redis
*/
protected function flush(IRedisHandler $redis): void
{
$redis->flushdb(false);
}

/**
* @phpstan-param T $redis
*/
Expand Down
16 changes: 1 addition & 15 deletions src/Components/redis/tests/Tests/PredisClusterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,11 @@ public function testGetDrive(): IRedisHandler
self::assertInstanceOf(PredisClusterHandler::class, $redisClient);
self::assertInstanceOf(Client::class, $redisClient->getInstance());

$this->flush($redisClient);
self::assertTrue($redisClient->flushdbEx());

return $redisClient;
}

/**
* @phpstan-param T $redis
*/
protected function flush(IRedisHandler $redis): void
{
// 清空数据
foreach ($redis->getNodes() as $node)
{
$client = $redis->getClientBy('id', "{$node[0]}:{$node[1]}");
$result = $client->flushdb();
self::assertTrue($result instanceof \Predis\Response\Status && 'OK' === (string) $result);
}
}

/**
* @phpstan-param T $redis
*/
Expand Down
2 changes: 2 additions & 0 deletions src/Components/redis/tests/Tests/PredisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public function testGetDrive(): IRedisHandler
self::assertInstanceOf(PredisHandler::class, $redisClient);
self::assertInstanceOf(Client::class, $redisClient->getInstance());

self::assertTrue($redisClient->flushdbEx());

return $redisClient;
}

Expand Down
2 changes: 0 additions & 2 deletions src/Components/redis/tests/Tests/RedisHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
use Imi\Redis\Connector\PredisConnector;
use Imi\Redis\Connector\RedisDriverConfig;
use Imi\Redis\Enum\RedisMode;
use Imi\Redis\Handler\IRedisClusterHandler;
use Imi\Redis\Handler\IRedisHandler;
use Imi\Redis\Handler\PhpRedisClusterHandler;
use Imi\Redis\Handler\PhpRedisHandler;
use Imi\Redis\Handler\PredisClusterHandler;
Expand Down

0 comments on commit 50b3b21

Please sign in to comment.