Skip to content

Commit 1d5bfcd

Browse files
committed
Add comprehensive Cache method aliases and utilities
- Add forget() alias for invalidate() - Add flush() alias for clear() - Add stats() alias for getStats() - Add set() alias for put() - Add delete() and remove() aliases for invalidate() - Add exists() alias for has() - Add size() method to get cache entry count - Add isExpired() to check if specific key expired - Add getRemainingTtl() to get remaining TTL for key - Add all() method to get all non-expired entries
1 parent 2c601d2 commit 1d5bfcd

1 file changed

Lines changed: 110 additions & 0 deletions

File tree

src/imperazim/components/cache/Cache.php

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,116 @@ public static function purgeExpired(): void {
152152
}
153153
}
154154

155+
/**
156+
* Alias for invalidate() - removes a cached item.
157+
* @param string $key The cache key.
158+
*/
159+
public static function forget(string $key): void {
160+
self::invalidate($key);
161+
}
162+
163+
/**
164+
* Alias for clear() - clears all cached data.
165+
*/
166+
public static function flush(): void {
167+
self::clear();
168+
}
169+
170+
/**
171+
* Alias for getStats() - returns cache statistics.
172+
* @return array Cache statistics.
173+
*/
174+
public static function stats(): array {
175+
return self::getStats();
176+
}
177+
178+
/**
179+
* Alias for put() - stores a value in the cache.
180+
* @param string $key The cache key.
181+
* @param mixed $value The value to store.
182+
* @param int|null $ttl Time to live in seconds (null for no expiration).
183+
*/
184+
public static function set(string $key, mixed $value, ?int $ttl = null): void {
185+
self::put($key, $value, $ttl);
186+
}
187+
188+
/**
189+
* Alias for invalidate() - removes a cached item.
190+
* @param string $key The cache key.
191+
*/
192+
public static function delete(string $key): void {
193+
self::invalidate($key);
194+
}
195+
196+
/**
197+
* Alias for invalidate() - removes a cached item.
198+
* @param string $key The cache key.
199+
*/
200+
public static function remove(string $key): void {
201+
self::invalidate($key);
202+
}
203+
204+
/**
205+
* Alias for has() - checks if a key exists in the cache.
206+
* @param string $key The cache key.
207+
* @return bool True if the key exists and is not expired.
208+
*/
209+
public static function exists(string $key): bool {
210+
return self::has($key);
211+
}
212+
213+
/**
214+
* Returns the number of entries in the cache.
215+
* @return int Number of cached entries.
216+
*/
217+
public static function size(): int {
218+
return count(self::$cache);
219+
}
220+
221+
/**
222+
* Checks if a specific cache key has expired.
223+
* @param string $key The cache key.
224+
* @return bool True if the key exists but has expired.
225+
*/
226+
public static function isExpired(string $key): bool {
227+
if (!isset(self::$cache[$key])) {
228+
return false; // Key doesn't exist, so it's not "expired" - it never existed
229+
}
230+
$item = self::$cache[$key];
231+
return $item['expires_at'] !== null && $item['expires_at'] < time();
232+
}
233+
234+
/**
235+
* Gets the remaining TTL (time to live) for a cache key in seconds.
236+
* @param string $key The cache key.
237+
* @return int|null Remaining TTL in seconds, or null if key doesn't exist or has no expiration.
238+
*/
239+
public static function getRemainingTtl(string $key): ?int {
240+
if (!isset(self::$cache[$key])) {
241+
return null;
242+
}
243+
$item = self::$cache[$key];
244+
if ($item['expires_at'] === null) {
245+
return null; // No expiration
246+
}
247+
$remaining = $item['expires_at'] - time();
248+
return max(0, $remaining); // Don't return negative values
249+
}
250+
251+
/**
252+
* Returns all cache entries as an associative array.
253+
* @return array All cache entries [key => value].
254+
*/
255+
public static function all(): array {
256+
$result = [];
257+
foreach (self::$cache as $key => $item) {
258+
if ($item['expires_at'] === null || $item['expires_at'] > time()) {
259+
$result[$key] = $item['value'];
260+
}
261+
}
262+
return $result;
263+
}
264+
155265
/**
156266
* Returns cache statistics such as total entries, hits, misses, and expired items.
157267
* @return array Cache statistics.

0 commit comments

Comments
 (0)