@gabliam/cache
Version:
cache plugin for gabliam
114 lines (113 loc) • 5.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CachePluginConfig = void 0;
const tslib_1 = require("tslib");
/* eslint-disable @typescript-eslint/no-redeclare */
const core_1 = require("@gabliam/core");
const debug_1 = tslib_1.__importDefault(require("debug"));
const lodash_1 = tslib_1.__importDefault(require("lodash"));
const caches_1 = require("./caches");
const constant_1 = require("./constant");
const error_1 = require("./error");
const simple_cache_manager_1 = require("./simple-cache-manager");
const debug = (0, debug_1.default)('Gabliam:Plugin:CachePlugin');
const stringOrClass = core_1.Joi.alternatives().try(core_1.Joi.string(), core_1.Joi.func());
const cacheValidator = core_1.Joi.object({
cache: stringOrClass,
options: core_1.Joi.object(),
});
const pluginValidator = core_1.Joi.object().keys({
cacheManager: stringOrClass.default('SimpleCacheManager'),
dynamic: core_1.Joi.boolean().default(true),
groups: core_1.Joi.object()
.pattern(/.*/, core_1.Joi.object({
defaultCache: stringOrClass,
defaultOptionsCache: core_1.Joi.object(),
caches: core_1.Joi.object().pattern(/.*/, cacheValidator),
}))
.default(undefined),
defaultCache: stringOrClass.default('NoOpCache'),
defaultOptionsCache: core_1.Joi.object(),
});
let CachePluginConfig = class CachePluginConfig {
createCacheManager() {
if (this.cacheConfig === undefined) {
return this.createDefaultManager();
}
const groups = new Map();
if (this.cacheConfig.groups) {
for (const [groupKey, { defaultCache, caches, defaultOptionsCache },] of Object.entries(this.cacheConfig.groups)) {
const groupCache = {
defaultCache,
defaultOptionsCache,
caches: new Map(),
};
groups.set(groupKey, groupCache);
if (caches) {
for (const [cacheKey, { cache, options }] of Object.entries(caches)) {
const CacheConstruc = this.getCacheConstruct(cache || defaultCache || this.cacheConfig.defaultCache);
const cacheOptions = lodash_1.default.merge({}, this.cacheConfig.defaultOptionsCache || {}, defaultOptionsCache || {}, options || {});
groupCache.caches.set(cacheKey, new CacheConstruc(cacheKey, cacheOptions));
}
}
}
}
const CacheManagerConstruct = this.getCacheManagerConstruct(this.cacheConfig.cacheManager);
return new CacheManagerConstruct(groups, this.cacheConfig.dynamic, this.getCacheConstruct(this.cacheConfig.defaultCache), this.cacheConfig.defaultOptionsCache);
}
getCacheManagerConstruct(cacheManager) {
if (typeof cacheManager === 'string') {
switch (cacheManager) {
case 'SimpleCacheManager':
return simple_cache_manager_1.SimpleCacheManager;
default:
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires, import/no-dynamic-require, global-require
return require(cacheManager).default;
}
catch (_a) {
throw new error_1.CacheManagerPgkNotInstalledError(cacheManager);
}
}
}
return cacheManager;
}
getCacheConstruct(cache) {
if (typeof cache === 'string') {
switch (cache) {
case 'MemoryCache':
return caches_1.MemoryCache;
case 'NoOpCache':
return caches_1.NoOpCache;
default:
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires, import/no-dynamic-require, global-require
return require(cache).default;
}
catch (_a) {
throw new error_1.CachePgkNotInstalledError(cache);
}
}
}
return cache;
}
createDefaultManager() {
debug('Create Defaut cache Manager');
return new simple_cache_manager_1.SimpleCacheManager(new Map(), true);
}
};
tslib_1.__decorate([
(0, core_1.Value)('application.cacheConfig', pluginValidator),
tslib_1.__metadata("design:type", Object)
], CachePluginConfig.prototype, "cacheConfig", void 0);
tslib_1.__decorate([
(0, core_1.Bean)(constant_1.CACHE_MANAGER),
tslib_1.__metadata("design:type", Function),
tslib_1.__metadata("design:paramtypes", []),
tslib_1.__metadata("design:returntype", Object)
], CachePluginConfig.prototype, "createCacheManager", null);
CachePluginConfig = tslib_1.__decorate([
(0, core_1.InjectContainer)(),
(0, core_1.PluginConfig)()
], CachePluginConfig);
exports.CachePluginConfig = CachePluginConfig;