hexo/node_modules/hexo-util/lib/cache.js

45 lines
650 B
JavaScript
Raw Normal View History

2023-10-03 11:14:36 +08:00
'use strict';
module.exports = class Cache {
constructor() {
this.cache = new Map();
}
set(id, value) {
this.cache.set(id, value);
}
has(id) {
return this.cache.has(id);
}
get(id) {
return this.cache.get(id);
}
del(id) {
this.cache.delete(id);
}
apply(id, value) {
if (this.has(id)) return this.get(id);
if (typeof value === 'function') value = value();
this.set(id, value);
return value;
}
flush() {
this.cache.clear();
}
size() {
return this.cache.size;
}
dump() {
return Object.fromEntries(this.cache);
}
};