diff --git a/system/Cache/CacheInterface.php b/system/Cache/CacheInterface.php index 9ae83f29283d..4cedb67e9538 100644 --- a/system/Cache/CacheInterface.php +++ b/system/Cache/CacheInterface.php @@ -13,6 +13,8 @@ namespace CodeIgniter\Cache; +use Closure; + interface CacheInterface { /** @@ -38,6 +40,16 @@ public function get(string $key): mixed; */ public function save(string $key, mixed $value, int $ttl = 60): bool; + /** + * Attempts to get an item from the cache, or executes the callback + * and stores the result on cache miss. + * + * @param string $key Cache item name + * @param int $ttl Time To Live, in seconds + * @param Closure(): mixed $callback Callback executed on cache miss + */ + public function remember(string $key, int $ttl, Closure $callback): mixed; + /** * Deletes a specific item from the cache store. * diff --git a/system/Cache/Handlers/BaseHandler.php b/system/Cache/Handlers/BaseHandler.php index 2d9aeff489d1..4f7022d3c249 100644 --- a/system/Cache/Handlers/BaseHandler.php +++ b/system/Cache/Handlers/BaseHandler.php @@ -75,13 +75,6 @@ public static function validateKey($key, $prefix = ''): string return strlen($prefix . $key) > static::MAX_KEY_LENGTH ? $prefix . md5($key) : $prefix . $key; } - /** - * Get an item from the cache, or execute the given Closure and store the result. - * - * @param string $key Cache item name - * @param int $ttl Time to live - * @param Closure(): mixed $callback Callback return value - */ public function remember(string $key, int $ttl, Closure $callback): mixed { $value = $this->get($key); diff --git a/user_guide_src/source/changelogs/v4.7.0.rst b/user_guide_src/source/changelogs/v4.7.0.rst index e8124847efc2..2fb147b2d1b8 100644 --- a/user_guide_src/source/changelogs/v4.7.0.rst +++ b/user_guide_src/source/changelogs/v4.7.0.rst @@ -180,6 +180,7 @@ Interface Changes **NOTE:** If you've implemented your own classes that implement these interfaces from scratch, you will need to update your implementations to include the new methods to ensure compatibility. - **Cache:** The ``CacheInterface`` now includes the ``deleteMatching()`` method. +- **Cache:** The ``CacheInterface`` now includes the ``remember()`` method. All built-in cache handlers inherit this method via ``BaseHandler``, so no changes are required for them. - **Database:** The ``QueryInterface`` now includes the ``getOriginalQuery()`` method. - **Images:** The ``ImageHandlerInterface`` now includes a new method: ``clearMetadata()``.