typescript-cacheable
Version:
An in-memory caching (memoization) decorator for Typescript
60 lines • 1.67 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExpiringMap = void 0;
const events_1 = __importDefault(require("events"));
const MapEntry_1 = require("./MapEntry");
class ExpiringMap extends events_1.default {
constructor() {
super();
this.store = new Map();
this.on('save', (_args) => {
this.clean();
});
}
set(key, value, duration) {
const entity = new MapEntry_1.MapEntry(value, duration);
this.store.set(key, entity);
this.emit('save', key, value, duration);
}
get(key) {
const entity = this.store.get(key);
if (entity === undefined) {
return undefined;
}
if (entity.isExpired) {
this.store.delete(key);
return undefined;
}
return entity.data;
}
has(key) {
var _a;
return this.store.has(key) && !((_a = this.store.get(key)) === null || _a === void 0 ? void 0 : _a.isExpired);
}
delete(key) {
if (this.store.has(key)) {
this.store.delete(key);
}
}
clear() {
this.store.clear();
}
get size() {
return this.store.size;
}
keys() {
return Array.from(this.store.keys());
}
clean() {
this.store.forEach((value, key) => {
if (value.isExpired) {
this.store.delete(key);
}
});
}
}
exports.ExpiringMap = ExpiringMap;
//# sourceMappingURL=ExpiringMap.js.map
;