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 26657dd commit e25bfab
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/Cache/Handler/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,27 @@ public function getMultiple(iterable $keys, mixed $default = null): iterable
{
$key = $this->parseKey($key);
}
unset($key);
$mgetResult = ImiRedis::use(static fn (\Imi\Redis\RedisHandler $redis) => $redis->mget($keys), $this->poolName, true);
$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)
{
$result[$keys[$i]] = $default;
$result[$key] = $default;
}
else
{
$result[$keys[$i]] = $this->decode($v);
$result[$key] = $this->decode($v);
}
}
}
Expand All @@ -114,21 +122,22 @@ public function setMultiple(iterable $values, null|int|\DateInterval $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
30 changes: 30 additions & 0 deletions tests/unit/Component/Tests/CacheRedisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,40 @@

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 BaseCacheTestCase
{
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,
]);

$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 e25bfab

Please sign in to comment.