Aléki
Aléki•11mo ago

Looking for caching API Responses

Hello there, I'm looking for a package/or examples to cache API responses for a fresh project which call a mongodb server. I already check Deno Cache API but it doesn't answer my problem. or I'm using it wrong way Thanks for your time 🙂
5 Replies
javi
javi•11mo ago
You can use Deno KV for that. Something along the lines of
const kv = await Deno.openKv();

const { value } = await kv.get<{ date: Date, data: unknown }>(["cache"])

if (value && Date.now() - value.date.getTime() <= 60 * 60 * 1000) {
return value.data
}

const data = await (await fetch("/my/api")).json()

await kv.set(["cache"], { date: new Date(), data })

return data
const kv = await Deno.openKv();

const { value } = await kv.get<{ date: Date, data: unknown }>(["cache"])

if (value && Date.now() - value.date.getTime() <= 60 * 60 * 1000) {
return value.data
}

const data = await (await fetch("/my/api")).json()

await kv.set(["cache"], { date: new Date(), data })

return data
Aléki
Aléki•11mo ago
Hello javi thx for your answer, will it work on Deno Deploy with read-only permission ?
javi
javi•11mo ago
Hello Aléki! No worries You need access to kv's beta for it to work on deploy
Aléki
Aléki•11mo ago
Thanks a lot , I'll go work on that, have a good day ! 👋
javi
javi•11mo ago
<:to_anime1_wave:965663270310658109>