Files
gitbook/src/lib/cache/memory.ts
T
Samy Pessé 5f7b29bbc8 Improve purging of cache and return keys (#111)
* 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
2024-01-23 18:21:46 +01:00

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;
}