Skip to content

Commit 620f822

Browse files
committed
Add missing remember() method to Cache component
1 parent c5c35ec commit 620f822

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

src/imperazim/components/cache/Cache.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,24 @@ public static function has(string $key): bool {
8383
return true;
8484
}
8585

86+
/**
87+
* Gets a cached value or executes a callback and caches the result.
88+
* @param string $key The cache key.
89+
* @param callable $callback The callback to execute if cache miss.
90+
* @param int|null $ttl Time to live in seconds. Null for no expiration.
91+
* @return mixed The cached value or callback result.
92+
*/
93+
public static function remember(string $key, callable $callback, ?int $ttl = null): mixed {
94+
$value = self::get($key);
95+
if ($value !== null) {
96+
return $value;
97+
}
98+
99+
$value = $callback();
100+
self::put($key, $value, $ttl);
101+
return $value;
102+
}
103+
86104
/**
87105
* Invalidates a cached item, removing it from the cache.
88106
* @param string $key The cache key.

0 commit comments

Comments
 (0)