export class Cache { private cache: Map = new Map(); constructor(public kv: KVNamespace, public disableCache: boolean = false) {} async get(id: string): Promise { if (this.disableCache) { return null; } // console.log("cache get", id); let item = this.cache.get(id); if (!item) { // console.log("remote cache get", id); item = await this.kv.get(id, "json"); if (item) { // console.log("local cache miss, remote cache hit", id); this.cache.set(id, item); } } return item; } async put(id: string, world: T): Promise { if (this.disableCache) { return world; } this.cache.set(id, world); await this.kv.put(id, JSON.stringify(world), { expirationTtl: 60 * 3, }); return world; } }