Skip to content

Commit 2c601d2

Browse files
committed
Add missing putMany(), getMany(), and purgeExpired() methods to Cache component
1 parent 620f822 commit 2c601d2

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

src/imperazim/components/cache/Cache.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,42 @@ public static function clear(): void {
116116
self::$cache = [];
117117
}
118118

119+
/**
120+
* Stores multiple values in the cache with optional TTL.
121+
* @param array $items Associative array of key => value pairs.
122+
* @param int|null $ttl Time to live in seconds. Null for no expiration.
123+
*/
124+
public static function putMany(array $items, ?int $ttl = null): void {
125+
foreach ($items as $key => $value) {
126+
self::put($key, $value, $ttl);
127+
}
128+
}
129+
130+
/**
131+
* Retrieves multiple cached values by keys.
132+
* @param array $keys Array of cache keys.
133+
* @return array Associative array of key => value pairs (null for missing/expired).
134+
*/
135+
public static function getMany(array $keys): array {
136+
$results = [];
137+
foreach ($keys as $key) {
138+
$results[$key] = self::get($key);
139+
}
140+
return $results;
141+
}
142+
143+
/**
144+
* Removes expired cache entries.
145+
*/
146+
public static function purgeExpired(): void {
147+
$currentTime = time();
148+
foreach (self::$cache as $key => $item) {
149+
if ($item['expires_at'] !== null && $item['expires_at'] < $currentTime) {
150+
unset(self::$cache[$key]);
151+
}
152+
}
153+
}
154+
119155
/**
120156
* Returns cache statistics such as total entries, hits, misses, and expired items.
121157
* @return array Cache statistics.

0 commit comments

Comments
 (0)