Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.0] 修复Cache前缀选项与setMultiple不兼容问题 #688

Merged
merged 4 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions src/Cache/Handler/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,26 @@ public function clear(): bool
*/
public function getMultiple(iterable $keys, mixed $default = null): iterable
{
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 @@ -114,21 +117,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
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 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,
]);

$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);
}
}
Loading