import { ICache } from "../cache"; interface CacheEntry { value: V; expiry: number | null; } /** * A simple LRU cache implementation. */ export class LRUCache implements ICache { private cache = new Map>(); constructor(private maxSize: number = 100) {} get(key: K): V | undefined { const entry = this.cache.get(key); if (!entry) return undefined; if (entry.expiry !== null && Date.now() > entry.expiry) { this.cache.delete(key); return undefined; } // Update the key to mark it as recently used this.cache.delete(key); this.cache.set(key, entry); return entry.value; } set(key: K, value: V, ttl?: number): void { if (this.cache.has(key)) { this.cache.delete(key); } else if (this.cache.size >= this.maxSize) { // Remove the least recently used item (first inserted) const firstKey = this.cache.keys().next().value; if (firstKey !== undefined) { this.cache.delete(firstKey); } } const expiry = ttl ? Date.now() + ttl : null; this.cache.set(key, { value, expiry }); } delete(key: K): void { this.cache.delete(key); } clear(): void { this.cache.clear(); } }