Cache the LNURL response

This commit is contained in:
Alex Gleason
2024-01-22 12:42:39 -06:00
parent d73fa7a311
commit 13c50c71bd
3 changed files with 27 additions and 5 deletions

View File

@@ -1,4 +1,24 @@
import { LNURL } from '@/deps.ts';
import { Debug, LNURL, type LNURLDetails } from '@/deps.ts';
import { SimpleLRU } from '@/utils/SimpleLRU.ts';
import { Time } from '@/utils/time.ts';
import { fetchWorker } from '@/workers/fetch.ts';
const debug = Debug('ditto:lnurl');
const lnurlCache = new SimpleLRU<string, LNURLDetails>(
async (lnurl, { signal }) => {
debug(`Lookup ${lnurl}`);
try {
const result = await LNURL.lookup(lnurl, { fetch: fetchWorker, signal });
debug(`Found: ${lnurl}`);
return result;
} catch (e) {
debug(`Not found: ${lnurl}`);
throw e;
}
},
{ max: 1000, ttl: Time.minutes(30) },
);
/** Get an LNURL from a lud06 or lud16. */
function getLnurl({ lud06, lud16 }: { lud06?: string; lud16?: string }, limit?: number): string | undefined {
@@ -12,4 +32,4 @@ function getLnurl({ lud06, lud16 }: { lud06?: string; lud16?: string }, limit?:
}
}
export { getLnurl };
export { getLnurl, lnurlCache };