mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-20 09:33:21 +00:00
5f7b29bbc8
* Use input url instead of request url * Improve cache purging * Lint * Fix TS * Add method to delete from cloudflare * Clear on all backends * Fix * Add global cloudflare tag
60 lines
1.5 KiB
TypeScript
60 lines
1.5 KiB
TypeScript
import { CacheBackend, CacheEntry } from './types';
|
|
|
|
export const memoryCache: CacheBackend = {
|
|
name: 'memory',
|
|
fallback: true,
|
|
async get(key) {
|
|
const memoryCache = getMemoryCache();
|
|
const memoryEntry = memoryCache.get(key);
|
|
if (!memoryEntry) {
|
|
return null;
|
|
}
|
|
|
|
if (memoryEntry.meta.expiresAt > Date.now()) {
|
|
memoryEntry.meta.hits + 1;
|
|
return memoryEntry;
|
|
} else {
|
|
memoryCache.delete(key);
|
|
}
|
|
|
|
return null;
|
|
},
|
|
async set(key, entry) {
|
|
const memoryCache = getMemoryCache();
|
|
memoryCache.set(key, entry);
|
|
},
|
|
async del(keys) {
|
|
const memoryCache = getMemoryCache();
|
|
keys.forEach((key) => memoryCache.delete(key));
|
|
},
|
|
async revalidateTags(tags) {
|
|
const memoryCache = getMemoryCache();
|
|
|
|
memoryCache.forEach((entry, key) => {
|
|
if (tags.some((tag) => entry.meta.tags.includes(tag))) {
|
|
memoryCache.delete(key);
|
|
}
|
|
});
|
|
|
|
return {
|
|
keys: [],
|
|
metas: [],
|
|
};
|
|
},
|
|
};
|
|
|
|
/**
|
|
* With next-on-pages, the code seems to be isolated between the middleware and the handler.
|
|
* To share the cache between the two, we use a global variable.
|
|
*/
|
|
function getMemoryCache(): Map<string, CacheEntry> {
|
|
// @ts-ignore
|
|
if (!globalThis.gitbookMemoryCache) {
|
|
// @ts-ignore
|
|
globalThis.gitbookMemoryCache = new Map();
|
|
}
|
|
|
|
// @ts-ignore
|
|
return globalThis.gitbookMemoryCache;
|
|
}
|