// deno-lint-ignore-file ban-types import { LRUCache } from 'lru-cache'; type FetchFn = (key: K, opts: O) => Promise; interface FetchFnOpts { signal?: AbortSignal | null; } export class SimpleLRU< K extends {}, V extends {}, O extends {} = FetchFnOpts, > { protected cache: LRUCache; constructor(fetchFn: FetchFn, opts: LRUCache.Options) { this.cache = new LRUCache({ fetchMethod: (key, _staleValue, { signal }) => fetchFn(key, { signal: signal as AbortSignal }), ...opts, }); } async fetch(key: K, opts?: O): Promise { const result = await this.cache.fetch(key, opts); if (result === undefined) { throw new Error('SimpleLRU: fetch failed'); } return result; } put(key: K, value: V): Promise { this.cache.set(key, value); return Promise.resolve(); } }