Skip to content

Spot it. Save it. Free, local-first Chrome extension for images, videos and prompts. Explore 2,000+ curated cases and connect your library to AI Agents via MCP. 提示词导演

1.3 KB
Raw Blame
// Evict only resources that consumers have released. A protected working set
// may temporarily exceed the budget; retry trimming when it is unmounted.
export class ResourceCache extends Map {
  constructor({ maxEntries, maxBytes = Infinity, cost = () => 1, protectedValue = () => false, dispose = () => {} }) {
    super(); this.policy = { maxEntries, maxBytes, cost, protectedValue, dispose };
  }
  get(key) {
    if (!super.has(key)) return undefined;
    const value = super.get(key); super.delete(key); super.set(key, value); return value;
  }
  set(key, value) {
    const previous = super.get(key);
    super.delete(key);
    if (previous !== undefined && previous !== value) this.release(previous); super.set(key, value); this.trim(key); return this;
  }
  trim(incomingKey) {
    let bytes = [...new Set(this.values())].reduce((sum, value) => sum + this.policy.cost(value), 0);
    for (const [key, value] of this) {
      if (this.size <= this.policy.maxEntries && bytes <= this.policy.maxBytes) break;
      if (key === incomingKey || this.policy.protectedValue(value, key)) continue;
      super.delete(key);
      if (![...this.values()].includes(value)) bytes -= this.policy.cost(value);
      this.release(value);
    }
  }
  release(value) {
    if (![...this.values()].includes(value)) this.policy.dispose(value);
  }
}