Try Deno's experimental KV as a cache

This commit is contained in:
Alex Gleason 2023-05-03 22:15:18 -05:00
parent eb0e9094a4
commit 0c71b5a696
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
1 changed files with 10 additions and 1 deletions

View File

@ -5,6 +5,7 @@ import { poolRelays } from './config.ts';
import { eventDateComparator, nostrNow } from './utils.ts'; import { eventDateComparator, nostrNow } from './utils.ts';
const db = await Deno.openKv();
const pool = new RelayPool(poolRelays); const pool = new RelayPool(poolRelays);
type Filter<K extends number = number> = { type Filter<K extends number = number> = {
@ -86,7 +87,15 @@ const getAuthor = async (pubkey: string): Promise<SignedEvent<0> | undefined> =>
/** Get users the given pubkey follows. */ /** Get users the given pubkey follows. */
const getFollows = async (pubkey: string): Promise<SignedEvent<3> | undefined> => { const getFollows = async (pubkey: string): Promise<SignedEvent<3> | undefined> => {
const [event] = await getFilter({ authors: [pubkey], kinds: [3] }, { timeout: 5000 }); 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<SignedEvent<3>>(['event3', pubkey])).value || undefined;
}
}; };
interface PaginationParams { interface PaginationParams {