File tree Expand file tree Collapse file tree
src/imperazim/components/cache Expand file tree Collapse file tree Original file line number Diff line number Diff 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.
You can’t perform that action at this time.
0 commit comments