UNPKG

layered-loader

Version:

Data loader with support for caching and fallback data sources

57 lines 1.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.InMemoryCache = void 0; const memoryCacheUtils_1 = require("./memoryCacheUtils"); const DEFAULT_CONFIGURATION = { cacheType: 'lru-map', maxItems: 500, }; class InMemoryCache { cache; name = 'In-memory cache'; ttlLeftBeforeRefreshInMsecs; constructor(config) { const resolvedConstructor = (0, memoryCacheUtils_1.resolveCacheConstructor)(config.cacheType ?? DEFAULT_CONFIGURATION.cacheType); this.cache = new resolvedConstructor(config.maxItems ?? DEFAULT_CONFIGURATION.maxItems, config.ttlInMsecs ?? 0, config.cacheId, config.globalStatisticsRecord); this.ttlLeftBeforeRefreshInMsecs = config.ttlLeftBeforeRefreshInMsecs; } clear() { this.cache.clear(); } delete(key) { this.cache.delete(key); } deleteMany(keys) { for (let i = 0; i < keys.length; i++) { this.delete(keys[i]); } } get(key) { return this.cache.get(key); } getMany(keys) { const resolvedValues = []; const unresolvedKeys = []; for (let i = 0; i < keys.length; i++) { const resolvedValue = this.cache.get(keys[i]); if (resolvedValue) { resolvedValues.push(resolvedValue); } else { unresolvedKeys.push(keys[i]); } } return { resolvedValues, unresolvedKeys, }; } getExpirationTime(key) { return this.cache.expiresAt(key); } set(key, value) { this.cache.set(key, value); } } exports.InMemoryCache = InMemoryCache; //# sourceMappingURL=InMemoryCache.js.map