@neosjs/vitepress-theme
Version:
NeosJS VitePress theme
28 lines (27 loc) • 584 B
JavaScript
export class LRUCache {
max;
cache;
constructor(max = 10) {
this.max = max;
this.cache = /* @__PURE__ */ new Map();
}
get(key) {
const item = this.cache.get(key);
if (item !== void 0) {
this.cache.delete(key);
this.cache.set(key, item);
}
return item;
}
set(key, val) {
if (this.cache.has(key)) this.cache.delete(key);
else if (this.cache.size === this.max) this.cache.delete(this.first());
this.cache.set(key, val);
}
first() {
return this.cache.keys().next().value;
}
clear() {
this.cache.clear();
}
}