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