@inventivetalent/loading-cache
Version:
loading cache based on ben-manes/caffeine and node-cache
43 lines • 1.22 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CacheStats = void 0;
const events_1 = require("events");
const CacheEvents_1 = require("./CacheEvents");
/**
* Static definitions of all cache stats
*/
class CacheStats extends events_1.EventEmitter {
constructor() {
super(...arguments);
this.map = new Map();
}
inc(key, amount = 1) {
if (amount === 0)
return;
const prev = this.get(key);
this.map.set(key, prev + amount);
try {
this.emit(CacheEvents_1.CacheEvents.STAT, key, amount, prev);
}
catch (e) {
console.error(e);
}
}
get(key) {
return this.map.get(key) || 0;
}
reset() {
this.map.clear();
}
toString() {
return JSON.stringify(this.map, null, 2);
}
}
exports.CacheStats = CacheStats;
CacheStats.HIT = "hit";
CacheStats.MISS = "miss";
CacheStats.LOAD_SUCCESS = "load_success";
CacheStats.LOAD_FAIL = "load_failure";
CacheStats.EXPIRE = "expire";
CacheStats.ALL = [CacheStats.HIT, CacheStats.MISS, CacheStats.LOAD_SUCCESS, CacheStats.LOAD_FAIL, CacheStats.EXPIRE];
//# sourceMappingURL=CacheStats.js.map