fix: allow touching expired cache entries

This commit is contained in:
Torsten Dittmann committed 2026-05-08 16:42:37 +04:00
1 parent d401e64ea1
commit c6293e25f0
6 files changed
+64 -26

No files matched your search

+8 -6
View File
@@ -94,16 +94,18 @@ class Hazelcast implements Adapter
*/
public function touch(string $key, string $hash = ''): bool
{
$data = $this->load($key, PHP_INT_MAX, $hash);
if ($data === false) {
$cache = $this->execute(fn () => $this->memcached->get($key));
if (is_string($cache)) {
$cache = json_decode($cache, true);
}
if (! is_array($cache)) {
return false;
}
if (! is_string($data) && ! is_array($data)) {
return false;
}
$cache['time'] = time();
return $this->save($key, $data, $hash) !== false;
return (bool) $this->execute(fn () => $this->memcached->set($key, json_encode($cache)));
}
/**
+5 -6
View File
@@ -96,16 +96,15 @@ class Memcached implements Adapter
*/
public function touch(string $key, string $hash = ''): bool
{
$data = $this->load($key, PHP_INT_MAX, $hash);
if ($data === false) {
/** @var array{time: int, data: string|array<int|string, mixed>}|false */
$cache = $this->execute(fn () => $this->memcached->get($key));
if ($cache === false) {
return false;
}
if (! is_string($data) && ! is_array($data)) {
return false;
}
$cache['time'] = time();
return $this->save($key, $data, $hash) !== false;
return (bool) $this->execute(fn () => $this->memcached->set($key, $cache));
}
/**
+6 -6
View File
@@ -83,16 +83,16 @@ class Memory implements Adapter
*/
public function touch(string $key, string $hash = ''): bool
{
$data = $this->load($key, PHP_INT_MAX, $hash);
if ($data === false) {
if (empty($key) || ! isset($this->store[$key])) {
return false;
}
if (! is_string($data) && ! is_array($data)) {
return false;
}
/** @var array{time: int, data: string|array<int|string, mixed>} $saved */
$saved = $this->store[$key];
$saved['time'] = time();
$this->store[$key] = $saved;
return $this->save($key, $data, $hash) !== false;
return true;
}
/**
+14 -4
View File
@@ -159,16 +159,26 @@ class Redis implements Adapter
*/
public function touch(string $key, string $hash = ''): bool
{
$data = $this->load($key, PHP_INT_MAX, $hash);
if ($data === false) {
if (empty($hash)) {
$hash = $key;
}
$redis_string = $this->execute(fn () => $this->redis->hGet($key, $hash));
if ($redis_string === false || $redis_string === null || ! is_string($redis_string)) {
return false;
}
if (! is_string($data) && ! is_array($data)) {
try {
/** @var array{time: int, data: mixed} $cache */
$cache = json_decode($redis_string, true, flags: JSON_THROW_ON_ERROR);
$cache['time'] = time();
$value = json_encode($cache, flags: JSON_THROW_ON_ERROR);
} catch (Throwable $th) {
return false;
}
return $this->save($key, $data, $hash) !== false;
return $this->execute(fn () => $this->redis->hSet($key, $hash, $value)) !== false;
}
/**
+15 -4
View File
@@ -158,16 +158,27 @@ class RedisCluster implements Adapter
*/
public function touch(string $key, string $hash = ''): bool
{
$data = $this->load($key, PHP_INT_MAX, $hash);
if ($data === false) {
if (empty($hash)) {
$hash = $key;
}
/** @var string|false */
$redis_string = $this->execute(fn () => $this->redis->hGet($key, $hash));
if ($redis_string === false || ! is_string($redis_string)) {
return false;
}
if (! is_string($data) && ! is_array($data)) {
try {
/** @var array{time: int, data: mixed} $cache */
$cache = json_decode($redis_string, true, flags: JSON_THROW_ON_ERROR);
$cache['time'] = time();
$value = json_encode($cache, flags: JSON_THROW_ON_ERROR);
} catch (Throwable $th) {
return false;
}
return $this->save($key, $data, $hash) !== false;
return $this->execute(fn () => $this->redis->hSet($key, $hash, $value)) !== false;
}
/**
+16
View File
@@ -20,4 +20,20 @@ class MemoryTest extends Base
self::$cache->save('test:file36', 'file36');
$this->assertEquals(4, self::$cache->getSize());
}
public function testTouchExpiredEntry(): void
{
$adapter = new Memory();
$cache = new Cache($adapter);
$cache->save('expired-touch-key', 'expired data');
/** @var array{time: int, data: string} $stored */
$stored = $adapter->store['expired-touch-key'];
$stored['time'] = time() - 10;
$adapter->store['expired-touch-key'] = $stored;
$this->assertEquals(false, $cache->load('expired-touch-key', 1));
$this->assertEquals(true, $cache->touch('expired-touch-key'));
$this->assertEquals('expired data', $cache->load('expired-touch-key', 1));
}
}