import { getCacheKey } from "./base.js"; import { Generation, BaseCache } from "../schema/index.js"; const GLOBAL_MAP = new Map(); export class InMemoryCache extends BaseCache { private cache: Map; constructor(map?: Map) { super(); this.cache = map ?? new Map(); } lookup(prompt: string, llmKey: string): Promise { return Promise.resolve(this.cache.get(getCacheKey(prompt, llmKey)) ?? null); } async update(prompt: string, llmKey: string, value: T): Promise { this.cache.set(getCacheKey(prompt, llmKey), value); } static global(): InMemoryCache { return new InMemoryCache(GLOBAL_MAP); } }