Skip to content

Commit

Permalink
Update: 优化实现
Browse files Browse the repository at this point in the history
  • Loading branch information
NHZEX authored Apr 6, 2024
1 parent 9060264 commit 935a887
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/Cache/Handler/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,12 @@ public function clear(): bool
*/
public function getMultiple(iterable $keys, mixed $default = null): iterable
{
$newKeys = [];
$parsedKeys = [];
foreach ($keys as $i => $key)
foreach ($keys as $key)
{
$parsedKeys[$i] = $this->parseKey($key);
$parsedKeys[] = $this->parseKey($key);
$newKeys[] = $key;
}
$mgetResult = ImiRedis::use(static fn (\Imi\Redis\RedisHandler $redis) => $redis->mget($parsedKeys), $this->poolName, true);
$result = [];
Expand All @@ -90,11 +92,11 @@ public function getMultiple(iterable $keys, mixed $default = null): iterable
{
if (false === $v)
{
$result[$keys[$i]] = $default;
$result[$newKeys[$i]] = $default;
}
else
{
$result[$keys[$i]] = $this->decode($v);
$result[$newKeys[$i]] = $this->decode($v);
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/Component/Tests/CacheRedisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ public function testMultipleWithPrefix(): void
'formatHandlerClass' => \Imi\Util\Format\Json::class,
]);

$value = bin2hex(random_bytes(8));
$values = [
'k1' => 'v1' . $value,
'k2' => 'v2' . $value,
'k3' => 'v3' . $value,
];
Assert::assertTrue($cache->setMultiple($values));
$getValues = $cache->getMultiple([0 => 'k1', 2 => 'k2', 'A' => 'k3']);
Assert::assertEquals($values, $getValues);

$this->go(static function () use ($cache): void {
$value = bin2hex(random_bytes(8));

Expand Down

0 comments on commit 935a887

Please sign in to comment.