perf: implement proper LRU Cache and ETag optimizations

This commit is contained in:
ralksta
2026-03-27 11:11:23 +01:00
parent af7e2e9206
commit bf72a88d6c
9 changed files with 182 additions and 14 deletions

View File

@@ -21,12 +21,19 @@ class MemoryCache {
return null;
}
// LRU: Move accessed item to the back of the Map (newest)
this.store.delete(key);
this.store.set(key, entry);
return entry.data as T;
}
set<T>(key: string, data: T, ttlMs: number): void {
// Evict oldest entries if at capacity
if (this.store.size >= this.maxEntries) {
// If key exists, delete it first to ensure we push it to the back
if (this.store.has(key)) {
this.store.delete(key);
} else if (this.store.size >= this.maxEntries) {
// Evict oldest entry (first item) if at capacity
const firstKey = this.store.keys().next().value;
if (firstKey) this.store.delete(firstKey);
}