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/user_guide_src/source/changelogs/v4.7.0.rst b/user_guide_src/source/changelogs/v4.7.0.rst index 2355e9a9f420..0b647c693a62 100644 --- a/user_guide_src/source/changelogs/v4.7.0.rst +++ b/user_guide_src/source/changelogs/v4.7.0.rst @@ -116,6 +116,7 @@ Interface Changes - **Cache:** The ``CacheInterface`` now includes the ``deleteMatching()`` method. If you've implemented your own caching driver from scratch, you will need to provide an implementation for this method to ensure compatibility. - **Images:** The ``ImageHandlerInterface`` now includes a new method: ``clearMetadata()``. If you've implemented your own handler from scratch, you will need to provide an implementation for this method to ensure compatibility. +- **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. If you have implemented your own caching driver directly from ``CacheInterface``, you will need to provide an implementation for ``remember()`` to maintain compatibility. Method Signature Changes ========================