Skip to content

Commit 42ae07f

Browse files
committed
Basic naive caching mainly for demo purposes
1 parent 6af55cb commit 42ae07f

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

packages/next/src/server/use-cache/use-cache-wrapper.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,35 @@ interface CacheHandler {
2727
}
2828

2929
const cacheHandlerMap: Map<string, CacheHandler> = new Map()
30+
31+
// TODO: Move default implementation to be injectable.
32+
const defaultCacheStorage: Map<string, ReadableStream> = new Map()
3033
cacheHandlerMap.set('default', {
31-
async get(_cacheKey: string | ArrayBuffer) {
32-
// TODO: Implement caching.
34+
async get(cacheKey: string | ArrayBuffer) {
35+
// TODO: Implement proper caching.
36+
if (typeof cacheKey === 'string') {
37+
const value = defaultCacheStorage.get(cacheKey)
38+
if (value !== undefined) {
39+
const [returnStream, newSaved] = value.tee()
40+
defaultCacheStorage.set(cacheKey, newSaved)
41+
return {
42+
value: returnStream,
43+
stale: false,
44+
}
45+
}
46+
} else {
47+
// TODO: Handle binary keys.
48+
}
3349
return undefined
3450
},
35-
async set(_cacheKey: string | ArrayBuffer, value: ReadableStream) {
36-
// TODO: Implement caching.
37-
await value.cancel()
51+
async set(cacheKey: string | ArrayBuffer, value: ReadableStream) {
52+
// TODO: Implement proper caching.
53+
if (typeof cacheKey === 'string') {
54+
defaultCacheStorage.set(cacheKey, value)
55+
} else {
56+
// TODO: Handle binary keys.
57+
await value.cancel()
58+
}
3859
},
3960
// In-memory caches are fragile and should not use stale-while-revalidate
4061
// semantics on the caches because it's not worth warming up an entry that's

0 commit comments

Comments
 (0)