/** In-memory cache adapter with time-based expiration. Used as the default result cache. */
export class MemoryCacheAdapter {
  #data = new Map();
  #options;
  constructor(options) {
    this.#options = options;
  }
  /**
   * @inheritDoc
   */
  get(name) {
    const data = this.#data.get(name);
    if (data) {
      if (data.expiration < Date.now()) {
        this.#data.delete(name);
      } else {
        return data.data;
      }
    }
    return undefined;
  }
  /**
   * @inheritDoc
   */
  set(name, data, origin, expiration) {
    this.#data.set(name, { data, expiration: Date.now() + (expiration ?? this.#options.expiration) });
  }
  /**
   * @inheritDoc
   */
  remove(name) {
    this.#data.delete(name);
  }
  /**
   * @inheritDoc
   */
  clear() {
    this.#data.clear();
  }
}
