Skip to content

Commit 67375d6

Browse files
committed
Use quick-lru instead of a huge map
1 parent 2161766 commit 67375d6

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

Diff for: bun.lock

+4-1
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@
148148
"@sindresorhus/fnv1a": "^3.1.0",
149149
"jwt-decode": "^4.0.0",
150150
"next": "canary",
151+
"quick-lru": "^7.0.0",
151152
"react": "^19.0.0",
152153
"react-dom": "^19.0.0",
153154
"rison": "^0.1.1",
@@ -2533,7 +2534,7 @@
25332534

25342535
"queue-microtask": ["[email protected]", "", {}, "sha512-NuaNSa6flKT5JaSYQzJok04JzTL1CA6aGhv5rfLW3PgqA+M2ChpZQnAC8h8i4ZFkBS8X5RqkDBHA7r4hej3K9A=="],
25352536

2536-
"quick-lru": ["quick-lru@4.0.1", "", {}, "sha512-ARhCpm70fzdcvNQfPoy49IaanKkTlRWF2JMzqhcJbhSFRZv7nPTvZJdcY7301IPmvW+/p0RgIWnQDLJxifsQ7g=="],
2537+
"quick-lru": ["quick-lru@7.0.0", "", {}, "sha512-MX8gB7cVYTrYcFfAnfLlhRd0+Toyl8yX8uBx1MrX7K0jegiz9TumwOK27ldXrgDlHRdVi+MqU9Ssw6dr4BNreg=="],
25372538

25382539
"radix-vue": ["[email protected]", "", { "dependencies": { "@floating-ui/dom": "^1.6.7", "@floating-ui/vue": "^1.1.0", "@internationalized/date": "^3.5.4", "@internationalized/number": "^3.5.3", "@tanstack/vue-virtual": "^3.8.1", "@vueuse/core": "^10.11.0", "@vueuse/shared": "^10.11.0", "aria-hidden": "^1.2.4", "defu": "^6.1.4", "fast-deep-equal": "^3.1.3", "nanoid": "^5.0.7" }, "peerDependencies": { "vue": ">= 3.2.0" } }, "sha512-1xleWzWNFPfAMmb81gu/4/MV8dXMvc7j2EIjutBpBcKwxdJfeIcQg4k9De18L2rL1/GZg5wA9KykeKTM4MjWow=="],
25392540

@@ -4009,6 +4010,8 @@
40094010

40104011
"cacheable-request/lowercase-keys": ["[email protected]", "", {}, "sha512-tqNXrS78oMOE73NMxK4EMLQsQowWf8jKooH9g7xPavRT706R6bkQJ6DY2Te7QukaZsulxa30wQ7bk0pm4XiHmA=="],
40114012

4013+
"camelcase-keys/quick-lru": ["[email protected]", "", {}, "sha512-ARhCpm70fzdcvNQfPoy49IaanKkTlRWF2JMzqhcJbhSFRZv7nPTvZJdcY7301IPmvW+/p0RgIWnQDLJxifsQ7g=="],
4014+
40124015
"capnp-ts/debug": ["[email protected]", "", { "dependencies": { "ms": "^2.1.3" } }, "sha512-Er2nc/H7RrMXZBFCEim6TCmMk02Z8vLC2Rbi1KEBggpo0fS6l0S1nnapwmIi3yW/+GOJap1Krg4w0Hg80oCqgQ=="],
40134016

40144017
"codemirror/@codemirror/autocomplete": ["@codemirror/[email protected]", "", { "dependencies": { "@codemirror/language": "^6.0.0", "@codemirror/state": "^6.0.0", "@codemirror/view": "^6.17.0", "@lezer/common": "^1.0.0" } }, "sha512-iWHdj/B1ethnHRTwZj+C1obmmuCzquH29EbcKr0qIjA9NfDeBDJ7vs+WOHsFeLeflE4o+dHfYndJloMKHUkWUA=="],

Diff for: packages/gitbook-v2/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"server-only": "^0.0.1",
1313
"warn-once": "^0.1.1",
1414
"rison": "^0.1.1",
15-
"jwt-decode": "^4.0.0"
15+
"jwt-decode": "^4.0.0",
16+
"quick-lru": "^7.0.0"
1617
},
1718
"devDependencies": {
1819
"gitbook": "*",

Diff for: packages/gitbook-v2/src/lib/data/memoize.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import QuickLRU from 'quick-lru';
2+
13
/**
24
* Wrap cache calls to avoid duplicated executions of the same function during concurrent calls.
35
* The implementation is based on `p-memoize` but is adapted to work per-request in Cloudflare Workers.
@@ -6,7 +8,7 @@ export function memoize<ArgsType extends any[], ReturnType>(
68
getGlobalContext: () => object | null | undefined,
79
wrapped: (cacheKey: string, ...args: ArgsType) => Promise<ReturnType>
810
): (...args: ArgsType) => Promise<ReturnType> {
9-
const globalCache = new WeakMap<object, Map<string, ReturnType>>();
11+
const globalCache = new WeakMap<object, QuickLRU<string, ReturnType>>();
1012
const globalPromiseCache = new WeakMap<object, Map<string, Promise<ReturnType>>>();
1113

1214
return (...args: ArgsType) => {
@@ -16,7 +18,11 @@ export function memoize<ArgsType extends any[], ReturnType>(
1618
* Cache storage that is scoped to the current request when executed in Cloudflare Workers,
1719
* to avoid "Cannot perform I/O on behalf of a different request" errors.
1820
*/
19-
const cache = globalCache.get(globalContext) ?? new Map<string, ReturnType>();
21+
const cache =
22+
globalCache.get(globalContext) ??
23+
new QuickLRU<string, ReturnType>({
24+
maxSize: 1000,
25+
});
2026
globalCache.set(globalContext, cache);
2127

2228
const promiseCache =

0 commit comments

Comments
 (0)