Skip to content

Commit

Permalink
Update: 修复Cache前缀选项与setMultiple不兼容问题
Browse files Browse the repository at this point in the history
  • Loading branch information
NHZEX authored Apr 6, 2024
1 parent 55eabb4 commit 896aec8
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/Cache/Handler/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,23 +84,26 @@ 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)
{
foreach ($mgetResult as $i => $v)
{
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 All @@ -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);
}
Expand Down
40 changes: 40 additions & 0 deletions tests/unit/Component/Tests/CacheRedisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 896aec8

Please sign in to comment.