import { Valkey } from "iovalkey"; const PREFIX = "mancala:cache:"; // Short-lived cache of JSON values. Never fails: errors count as a miss, so callers just compute the value. export interface Cache { getJson(key: string): Promise; setJson(key: string, value: unknown, ttlSeconds: number): Promise; delete(...keys: string[]): Promise; } export class ValkeyCache implements Cache { constructor(private readonly client: Valkey) { } async getJson(key: string): Promise { try { const value = await this.client.get(PREFIX + key); return value === null ? undefined : JSON.parse(value) as T; } catch (err) { return undefined; } } async setJson(key: string, value: unknown, ttlSeconds: number): Promise { try { await this.client.set(PREFIX + key, JSON.stringify(value), "EX", ttlSeconds); } catch (err) { // A missing cache entry only costs a database query. } } async delete(...keys: string[]): Promise { if (keys.length === 0) return; try { await this.client.del(...keys.map((key) => PREFIX + key)); } catch (err) { // Entries expire on their own. } } }