@beenotung/tslib
Version:
utils library in Typescript
50 lines (49 loc) • 1.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CountedCache = void 0;
class CountedCache {
caches = {};
count = 0;
get length() {
return this.count;
}
// return Array<[key, {count: number, data: T}]>
getAll() {
return Object.keys(this.caches).map(key => [key, this.caches[key]]);
}
clear() {
this.caches = {};
}
has(key) {
return key in this.caches;
}
set(key, value) {
const item = this.caches[key];
if (item) {
item.count++;
item.data = value;
}
else {
this.caches[key] = {
count: 1,
data: value,
};
this.count++;
}
}
get(key) {
const item = this.caches[key];
if (item) {
item.count++;
return item.data;
}
return null;
}
remove(key) {
if (key in this.caches) {
delete this.caches[key];
this.count--;
}
}
}
exports.CountedCache = CountedCache;