From 91f3fb979e1a2912028bd5dc9a55319762827293 Mon Sep 17 00:00:00 2001 From: zxin <14545600+NHZEX@users.noreply.github.com> Date: Tue, 9 Apr 2024 08:17:14 +0800 Subject: [PATCH] =?UTF-8?q?Update:=20=E4=BF=AE=E5=A4=8DCache=E5=89=8D?= =?UTF-8?q?=E7=BC=80=E9=80=89=E9=A1=B9=E4=B8=8EsetMultiple=E4=B8=8D?= =?UTF-8?q?=E5=85=BC=E5=AE=B9=E9=97=AE=E9=A2=98=20(#690)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Cache/Handler/Redis.php | 22 +++++----- tests/unit/Component/Tests/CacheRedisTest.php | 40 +++++++++++++++++++ 2 files changed, 53 insertions(+), 9 deletions(-) diff --git a/src/Cache/Handler/Redis.php b/src/Cache/Handler/Redis.php index 2da4528356..7012d0b00c 100644 --- a/src/Cache/Handler/Redis.php +++ b/src/Cache/Handler/Redis.php @@ -84,11 +84,14 @@ public function clear() public function getMultiple($keys, $default = null) { $this->checkArrayOrTraversable($keys); - foreach ($keys as &$key) + $newKeys = []; + $parsedKeys = []; + foreach ($keys as $key) { - $key = $this->parseKey($key); + $parsedKeys[] = $this->parseKey($key); + $newKeys[] = $key; } - $mgetResult = ImiRedis::use(static fn (\Imi\Redis\RedisHandler $redis) => $redis->mget($keys), $this->poolName, true); + $mgetResult = ImiRedis::use(static fn (\Imi\Redis\RedisHandler $redis) => $redis->mget($parsedKeys), $this->poolName, true); $result = []; if ($mgetResult) { @@ -96,11 +99,11 @@ public function getMultiple($keys, $default = null) { if (false === $v) { - $result[$keys[$i]] = $default; + $result[$newKeys[$i]] = $default; } else { - $result[$keys[$i]] = $this->decode($v); + $result[$newKeys[$i]] = $this->decode($v); } } } @@ -122,21 +125,22 @@ public function setMultiple($values, $ttl = null) { $setValues = $values; } + $values = []; foreach ($setValues as $k => $v) { - $setValues[$this->parseKey((string) $k)] = $this->encode($v); + $values[$this->parseKey((string) $k)] = $this->encode($v); } // ttl 支持 \DateInterval 格式 if ($ttl instanceof \DateInterval) { $ttl = DateTime::getSecondsByInterval($ttl); } - $result = ImiRedis::use(static function (\Imi\Redis\RedisHandler $redis) use ($setValues, $ttl) { + $result = ImiRedis::use(static function (\Imi\Redis\RedisHandler $redis) use ($values, $ttl) { $redis->multi(); - $redis->mset($setValues); + $redis->mset($values); if (null !== $ttl) { - foreach ($setValues as $k => $v) + foreach ($values as $k => $v) { $redis->expire((string) $k, $ttl); } diff --git a/tests/unit/Component/Tests/CacheRedisTest.php b/tests/unit/Component/Tests/CacheRedisTest.php index 1ec8526425..b7a55c7ba1 100644 --- a/tests/unit/Component/Tests/CacheRedisTest.php +++ b/tests/unit/Component/Tests/CacheRedisTest.php @@ -4,10 +4,50 @@ namespace Imi\Test\Component\Tests; +use Imi\Bean\BeanFactory; +use Imi\Cache\Handler\Redis; +use PHPUnit\Framework\Assert; + /** * @testdox Cache Redis Handler */ class CacheRedisTest extends BaseCacheTest { protected string $cacheName = 'redis'; + + public function testMultipleWithPrefix(): void + { + $cache = BeanFactory::newInstance(Redis::class, [ + 'poolName' => 'redis_test', + 'prefix' => 'imi-test:', + '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)); + + $values = [ + 'k1' => 'v1' . $value, + 'k2' => 'v2' . $value, + ]; + Assert::assertTrue($cache->setMultiple($values, 1)); + $getValues = $cache->getMultiple(array_keys_string($values)); + Assert::assertEquals($values, $getValues); + sleep(2); + Assert::assertEquals([ + 'k1' => 'none', + 'k2' => 'none', + ], $cache->getMultiple(array_keys_string($values), 'none')); + }, null, 3); + } }