dino-core
Version:
A dependency injection framework for NodeJS applications
63 lines • 2.17 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CacheManager = void 0;
const tslib_1 = require("tslib");
// Copyright 2021 Quirino Brizi [quirino.brizi@gmail.com]
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
const lru_cache_1 = tslib_1.__importDefault(require("lru-cache"));
class CacheManager {
constructor() {
this.caches = new Map();
}
store(cacheName, key, value, opts) {
this.getCacheByName(cacheName, opts).set(key, value);
return this;
}
get(cacheName, key, opts) {
return this.getCacheByName(cacheName, opts).get(key);
}
has(cacheName, key) {
return this.getCacheByName(cacheName).has(key);
}
evict(cacheName, key) {
this.getCacheByName(cacheName).del(key);
return this;
}
clear(cacheName) {
this.getCacheByName(cacheName).reset();
return this;
}
create(cacheName, opts) {
if (!this.caches.has(cacheName)) {
// const LRUCache = require('lru-cache')
const lruCache = new lru_cache_1.default(opts);
this.caches.set(cacheName, lruCache);
return lruCache;
}
return this.caches.get(cacheName);
}
/**
*
* @param cacheName the cache name
* @returns an instance of lru-cache
*/
getCacheByName(cacheName, opts = { max: 100, maxAge: 300000, updateAgeOnGet: true }) {
if (this.caches.has(cacheName)) {
return this.caches.get(cacheName);
}
return this.create(cacheName, opts);
}
}
exports.CacheManager = CacheManager;
//# sourceMappingURL=cache.manager.js.map