diff --git a/src/Cache/Adapter/Hazelcast.php b/src/Cache/Adapter/Hazelcast.php index c43e3957f5..6818c069bb 100644 --- a/src/Cache/Adapter/Hazelcast.php +++ b/src/Cache/Adapter/Hazelcast.php @@ -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))); } /** diff --git a/src/Cache/Adapter/Memcached.php b/src/Cache/Adapter/Memcached.php index b5d040b837..b80aa3fec0 100644 --- a/src/Cache/Adapter/Memcached.php +++ b/src/Cache/Adapter/Memcached.php @@ -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}|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)); } /** diff --git a/src/Cache/Adapter/Memory.php b/src/Cache/Adapter/Memory.php index b4e99bb613..bb4a82a92d 100644 --- a/src/Cache/Adapter/Memory.php +++ b/src/Cache/Adapter/Memory.php @@ -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} $saved */ + $saved = $this->store[$key]; + $saved['time'] = time(); + $this->store[$key] = $saved; - return $this->save($key, $data, $hash) !== false; + return true; } /** diff --git a/src/Cache/Adapter/Redis.php b/src/Cache/Adapter/Redis.php index 5b48a5abd0..654da4cffa 100644 --- a/src/Cache/Adapter/Redis.php +++ b/src/Cache/Adapter/Redis.php @@ -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; } /** diff --git a/src/Cache/Adapter/RedisCluster.php b/src/Cache/Adapter/RedisCluster.php index 0056a77051..7dcd4b51d5 100644 --- a/src/Cache/Adapter/RedisCluster.php +++ b/src/Cache/Adapter/RedisCluster.php @@ -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; } /** diff --git a/tests/Cache/MemoryTest.php b/tests/Cache/MemoryTest.php index b3e2a7683b..6b99ffd117 100644 --- a/tests/Cache/MemoryTest.php +++ b/tests/Cache/MemoryTest.php @@ -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)); + } }