Skip to content

Commit 3ca1460

Browse files
juliusmarmingecodex
andcommitted
Preserve LRU entries on oversized replacement
Co-authored-by: codex <[email protected]>
1 parent 2ebd3f6 commit 3ca1460

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

‎apps/web/src/lib/lruCache.test.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,12 @@ describe("LRUCache", () => {
4949
expect(cache.get("a")).toBe("A");
5050
expect(cache.get("oversized")).toBeNull();
5151
});
52+
53+
it("preserves an existing entry when an oversized replacement is rejected", () => {
54+
const cache = new LRUCache<string>(2, 25);
55+
cache.set("a", "A", 10);
56+
cache.set("a", "oversized", 30);
57+
58+
expect(cache.get("a")).toBe("A");
59+
});
5260
});

‎apps/web/src/lib/lruCache.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ export class LRUCache<T> {
2323
}
2424

2525
set(key: string, value: T, approximateSize: number): void {
26+
if (approximateSize > this.maxMemoryBytes) {
27+
return;
28+
}
29+
2630
const existing = this.cache.get(key);
2731
if (existing) {
2832
this.totalSize -= existing.approximateSize;
2933
this.cache.delete(key);
3034
}
3135

32-
if (approximateSize > this.maxMemoryBytes) {
33-
return;
34-
}
35-
3636
this.evictIfNeeded(approximateSize);
3737
this.cache.set(key, { value, approximateSize });
3838
this.totalSize += approximateSize;

0 commit comments

Comments
 (0)