import type { CacheEntry, CacheOptions, ICache } from "./types.js"; const DEFAULT_TTL_MS = 10 * 60 * 1000; // 10 分钟 const DEFAULT_MAX_ENTRIES = 2000; export class MemoryCache implements ICache { // Map 保持键的插入顺序,结合重新插入机制实现高效的 O(1) LRU 淘汰 private map = new Map>(); private maxEntries: number; constructor(maxEntries = DEFAULT_MAX_ENTRIES) { this.maxEntries = maxEntries; } /** * CRIT-02: 构建命名空间隔离的复合内存键,彻底规避跨域碰撞 */ private toCompositeKey(key: string, namespace?: string): string { const ns = namespace || "default"; return `${ns}:${key}`; } public get(key: string, namespace = "default"): T | undefined { const compositeKey = this.toCompositeKey(key, namespace); const entry = this.map.get(compositeKey); if (!entry) return undefined; const now = Date.now(); if (entry.expiresAt <= now) { this.map.delete(compositeKey); return undefined; } // MAJ-09: 真正 LRU 访问刷新 —— 命中后移至 Map 末尾 this.map.delete(compositeKey); this.map.set(compositeKey, entry); return entry.value as T; } public getEntry(key: string, namespace = "default"): CacheEntry | undefined { const compositeKey = this.toCompositeKey(key, namespace); const entry = this.map.get(compositeKey); if (!entry) return undefined; const now = Date.now(); if (entry.expiresAt <= now) { this.map.delete(compositeKey); return undefined; } // 命中后移至 Map 末尾保持热度 this.map.delete(compositeKey); this.map.set(compositeKey, entry); return entry as CacheEntry; } public set(key: string, value: T, options?: CacheOptions): void { const namespace = options?.namespace ?? "default"; const compositeKey = this.toCompositeKey(key, namespace); const now = Date.now(); const ttl = options?.ttlMs ?? DEFAULT_TTL_MS; const entry: CacheEntry = { value, expiresAt: now + ttl, updatedAt: now, namespace, }; // MAJ-08: 更新已有键时,先删后插调至末尾,不应误触发 LRU 淘汰 if (this.map.has(compositeKey)) { this.map.delete(compositeKey); this.map.set(compositeKey, entry); return; } // 若达到容量限制,执行 LRU 淘汰 if (this.map.size >= this.maxEntries) { this.evict(); } this.map.set(compositeKey, entry); } public setEntry(key: string, entry: CacheEntry): void { const compositeKey = this.toCompositeKey(key, entry.namespace); if (this.map.has(compositeKey)) { this.map.delete(compositeKey); } else if (this.map.size >= this.maxEntries) { this.evict(); } this.map.set(compositeKey, entry); } public delete(key: string, namespace = "default"): void { const compositeKey = this.toCompositeKey(key, namespace); this.map.delete(compositeKey); } public clear(namespace?: string): void { if (!namespace) { this.map.clear(); return; } const prefix = `${namespace}:`; for (const k of this.map.keys()) { if (k.startsWith(prefix)) { this.map.delete(k); } } } public has(key: string, namespace = "default"): boolean { return this.get(key, namespace) !== undefined; } public size(): number { return this.map.size; } private evict(): void { const now = Date.now(); // 1. 优先剔除已过期的死键 for (const [k, v] of this.map.entries()) { if (v.expiresAt <= now) { this.map.delete(k); } } // 2. 若仍然超限,按真正的 LRU(头部为最久未访问的条目)淘汰最早的条目 while (this.map.size >= this.maxEntries) { const oldestKey = this.map.keys().next().value; if (oldestKey !== undefined) { this.map.delete(oldestKey); } else { break; } } } }