31 lines
612 B
JavaScript
31 lines
612 B
JavaScript
//#region src/client/utils/lru.ts
|
|
var LRUCache = class {
|
|
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();
|
|
}
|
|
};
|
|
|
|
//#endregion
|
|
export { LRUCache }; |