UNPKG

@gabliam/cache

Version:
57 lines (56 loc) 2.06 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SimpleCacheManager = void 0; const tslib_1 = require("tslib"); const lodash_1 = tslib_1.__importDefault(require("lodash")); const no_op_cache_1 = require("./caches/no-op-cache"); class SimpleCacheManager { constructor(group, dynamic, defaultCache = no_op_cache_1.NoOpCache, defaultOptionsCache) { this.group = group; this.dynamic = dynamic; this.defaultCache = defaultCache; this.defaultOptionsCache = defaultOptionsCache; this.startedCache = new Map(); } async getCache(name, groupName = 'default') { if (!this.group.has(groupName) && this.dynamic) { this.group.set(groupName, { defaultCache: this.defaultCache, defaultOptionsCache: this.defaultOptionsCache, caches: new Map(), }); } const group = this.group.get(groupName); if (!group) { return group; } if (!group.caches.has(name) && this.dynamic) { group.caches.set(name, this.constructCache(group, name)); } const cache = group.caches.get(name); if (!cache) { return cache; } if (!this.startedCache.has(name)) { await cache.start(); this.startedCache.set(name, true); } return cache; } getCacheNames() { const names = []; for (const [, group] of this.group.entries()) { for (const [, cache] of group.caches.entries()) { names.push(cache.getName()); } } return names; } constructCache(group, name) { const defaultCache = group.defaultCache || this.defaultCache; const optionsCache = lodash_1.default.merge({}, this.defaultOptionsCache || {}, group.defaultOptionsCache || {}); // eslint-disable-next-line new-cap return new defaultCache(name, optionsCache); } } exports.SimpleCacheManager = SimpleCacheManager;