11import { CacheBackend , CacheEntry } from './types' ;
22import { NON_IMMUTABLE_LOCAL_CACHE_MAX_AGE_SECONDS , isCacheEntryImmutable } from './utils' ;
3+ import { getGlobalContext } from '../waitUntil' ;
34
45export const memoryCache : CacheBackend = {
56 name : 'memory' ,
67 replication : 'local' ,
78 async get ( key ) {
8- const memoryCache = getMemoryCache ( ) ;
9+ const memoryCache = await getMemoryCache ( ) ;
910 const memoryEntry = memoryCache . get ( key ) ;
1011
1112 if ( ! memoryEntry ) {
@@ -21,7 +22,7 @@ export const memoryCache: CacheBackend = {
2122 return null ;
2223 } ,
2324 async set ( key , entry ) {
24- const memoryCache = getMemoryCache ( ) ;
25+ const memoryCache = await getMemoryCache ( ) ;
2526 // When the entry is immutable, we can cache it for the entire duration.
2627 // Else we cache it for a very short time.
2728 const expiresAt =
@@ -41,11 +42,11 @@ export const memoryCache: CacheBackend = {
4142 memoryCache . set ( key , { ...entry , meta } ) ;
4243 } ,
4344 async del ( keys ) {
44- const memoryCache = getMemoryCache ( ) ;
45+ const memoryCache = await getMemoryCache ( ) ;
4546 keys . forEach ( ( key ) => memoryCache . delete ( key ) ) ;
4647 } ,
4748 async revalidateTags ( tags ) {
48- const memoryCache = getMemoryCache ( ) ;
49+ const memoryCache = await getMemoryCache ( ) ;
4950 const keys : string [ ] = [ ] ;
5051
5152 memoryCache . forEach ( ( entry , key ) => {
@@ -66,13 +67,12 @@ export const memoryCache: CacheBackend = {
6667 * With next-on-pages, the code seems to be isolated between the middleware and the handler.
6768 * To share the cache between the two, we use a global variable.
6869 */
69- function getMemoryCache ( ) : Map < string , CacheEntry > {
70- // @ts -ignore
71- if ( ! globalThis . gitbookMemoryCache ) {
72- // @ts -ignore
73- globalThis . gitbookMemoryCache = new Map ( ) ;
70+ async function getMemoryCache ( ) : Promise < Map < string , CacheEntry > > {
71+ let globalThisForMemoryCache : any = await getGlobalContext ( ) ;
72+
73+ if ( ! globalThisForMemoryCache . gitbookMemoryCache ) {
74+ globalThisForMemoryCache . gitbookMemoryCache = new Map ( ) ;
7475 }
7576
76- // @ts -ignore
77- return globalThis . gitbookMemoryCache ;
77+ return globalThisForMemoryCache . gitbookMemoryCache ;
7878}
0 commit comments