memorium/lib/cache/documents.ts

32 lines
722 B
TypeScript
Raw Normal View History

import { Document } from "@lib/documents.ts";
import * as cache from "@lib/cache/cache.ts";
2023-08-02 01:58:03 +02:00
const CACHE_INTERVAL = 20; // 5 seconds;
const CACHE_KEY = "documents";
export async function getDocuments() {
2023-08-02 01:58:03 +02:00
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,
2023-08-02 01:58:03 +02:00
JSON.stringify(documents),
{ expires: CACHE_INTERVAL },
);
}
2023-08-02 01:58:03 +02:00
export function getDocument(id: string) {
return cache.get<string>(CACHE_KEY + "/" + id);
}
export async function setDocument(id: string, content: string) {
await cache.set(
CACHE_KEY + "/" + id,
2023-08-02 01:58:03 +02:00
content,
{ expires: CACHE_INTERVAL },
);
}