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 @@ -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.
You can’t perform that action at this time.
0 commit comments