Skip to content

Commit c5c35ec

Browse files
committed
Add missing has() method to Cache component
1 parent c84fc72 commit c5c35ec

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

src/imperazim/components/cache/Cache.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,27 @@ public static function get(string $key): mixed {
6262
return $cacheItem['value'];
6363
}
6464

65+
/**
66+
* Checks if a cache key exists and is not expired.
67+
* @param string $key The cache key.
68+
* @return bool True if the key exists and is not expired, false otherwise.
69+
*/
70+
public static function has(string $key): bool {
71+
if (!isset(self::$cache[$key])) {
72+
return false;
73+
}
74+
75+
$cacheItem = self::$cache[$key];
76+
77+
// Check if the item has expired
78+
if ($cacheItem['expires_at'] !== null && $cacheItem['expires_at'] < time()) {
79+
self::invalidate($key);
80+
return false;
81+
}
82+
83+
return true;
84+
}
85+
6586
/**
6687
* Invalidates a cached item, removing it from the cache.
6788
* @param string $key The cache key.

0 commit comments

Comments
 (0)