From 50b3b21c2d1a128ec215d2150ab6be3460f1480c Mon Sep 17 00:00:00 2001 From: auooru Date: Wed, 13 Mar 2024 20:13:38 +0800 Subject: [PATCH] =?UTF-8?q?Update:=20=E5=AE=9E=E7=8E=B0=20flushdb,=20flush?= =?UTF-8?q?all=20=E5=A2=9E=E5=BC=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/Handler/AbstractRedisHandler.php | 2 +- .../redis/src/Handler/IRedisHandler.php | 12 +++++++++ .../redis/src/Handler/IRedisScanMethod.php | 16 ------------ .../src/Handler/PhpRedisClusterHandler.php | 20 ++++++++++++++ .../redis/src/Handler/PhpRedisHandler.php | 10 +++++++ .../src/Handler/PredisClusterHandler.php | 26 +++++++++++++++++++ .../redis/src/Handler/PredisHandler.php | 10 +++++++ .../redis/tests/Tests/PhpRedisClusterTest.php | 14 +--------- .../redis/tests/Tests/PhpRedisTest.php | 10 +------ .../redis/tests/Tests/PredisClusterTest.php | 16 +----------- .../redis/tests/Tests/PredisTest.php | 2 ++ .../redis/tests/Tests/RedisHandlerTest.php | 2 -- 12 files changed, 84 insertions(+), 56 deletions(-) delete mode 100644 src/Components/redis/src/Handler/IRedisScanMethod.php diff --git a/src/Components/redis/src/Handler/AbstractRedisHandler.php b/src/Components/redis/src/Handler/AbstractRedisHandler.php index 6e9701cea..bb33d74ce 100644 --- a/src/Components/redis/src/Handler/AbstractRedisHandler.php +++ b/src/Components/redis/src/Handler/AbstractRedisHandler.php @@ -6,7 +6,7 @@ use Imi\Redis\Connector\RedisDriverConfig; -abstract class AbstractRedisHandler implements IRedisScanMethod, IRedisHandler +abstract class AbstractRedisHandler implements IRedisHandler { protected RedisDriverConfig $config; diff --git a/src/Components/redis/src/Handler/IRedisHandler.php b/src/Components/redis/src/Handler/IRedisHandler.php index ae7791792..364c3035c 100644 --- a/src/Components/redis/src/Handler/IRedisHandler.php +++ b/src/Components/redis/src/Handler/IRedisHandler.php @@ -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; diff --git a/src/Components/redis/src/Handler/IRedisScanMethod.php b/src/Components/redis/src/Handler/IRedisScanMethod.php deleted file mode 100644 index 4d15ff0c9..000000000 --- a/src/Components/redis/src/Handler/IRedisScanMethod.php +++ /dev/null @@ -1,16 +0,0 @@ -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 diff --git a/src/Components/redis/src/Handler/PhpRedisHandler.php b/src/Components/redis/src/Handler/PhpRedisHandler.php index 2aef7809a..23be91572 100644 --- a/src/Components/redis/src/Handler/PhpRedisHandler.php +++ b/src/Components/redis/src/Handler/PhpRedisHandler.php @@ -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 = []; diff --git a/src/Components/redis/src/Handler/PredisClusterHandler.php b/src/Components/redis/src/Handler/PredisClusterHandler.php index 031f360d3..35e2cbe45 100644 --- a/src/Components/redis/src/Handler/PredisClusterHandler.php +++ b/src/Components/redis/src/Handler/PredisClusterHandler.php @@ -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); diff --git a/src/Components/redis/src/Handler/PredisHandler.php b/src/Components/redis/src/Handler/PredisHandler.php index 2b57bdddf..e16a359e7 100644 --- a/src/Components/redis/src/Handler/PredisHandler.php +++ b/src/Components/redis/src/Handler/PredisHandler.php @@ -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); diff --git a/src/Components/redis/tests/Tests/PhpRedisClusterTest.php b/src/Components/redis/tests/Tests/PhpRedisClusterTest.php index d478be05b..e129eba79 100644 --- a/src/Components/redis/tests/Tests/PhpRedisClusterTest.php +++ b/src/Components/redis/tests/Tests/PhpRedisClusterTest.php @@ -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 */ diff --git a/src/Components/redis/tests/Tests/PhpRedisTest.php b/src/Components/redis/tests/Tests/PhpRedisTest.php index 1386169e3..e8fbce808 100644 --- a/src/Components/redis/tests/Tests/PhpRedisTest.php +++ b/src/Components/redis/tests/Tests/PhpRedisTest.php @@ -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 */ diff --git a/src/Components/redis/tests/Tests/PredisClusterTest.php b/src/Components/redis/tests/Tests/PredisClusterTest.php index c2d5c6df8..3bec40b9d 100644 --- a/src/Components/redis/tests/Tests/PredisClusterTest.php +++ b/src/Components/redis/tests/Tests/PredisClusterTest.php @@ -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 */ diff --git a/src/Components/redis/tests/Tests/PredisTest.php b/src/Components/redis/tests/Tests/PredisTest.php index 1ecba6e06..cd8110396 100644 --- a/src/Components/redis/tests/Tests/PredisTest.php +++ b/src/Components/redis/tests/Tests/PredisTest.php @@ -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; } diff --git a/src/Components/redis/tests/Tests/RedisHandlerTest.php b/src/Components/redis/tests/Tests/RedisHandlerTest.php index e48a51fb6..d4da30dbe 100644 --- a/src/Components/redis/tests/Tests/RedisHandlerTest.php +++ b/src/Components/redis/tests/Tests/RedisHandlerTest.php @@ -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;