openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
57 lines (56 loc) • 1.34 kB
JavaScript
//#region src/infra/lru-cache.ts
/** Process-local LRU storage; callers own freshness and lifecycle. */
var LruCache = class {
#e;
#t;
#n;
#r = /* @__PURE__ */ new Map();
#i = 0;
constructor(e, t) {
this.#e = Number.isFinite(e) && e > 0 ? Math.max(1, Math.floor(e)) : 1;
this.#t = t?.maxBytes ?? Number.POSITIVE_INFINITY;
this.#n = t?.sizeOf;
}
get size() {
return this.#r.size;
}
clear() {
this.#r.clear();
this.#i = 0;
}
keys() {
return this.#r.keys();
}
delete(e) {
if (!this.#r.has(e)) return false;
this.#i -= this.#n?.(this.#r.get(e)) ?? 0;
return this.#r.delete(e);
}
deleteValue(e) {
for (const [t, n] of this.#r) if (n === e) this.delete(t);
}
/** Reads without promoting a value whose revalidation is still pending. */
peek(e) {
return this.#r.get(e);
}
/** Returns a cached value and refreshes its recency when present. */
get(e) {
if (!this.#r.has(e)) return;
const t = this.#r.get(e);
this.#r.delete(e);
this.#r.set(e, t);
return t;
}
/** Stores a value as the newest entry and evicts oldest entries past capacity. */
set(e, t) {
this.delete(e);
this.#r.set(e, t);
this.#i += this.#n?.(t) ?? 0;
for (const e of this.#r.keys()) {
if (this.#r.size <= this.#e && this.#i <= this.#t) break;
this.delete(e);
}
}
};
//#endregion
export { LruCache as t };