diff --git a/src/Cache/Adapter.php b/src/Cache/Adapter.php index a1ea41e2b7..91aa22d68a 100644 --- a/src/Cache/Adapter.php +++ b/src/Cache/Adapter.php @@ -24,6 +24,13 @@ interface Adapter */ public function save(string $key, array|string $data, string $hash = ''): bool|string|array; + /** + * @param string $key + * @param string $hash optional + * @return bool + */ + public function touch(string $key, string $hash = ''): bool; + /** * @param string $key * @return string[] diff --git a/src/Cache/Adapter/Filesystem.php b/src/Cache/Adapter/Filesystem.php index b07c01203f..43fb5ed631 100644 --- a/src/Cache/Adapter/Filesystem.php +++ b/src/Cache/Adapter/Filesystem.php @@ -97,6 +97,18 @@ class Filesystem implements Adapter } } + /** + * @param string $key + * @param string $hash optional + * @return bool + */ + public function touch(string $key, string $hash = ''): bool + { + $file = $this->getPath($key); + + return file_exists($file) && touch($file); + } + /** * @param string $key * @return string[] diff --git a/src/Cache/Adapter/Hazelcast.php b/src/Cache/Adapter/Hazelcast.php index 2a696c0958..c43e3957f5 100644 --- a/src/Cache/Adapter/Hazelcast.php +++ b/src/Cache/Adapter/Hazelcast.php @@ -87,6 +87,25 @@ class Hazelcast implements Adapter return ($this->execute(fn () => $this->memcached->set($key, json_encode($cache)))) ? $data : false; } + /** + * @param string $key + * @param string $hash optional + * @return bool + */ + public function touch(string $key, string $hash = ''): bool + { + $data = $this->load($key, PHP_INT_MAX, $hash); + if ($data === false) { + return false; + } + + if (! is_string($data) && ! is_array($data)) { + return false; + } + + return $this->save($key, $data, $hash) !== false; + } + /** * @param string $key * @return string[] diff --git a/src/Cache/Adapter/Memcached.php b/src/Cache/Adapter/Memcached.php index 98363c2e3f..b5d040b837 100644 --- a/src/Cache/Adapter/Memcached.php +++ b/src/Cache/Adapter/Memcached.php @@ -89,6 +89,25 @@ class Memcached implements Adapter return $this->execute(fn () => $this->memcached->set($key, $cache)) ? $data : false; } + /** + * @param string $key + * @param string $hash optional + * @return bool + */ + public function touch(string $key, string $hash = ''): bool + { + $data = $this->load($key, PHP_INT_MAX, $hash); + if ($data === false) { + return false; + } + + if (! is_string($data) && ! is_array($data)) { + return false; + } + + return $this->save($key, $data, $hash) !== false; + } + /** * @param string $key * @return string[] diff --git a/src/Cache/Adapter/Memory.php b/src/Cache/Adapter/Memory.php index 63c084281b..b4e99bb613 100644 --- a/src/Cache/Adapter/Memory.php +++ b/src/Cache/Adapter/Memory.php @@ -76,6 +76,25 @@ class Memory implements Adapter return $data; } + /** + * @param string $key + * @param string $hash optional + * @return bool + */ + public function touch(string $key, string $hash = ''): bool + { + $data = $this->load($key, PHP_INT_MAX, $hash); + if ($data === false) { + return false; + } + + if (! is_string($data) && ! is_array($data)) { + return false; + } + + return $this->save($key, $data, $hash) !== false; + } + /** * @param string $key * @return string[] diff --git a/src/Cache/Adapter/None.php b/src/Cache/Adapter/None.php index a3bb629f58..76ec9ff672 100644 --- a/src/Cache/Adapter/None.php +++ b/src/Cache/Adapter/None.php @@ -53,6 +53,16 @@ class None implements Adapter return false; } + /** + * @param string $key + * @param string $hash optional + * @return bool + */ + public function touch(string $key, string $hash = ''): bool + { + return false; + } + /** * @param string $key * @return string[] diff --git a/src/Cache/Adapter/Pool.php b/src/Cache/Adapter/Pool.php index 2fa228bf3e..851069830b 100644 --- a/src/Cache/Adapter/Pool.php +++ b/src/Cache/Adapter/Pool.php @@ -59,6 +59,16 @@ class Pool implements Adapter return $result; } + public function touch(string $key, string $hash = ''): bool + { + /** + * @var bool $result + */ + $result = $this->delegate(__FUNCTION__, \func_get_args()); + + return $result; + } + public function list(string $key): array { /** diff --git a/src/Cache/Adapter/Redis.php b/src/Cache/Adapter/Redis.php index 3c0683f8b2..5b48a5abd0 100644 --- a/src/Cache/Adapter/Redis.php +++ b/src/Cache/Adapter/Redis.php @@ -152,6 +152,25 @@ class Redis implements Adapter } } + /** + * @param string $key + * @param string $hash optional + * @return bool + */ + public function touch(string $key, string $hash = ''): bool + { + $data = $this->load($key, PHP_INT_MAX, $hash); + if ($data === false) { + return false; + } + + if (! is_string($data) && ! is_array($data)) { + return false; + } + + return $this->save($key, $data, $hash) !== false; + } + /** * @param string $key * @return string[] diff --git a/src/Cache/Adapter/RedisCluster.php b/src/Cache/Adapter/RedisCluster.php index 186fe3f7d0..0056a77051 100644 --- a/src/Cache/Adapter/RedisCluster.php +++ b/src/Cache/Adapter/RedisCluster.php @@ -103,7 +103,7 @@ class RedisCluster implements Adapter /** @var string|false */ $redis_string = $this->execute(fn () => $this->redis->hGet($key, $hash)); - if ($redis_string === false) { + if ($redis_string === false || ! is_string($redis_string)) { return false; } @@ -151,6 +151,25 @@ class RedisCluster implements Adapter } } + /** + * @param string $key + * @param string $hash optional + * @return bool + */ + public function touch(string $key, string $hash = ''): bool + { + $data = $this->load($key, PHP_INT_MAX, $hash); + if ($data === false) { + return false; + } + + if (! is_string($data) && ! is_array($data)) { + return false; + } + + return $this->save($key, $data, $hash) !== false; + } + /** * @param string $key * @return string[] diff --git a/src/Cache/Adapter/Sharding.php b/src/Cache/Adapter/Sharding.php index a036b8972f..0521edff6a 100644 --- a/src/Cache/Adapter/Sharding.php +++ b/src/Cache/Adapter/Sharding.php @@ -92,6 +92,16 @@ class Sharding implements Adapter return $this->getAdapter($key)->save($key, $data, $hash); } + /** + * @param string $key + * @param string $hash optional + * @return bool + */ + public function touch(string $key, string $hash = ''): bool + { + return $this->getAdapter($key)->touch($key, $hash); + } + /** * @param string $key * @return string[] diff --git a/src/Cache/Cache.php b/src/Cache/Cache.php index 416e1e5118..324fb71da1 100644 --- a/src/Cache/Cache.php +++ b/src/Cache/Cache.php @@ -106,6 +106,29 @@ class Cache } } + /** + * Refresh a cache entry timestamp without replacing its data. + * + * @param string $key + * @param string $hash optional + * @return bool + */ + public function touch(string $key, string $hash = ''): bool + { + $key = $this->caseSensitive ? $key : strtolower($key); + $hash = $this->caseSensitive ? $hash : strtolower($hash); + + $start = microtime(true); + $result = $this->adapter->touch($key, $hash); + $duration = microtime(true) - $start; + $this->operationDuration?->record($duration, [ + 'operation' => 'touch', + 'adapter' => $this->adapter->getName($key), + ]); + + return $result; + } + /** * Returns a list of keys. * diff --git a/tests/Cache/Base.php b/tests/Cache/Base.php index eecfdf5278..6f90730d00 100644 --- a/tests/Cache/Base.php +++ b/tests/Cache/Base.php @@ -57,6 +57,18 @@ abstract class Base extends TestCase $this->assertEquals(false, $data); } + public function testCacheTouch(): void + { + $result = self::$cache->save('touch-key', 'touch data', 'touch-key'); + $this->assertEquals('touch data', $result); + + $this->assertEquals(true, self::$cache->touch('touch-key', 'touch-key')); + $this->assertEquals('touch data', self::$cache->load('touch-key', 60, 'touch-key')); + $this->assertEquals(false, self::$cache->touch('missing-touch-key', 'missing-touch-key')); + + self::$cache->purge('touch-key'); + } + public function testCaseInsensitivity(): void { // Ensure case in-sensitivity first (configured in adapter's setUp) diff --git a/tests/Cache/NoneTest.php b/tests/Cache/NoneTest.php index bf86461969..0eb57886a8 100644 --- a/tests/Cache/NoneTest.php +++ b/tests/Cache/NoneTest.php @@ -60,6 +60,11 @@ class NoneTest extends Base $this->assertEquals(true, $result); } + public function testCacheTouch(): void + { + $this->assertEquals(false, self::$cache->touch($this->key)); + } + public function testCaseInsensitivity(): void { // None adapter does not expect case sensitivity/insensitivy