From 0c71b5a6969d8ee78dd817930cead64e4b5bc617 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 3 May 2023 22:15:18 -0500 Subject: [PATCH] Try Deno's experimental KV as a cache --- src/client.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/client.ts b/src/client.ts index 9a624249..e014d163 100644 --- a/src/client.ts +++ b/src/client.ts @@ -5,6 +5,7 @@ import { poolRelays } from './config.ts'; import { eventDateComparator, nostrNow } from './utils.ts'; +const db = await Deno.openKv(); const pool = new RelayPool(poolRelays); type Filter = { @@ -86,7 +87,15 @@ const getAuthor = async (pubkey: string): Promise | undefined> => /** Get users the given pubkey follows. */ const getFollows = async (pubkey: string): Promise | undefined> => { const [event] = await getFilter({ authors: [pubkey], kinds: [3] }, { timeout: 5000 }); - return event; + + // TODO: figure out a better, more generic & flexible way to handle event (and timeouts?) + // Prewarm cache in GET `/api/v1/accounts/verify_credentials` + if (event) { + await db.set(['event3', pubkey], event); + return event; + } else { + return (await db.get>(['event3', pubkey])).value || undefined; + } }; interface PaginationParams {