import type { CacheStore } from "./redis-cache";

export async function cachedCompute<T>(
  cache: CacheStore,
  key: string,
  compute: () => Promise<T>,
  ttlSeconds?: number,
): Promise<{ value: T; cached: boolean }> {
  const hit = await cache.getJson<T>(key);
  if (hit !== undefined) return { value: hit, cached: true };

  const value = await compute();
  await cache.setJson(key, value, ttlSeconds);
  return { value, cached: false };
}

