import { Document } from "@lib/documents.ts";
import * as cache from "@lib/cache/cache.ts";

const CACHE_INTERVAL = 60;
const CACHE_KEY = "documents";

export async function getDocuments() {
  const res = await cache.get<string>(CACHE_KEY);
  if (res) return JSON.parse(res);
  return;
}

export function setDocuments(documents: Document[]) {
  return cache.set(
    CACHE_KEY,
    JSON.stringify(documents),
    { expires: CACHE_INTERVAL },
  );
}

export function getDocument(id: string) {
  return cache.get<string>(CACHE_KEY + ":" + id.replaceAll("/", ":"));
}

export async function setDocument(id: string, content: string) {
  await cache.set(
    CACHE_KEY + ":" + id.replaceAll("/", ":"),
    content,
    { expires: CACHE_INTERVAL },
  );
}
