Skip to content

Commit

Permalink
Update: 修复代码样式
Browse files Browse the repository at this point in the history
  • Loading branch information
NHZEX committed Mar 7, 2024
1 parent 7f89a57 commit 4515862
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 39 deletions.
1 change: 1 addition & 0 deletions src/Components/redis/src/Handler/PhpRedisHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function scan(?int &$iterator, ?string $pattern = null, int $count = 0, ?
{
$args[] = $type;
}

// @phpstan-ignore-next-line
return $this->client->scan($iterator, $pattern, $count, ...$args);
}
Expand Down
52 changes: 39 additions & 13 deletions src/Components/redis/src/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,15 +278,23 @@ public static function use(callable $callable, ?string $poolName = null): mixed
public static function scan(?int &$iterator, ?string $pattern = null, int $count = 0): mixed
{
return RedisManager::use(null, static function (IConnection $connection, IRedisHandler $redis) use (&$iterator, $pattern, $count) {
if ($redis instanceof PhpRedisHandler) {
if ($redis instanceof PhpRedisHandler)
{
return $redis->scan($iterator, $pattern, $count);
} elseif ($redis instanceof PredisHandler) {
}
elseif ($redis instanceof PredisHandler)
{
[$cursor, $keys] = $redis->scan($iterator, ['match' => $pattern, 'count' => $count]);
$iterator = (int) $cursor;

return $keys;
} elseif ($redis instanceof IRedisClusterHandler) {
}
elseif ($redis instanceof IRedisClusterHandler)
{
throw new \RuntimeException('redis cluster handler not support scan proxy, please use scanEach method');
} else {
}
else
{
throw new \RuntimeException('unknown redis handler');
}
});
Expand All @@ -298,13 +306,19 @@ public static function scan(?int &$iterator, ?string $pattern = null, int $count
public static function hscan(string $key, ?int &$iterator, ?string $pattern = null, int $count = 0): mixed
{
return RedisManager::use(null, static function (IConnection $connection, IRedisHandler $redis) use ($key, &$iterator, $pattern, $count) {
if ($redis instanceof PhpRedisHandler || $redis instanceof PhpRedisClusterHandler) {
if ($redis instanceof PhpRedisHandler || $redis instanceof PhpRedisClusterHandler)
{
return $redis->hscan($key, $iterator, $pattern, $count);
} else if ($redis instanceof PredisHandler || $redis instanceof PredisClusterHandler) {
}
elseif ($redis instanceof PredisHandler || $redis instanceof PredisClusterHandler)
{
[$cursor, $keys] = $redis->hscan($key, $iterator, ['match' => $pattern, 'count' => $count]);
$iterator = (int) $cursor;

return $keys;
} else {
}
else
{
throw new \RuntimeException('redis handler not support');
}
});
Expand All @@ -316,13 +330,19 @@ public static function hscan(string $key, ?int &$iterator, ?string $pattern = nu
public static function sscan(string $key, ?int &$iterator, ?string $pattern = null, int $count = 0): mixed
{
return RedisManager::use(null, static function (IConnection $connection, IRedisHandler $redis) use ($key, &$iterator, $pattern, $count) {
if ($redis instanceof PhpRedisHandler || $redis instanceof PhpRedisClusterHandler) {
if ($redis instanceof PhpRedisHandler || $redis instanceof PhpRedisClusterHandler)
{
return $redis->sscan($key, $iterator, $pattern, $count);
} else if ($redis instanceof PredisHandler || $redis instanceof PredisClusterHandler) {
}
elseif ($redis instanceof PredisHandler || $redis instanceof PredisClusterHandler)
{
[$cursor, $keys] = $redis->sscan($key, $iterator, ['match' => $pattern, 'count' => $count]);
$iterator = (int) $cursor;

return $keys;
} else {
}
else
{
throw new \RuntimeException('redis handler not support');
}
});
Expand All @@ -334,13 +354,19 @@ public static function sscan(string $key, ?int &$iterator, ?string $pattern = nu
public static function zscan(string $key, ?int &$iterator, ?string $pattern = null, int $count = 0): mixed
{
return RedisManager::use(null, static function (IConnection $connection, IRedisHandler $redis) use ($key, &$iterator, $pattern, $count) {
if ($redis instanceof PhpRedisHandler || $redis instanceof PhpRedisClusterHandler) {
if ($redis instanceof PhpRedisHandler || $redis instanceof PhpRedisClusterHandler)
{
return $redis->zscan($key, $iterator, $pattern, $count);
} else if ($redis instanceof PredisHandler || $redis instanceof PredisClusterHandler) {
}
elseif ($redis instanceof PredisHandler || $redis instanceof PredisClusterHandler)
{
[$cursor, $keys] = $redis->zscan($key, $iterator, ['match' => $pattern, 'count' => $count]);
$iterator = (int) $cursor;

return $keys;
} else {
}
else
{
throw new \RuntimeException('redis handler not support');
}
});
Expand Down
69 changes: 43 additions & 26 deletions src/Components/redis/tests/Tests/PhpRedisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@
use Imi\Redis\Handler\PhpRedisHandler;
use Imi\Redis\Redis;
use Imi\Redis\RedisManager;
use Imi\RequestContext;
use PHPUnit\Framework\Attributes\Depends;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;
use Predis\Collection\Iterator\Keyspace;

/**
* @template T of PhpRedisHandler
Expand Down Expand Up @@ -195,9 +193,12 @@ protected function staticContextWarp(callable $fn): void
// Config::get()
self::assertTrue(Config::set('@app.redis.defaultPool', $this->driveName));
self::assertEquals($this->driveName, RedisManager::getDefaultPoolName());
try {
try
{
$fn();
} finally {
}
finally
{
Config::set('@app.redis.defaultPool', $defaultName);
}
}
Expand All @@ -206,7 +207,7 @@ protected function staticContextWarp(callable $fn): void
public function testStaticCall(): void
{
$prefix = __FUNCTION__ . bin2hex(random_bytes(4));
$this->staticContextWarp(function () use ($prefix) {
$this->staticContextWarp(static function () use ($prefix): void {
Redis::set($prefix, '123456');
self::assertEquals('123456', Redis::get($prefix));
self::assertTrue(Redis::del($prefix) > 0);
Expand All @@ -219,12 +220,13 @@ public function testStaticCall(): void
#[Depends('testGetDrive')]
public function testStaticCallScan(IRedisHandler $redis): void
{
if ($redis instanceof IRedisClusterHandler) {
if ($redis instanceof IRedisClusterHandler)
{
self::markTestSkipped('RedisClusterHandler does not support hscan');
}

$prefix = __FUNCTION__ . bin2hex(random_bytes(4));
$this->staticContextWarp(function () use ($redis, $prefix) {
$this->staticContextWarp(static function () use ($redis, $prefix): void {
$excepted = $map = [];
for ($i = 0; $i < 100; ++$i)
{
Expand All @@ -235,14 +237,17 @@ public function testStaticCallScan(IRedisHandler $redis): void
}

$map = [];
do {
do
{
$keys = Redis::scan($it, $prefix . ':scanEach:*', 10);
foreach ($keys as $key) {
foreach ($keys as $key)
{
$map[$key] = 1;
}
} while ($it != 0);
}
while (0 != $it);
self::assertEquals($excepted, $map);
self::assertTrue(Redis::del(\array_keys($excepted)) > 0);
self::assertTrue(Redis::del(array_keys($excepted)) > 0);
});
}

Expand All @@ -252,12 +257,13 @@ public function testStaticCallScan(IRedisHandler $redis): void
#[Depends('testGetDrive')]
public function testStaticCallHScan(IRedisHandler $redis): void
{
if ($redis instanceof IRedisClusterHandler) {
if ($redis instanceof IRedisClusterHandler)
{
self::markTestSkipped('RedisClusterHandler does not support hscan');
}

$prefix = __FUNCTION__ . bin2hex(random_bytes(4));
$this->staticContextWarp(function () use ($redis, $prefix) {
$this->staticContextWarp(static function () use ($redis, $prefix): void {
$excepted = $map = $values = $exceptedValues = [];
$key = $prefix . ':hscanEach';
$redis->del($key);
Expand All @@ -270,13 +276,16 @@ public function testStaticCallHScan(IRedisHandler $redis): void
$exceptedValues[$member] = $i;
$redis->hSet($key, $member, $i);
}
do {
do
{
$items = Redis::hscan($key, $it, 'value:*', 10);
foreach ($items as $k => $value) {
foreach ($items as $k => $value)
{
$map[$k] = 1;
$values[$k] = $value;
}
} while ($it > 0);
}
while ($it > 0);
self::assertEquals($excepted, $map);
self::assertEquals($exceptedValues, $values);
self::assertTrue(Redis::del($key) > 0);
Expand All @@ -289,12 +298,13 @@ public function testStaticCallHScan(IRedisHandler $redis): void
#[Depends('testGetDrive')]
public function testStaticCallSScan(IRedisHandler $redis): void
{
if ($redis instanceof IRedisClusterHandler) {
if ($redis instanceof IRedisClusterHandler)
{
self::markTestSkipped('RedisClusterHandler does not support sscan');
}

$prefix = __FUNCTION__ . bin2hex(random_bytes(4));
$this->staticContextWarp(function () use ($redis, $prefix) {
$this->staticContextWarp(static function () use ($redis, $prefix): void {
$excepted = $map = [];
$key = $prefix . ':sscanEach';
$redis->del($key);
Expand All @@ -305,12 +315,15 @@ public function testStaticCallSScan(IRedisHandler $redis): void
$map[$value] = 0;
$redis->sAdd($key, $value);
}
do {
do
{
$items = Redis::sscan($key, $it, '*', 10);
foreach ($items as $value) {
foreach ($items as $value)
{
$map[$value] = 1;
}
} while ($it > 0);
}
while ($it > 0);
self::assertEquals($excepted, $map);
self::assertTrue(Redis::del($key) > 0);
});
Expand All @@ -322,12 +335,13 @@ public function testStaticCallSScan(IRedisHandler $redis): void
#[Depends('testGetDrive')]
public function testStaticCallZScan(IRedisHandler $redis): void
{
if ($redis instanceof IRedisClusterHandler) {
if ($redis instanceof IRedisClusterHandler)
{
self::markTestSkipped('RedisClusterHandler does not support zscan');
}

$prefix = __FUNCTION__ . bin2hex(random_bytes(4));
$this->staticContextWarp(function () use ($redis, $prefix) {
$this->staticContextWarp(static function () use ($redis, $prefix): void {
$excepted = $map = [];
$key = $prefix . ':zscanEach';
$redis->del($key);
Expand All @@ -338,12 +352,15 @@ public function testStaticCallZScan(IRedisHandler $redis): void
$map[$i] = 0;
$redis->zAdd($key, $i, $value);
}
do {
do
{
$items = Redis::zscan($key, $it, '*', 10);
foreach ($items as $score) {
foreach ($items as $score)
{
$map[$score] = 1;
}
} while ($it > 0);
}
while ($it > 0);
self::assertEquals($excepted, $map);
self::assertTrue(Redis::del($key) > 0);
});
Expand Down

0 comments on commit 4515862

Please sign in to comment.