Skip to content

Commit 854f079

Browse files
committed
Add back memoize
1 parent 196aa30 commit 854f079

File tree

1 file changed

+16
-83
lines changed

1 file changed

+16
-83
lines changed

packages/gitbook-v2/src/lib/data/memoize.ts

+16-83
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { getCloudflareContext } from '@opennextjs/cloudflare';
22

3-
import pMemoize, { type CacheStorage } from 'p-memoize';
4-
// import { getCloudflareContext } from './cloudflare';
3+
import pMemoize from 'p-memoize';
54

6-
// const requestWeakCache = new WeakMap<object, WeakMap<any, any>>();
5+
const requestWeakCache = new WeakMap<object, WeakMap<any, any>>();
76

87
/**
98
* We wrap 'use cache' calls in a p-memoize function to avoid
@@ -12,94 +11,28 @@ import pMemoize, { type CacheStorage } from 'p-memoize';
1211
* Hopefully one day this can be done directly by 'use cache'.
1312
*/
1413
export function memoize<F extends (...args: any[]) => any>(f: F): F {
15-
// const globalContext = getCloudflareContext()?.cf ?? globalThis;
16-
17-
// const requestCache = requestWeakCache.get(globalContext);
18-
// if (requestCache) {
19-
// const cache = requestCache.get(f);
20-
// if (cache) {
21-
// return cache as F;
22-
// }
23-
// }
24-
25-
if (process.env.NODE_ENV === 'production') {
26-
return f;
14+
const globalContext = getCloudflareContext()?.cf ?? globalThis;
15+
16+
/**
17+
* Cache storage that is scoped to the current request when executed in Cloudflare Workers,
18+
* to avoid "Cannot perform I/O on behalf of a different request" errors.
19+
*/
20+
const requestCache = requestWeakCache.get(globalContext) ?? new WeakMap<any, any>();
21+
const cached = requestCache.get(f);
22+
if (cached) {
23+
return cached as F;
2724
}
2825

29-
const getFunctionCache = async () => {
30-
const functionsCache = await getRequestCacheWeakMap();
31-
const cache = functionsCache.get(f);
32-
if (cache) {
33-
return cache;
34-
}
35-
36-
const newCache = new Map<string, unknown>();
37-
functionsCache.set(f, newCache);
38-
return newCache;
39-
};
40-
41-
return pMemoize(f, {
26+
const memoized = pMemoize(f, {
4227
cacheKey: (args) => {
4328
return JSON.stringify(deepSortValue(args));
4429
},
45-
/**
46-
* Cache storage that is scoped to the current request when executed in Cloudflare Workers,
47-
* to avoid "Cannot perform I/O on behalf of a different request" errors.
48-
*/
49-
cache: {
50-
has: async (key) => {
51-
const cache = await getFunctionCache();
52-
return cache.has(key);
53-
},
54-
get: async (key) => {
55-
const cache = await getFunctionCache();
56-
return cache.get(key) as Awaited<ReturnType<F>> | undefined;
57-
},
58-
set: async (key, value) => {
59-
const cache = await getFunctionCache();
60-
cache.set(key, value);
61-
},
62-
delete: async (key) => {
63-
const cache = await getFunctionCache();
64-
cache.delete(key);
65-
},
66-
clear: async () => {
67-
const cache = await getFunctionCache();
68-
cache.clear?.();
69-
},
70-
},
7130
});
72-
}
7331

74-
const globalCache = new WeakMap<any, CacheStorage<string, unknown>>();
75-
const perRequestCache = new WeakMap<object, WeakMap<any, CacheStorage<string, unknown>>>();
76-
77-
/**
78-
* Get a global weakmap that is scoped to the current request when executed in Cloudflare Workers,
79-
* to avoid "Cannot perform I/O on behalf of a different request" errors.
80-
* And global when executed in Node.js.
81-
*/
82-
async function getRequestCacheWeakMap(): Promise<WeakMap<any, CacheStorage<string, unknown>>> {
83-
try {
84-
const cloudflareContext = getCloudflareContext();
85-
if (cloudflareContext?.cf) {
86-
// `cf` changes for each request, we can use it as an identifier of the request to isolate the cache per request
87-
const requestCache = perRequestCache.get(cloudflareContext.cf);
88-
if (requestCache) {
89-
return requestCache;
90-
}
91-
92-
const newRequestCache = new WeakMap<any, CacheStorage<string, unknown>>();
93-
perRequestCache.set(cloudflareContext.cf, newRequestCache);
94-
return newRequestCache;
95-
}
96-
} catch (error) {
97-
if (process.env.NODE_ENV === 'production' && !process.env.VERCEL) {
98-
throw error;
99-
}
100-
}
32+
requestCache.set(f, memoized);
33+
requestWeakCache.set(globalContext, requestCache);
10134

102-
return globalCache;
35+
return memoized;
10336
}
10437

10538
export function getCacheKey(args: any[]) {

0 commit comments

Comments
 (0)