diff --git a/packages/core/src/cache/lru-ttl-cache.ts b/packages/core/src/cache/lru-ttl-cache.ts index fd5037f6..be51f124 100644 --- a/packages/core/src/cache/lru-ttl-cache.ts +++ b/packages/core/src/cache/lru-ttl-cache.ts @@ -1,5 +1,5 @@ -export interface CacheEntry { - value: V +export interface CacheEntry { + value: TValue expiresAt: number } @@ -9,10 +9,10 @@ export interface CacheEntry { * It uses a JavaScript Map to maintain insertion order for LRU eviction. * On every 'get' call, it samples a fixed number of items to check for expiration. */ -export class LruTtlCache { - private cache = new Map>() +export class LruTtlCache { + private cache = new Map>() private maxCapacity: number - private iterator: IterableIterator | null = null + private iterator: IterableIterator | null = null private sampleSize: number constructor(capacity: number = 50000, sampleSize: number = 50) { @@ -24,7 +24,7 @@ export class LruTtlCache { * Gets a value from the cache. * Performs proactive eviction of expired items and refreshes the LRU position of the accessed item. */ - get(key: K): V | undefined { + get(key: TKey): TValue | undefined { this.evictSample() const entry = this.cache.get(key) @@ -46,7 +46,7 @@ export class LruTtlCache { * Sets a value in the cache with a specified TTL. * If the cache exceeds capacity, the least recently used item is evicted. */ - set(key: K, value: V, ttlMs: number): void { + set(key: TKey, value: TValue, ttlMs: number): void { if (this.maxCapacity <= 0 || ttlMs <= 0 || isNaN(ttlMs)) { this.cache.delete(key) return @@ -64,7 +64,7 @@ export class LruTtlCache { this.cache.set(key, { value, expiresAt: this.now() + ttlMs }) } - delete(key: K): boolean { + delete(key: TKey): boolean { return this.cache.delete(key) } @@ -81,16 +81,17 @@ export class LruTtlCache { const now = this.now() for (let i = 0; i < this.sampleSize; i++) { - let result = this.iterator.next() + const result = this.iterator.next() if (result.done) { + // Reset iterator for next call and break early to avoid redundant loops + // if cache size < sampleSize. this.iterator = this.cache.keys() - result = this.iterator.next() - if (result.done) break + break } const key = result.value const entry = this.cache.get(key) - + // If entry was manually deleted, entry will be undefined. // If it's expired, we delete it. if (entry && now > entry.expiresAt) {