From 935a8874d2b2910b92ac9d4aa76a8dab2be5f328 Mon Sep 17 00:00:00 2001 From: zxin <14545600+NHZEX@users.noreply.github.com> Date: Sat, 6 Apr 2024 21:37:05 +0800 Subject: [PATCH] =?UTF-8?q?Update:=20=E4=BC=98=E5=8C=96=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Cache/Handler/Redis.php | 10 ++++++---- tests/unit/Component/Tests/CacheRedisTest.php | 10 ++++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/Cache/Handler/Redis.php b/src/Cache/Handler/Redis.php index 4172b9d67..432f8c2f2 100644 --- a/src/Cache/Handler/Redis.php +++ b/src/Cache/Handler/Redis.php @@ -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 = []; @@ -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); } } } diff --git a/tests/unit/Component/Tests/CacheRedisTest.php b/tests/unit/Component/Tests/CacheRedisTest.php index ad1d6c504..31c44aaa8 100644 --- a/tests/unit/Component/Tests/CacheRedisTest.php +++ b/tests/unit/Component/Tests/CacheRedisTest.php @@ -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));