UNPKG

@websolutespa/payload-plugin-bowl

Version:

Bowl PayloadCms plugin of the BOM Repository

33 lines (32 loc) 1.04 kB
// default in memory cache duration 5 minutes const CACHE_DURATION = 5 * 60 * 1000; export class InMemoryCache { duration = CACHE_DURATION; cache; constructor(options = {}){ this.duration = typeof options.duration !== 'undefined' ? options.duration : this.duration; this.cache = new Map(); } isExpired_(key) { return this.duration === false ? true : this.cache.get(key).timestamp < new Date().getTime() - this.duration; } has(key) { return this.cache.has(key) && !this.isExpired_(key); } get(key) { // log('InMemoryCache.get', key); return this.cache.get(key).data; } set(key, data) { // log('InMemoryCache.set', key); this.cache.set(key, { timestamp: new Date().getTime(), data }); } } export function keyWithRequest(key, req) { const segments = req.url?.split('?'); return segments && segments.length > 1 ? `${key}-${segments[1]}` : key; } //# sourceMappingURL=cache.service.js.map