Skip to content

Commit 21fbcf6

Browse files
feat: add remember() to CacheInterface (#9875)
Co-authored-by: John Paul E. Balandan, CPA <paulbalandan@gmail.com>
1 parent 4685507 commit 21fbcf6

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

system/Cache/CacheInterface.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
namespace CodeIgniter\Cache;
1515

16+
use Closure;
17+
1618
interface CacheInterface
1719
{
1820
/**
@@ -38,6 +40,16 @@ public function get(string $key): mixed;
3840
*/
3941
public function save(string $key, mixed $value, int $ttl = 60): bool;
4042

43+
/**
44+
* Attempts to get an item from the cache, or executes the callback
45+
* and stores the result on cache miss.
46+
*
47+
* @param string $key Cache item name
48+
* @param int $ttl Time To Live, in seconds
49+
* @param Closure(): mixed $callback Callback executed on cache miss
50+
*/
51+
public function remember(string $key, int $ttl, Closure $callback): mixed;
52+
4153
/**
4254
* Deletes a specific item from the cache store.
4355
*

system/Cache/Handlers/BaseHandler.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,6 @@ public static function validateKey($key, $prefix = ''): string
7575
return strlen($prefix . $key) > static::MAX_KEY_LENGTH ? $prefix . md5($key) : $prefix . $key;
7676
}
7777

78-
/**
79-
* Get an item from the cache, or execute the given Closure and store the result.
80-
*
81-
* @param string $key Cache item name
82-
* @param int $ttl Time to live
83-
* @param Closure(): mixed $callback Callback return value
84-
*/
8578
public function remember(string $key, int $ttl, Closure $callback): mixed
8679
{
8780
$value = $this->get($key);

user_guide_src/source/changelogs/v4.7.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ Interface Changes
180180
**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.
181181

182182
- **Cache:** The ``CacheInterface`` now includes the ``deleteMatching()`` method.
183+
- **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.
183184
- **Database:** The ``QueryInterface`` now includes the ``getOriginalQuery()`` method.
184185
- **Images:** The ``ImageHandlerInterface`` now includes a new method: ``clearMetadata()``.
185186

0 commit comments

Comments
 (0)