@gabliam/cache
Version:
cache plugin for gabliam
47 lines (46 loc) • 1.13 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MemoryCache = void 0;
const tslib_1 = require("tslib");
const lru_cache_1 = tslib_1.__importDefault(require("lru-cache"));
const DEFAULT_OPTIONS = {
max: 50,
};
class MemoryCache {
constructor(name, options = {}) {
this.options = Object.assign(Object.assign({}, DEFAULT_OPTIONS), options);
this.name = name;
}
async start() {
this.store = new lru_cache_1.default(this.options);
}
async stop() {
this.store.clear();
}
getName() {
return this.name;
}
getNativeCache() {
return this;
}
async get(key) {
return this.store.get(key);
}
async put(key, value) {
this.store.set(key, value);
}
async putIfAbsent(key, value) {
if (!this.store.has(key)) {
this.store.set(key, value);
return null;
}
return this.store.get(key);
}
async evict(key) {
this.store.delete(key);
}
async clear() {
this.store.clear();
}
}
exports.MemoryCache = MemoryCache;