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); + } }