Skip to content

Commit

Permalink
Update: 优化 Cache Redis 修复补丁
Browse files Browse the repository at this point in the history
  • Loading branch information
NHZEX committed Apr 6, 2024
1 parent 35beae6 commit 20b6e6f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
21 changes: 8 additions & 13 deletions src/Cache/Handler/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,31 +108,26 @@ public function clear(): bool
*/
public function getMultiple(iterable $keys, mixed $default = null): iterable
{
foreach ($keys as &$key)
$newKeys = [];
$parsedKeys = [];
foreach ($keys as $key)
{
$key = $this->parseKey($key);
$parsedKeys[] = $this->parseKey($key);
$newKeys[] = $key;
}
unset($key);
$mgetResult = $this->redisManager->getInstance()->mget($keys);
$mgetResult = $this->redisManager->getInstance()->mget($parsedKeys);
$result = [];
if ($mgetResult)
{
foreach ($mgetResult as $i => $v)
{
$key = $keys[$i];

if ($this->prefix && str_starts_with((string) $key, $this->prefix))
{
$key = substr((string) $key, \strlen($this->prefix));
}

if (false === $v || null === $v)
{
$result[$key] = $default;
$result[$newKeys[$i]] = $default;
}
else
{
$result[$key] = $this->decode($v);
$result[$newKeys[$i]] = $this->decode($v);
}
}
}
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/Component/Tests/Cache/RedisCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,27 @@ public function testSetMultiple(string $name): void
Assert::assertEquals($values, $getValues);
}

#[DataProvider('redisConnectionProvider')]
public function testSetMultipleKeys(string $name): void
{
if ('predis_cluster' === $name)
{
$this->expectExceptionMessage('predis cluster not support setMultiple method');
}
$value = bin2hex(random_bytes(8));

$values = [
'k1' => 'v1' . $value,
'k2' => 'v2' . $value,
'k3' => 'v3' . $value,
];
$cache = $this->getCacheHandler($name);

Assert::assertTrue($cache->setMultiple($values));
$getValues = $cache->getMultiple([0 => 'k1', 2 => 'k2', 'A' => 'k3']);
Assert::assertEquals($values, $getValues);
}

/**
* @testdox Set multiple TTL
*/
Expand Down

0 comments on commit 20b6e6f

Please sign in to comment.