Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/empty-cache-keys-evict.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

Fixed LRU cache eviction when an empty string is the oldest key, keeping SDK validation caches within their configured size.
28 changes: 28 additions & 0 deletions packages/thirdweb/src/utils/caching/lru.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,31 @@ test("update touched keys", () => {
expect(cache.has("c")).toBe(false);
expect(cache.has("d")).toBe(true);
});

test("evicts an empty-string oldest key", () => {
const cache = new LruMap<number>(2);
cache.set("", 0);
cache.set("a", 1);
cache.set("b", 2);

expect(cache.size).toBe(2);
expect([...cache.keys()]).toEqual(["a", "b"]);

cache.set("c", 3);
expect(cache.size).toBe(2);
expect([...cache.keys()]).toEqual(["b", "c"]);
});

test("refreshes an empty-string key before eventually evicting it", () => {
const cache = new LruMap<number>(2);
cache.set("", 0);
cache.set("a", 1);
expect(cache.get("")).toBe(0);

cache.set("b", 2);
expect([...cache.keys()]).toEqual(["", "b"]);

cache.set("c", 3);
expect(cache.size).toBe(2);
expect([...cache.keys()]).toEqual(["b", "c"]);
});
6 changes: 3 additions & 3 deletions packages/thirdweb/src/utils/caching/lru.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ export class LruMap<value = unknown> extends Map<string, value> {
override set(key: string, value: value) {
super.set(key, value);
if (this.maxSize && this.size > this.maxSize) {
const firstKey = this.keys().next().value;
if (firstKey) {
this.delete(firstKey);
const firstKey = this.keys().next();
if (!firstKey.done) {
this.delete(firstKey.value);
}
}
return this;
Expand Down
Loading