From 807bc784721e38fddd999d5e1ee35983b5567aca Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Fri, 20 Sep 2024 13:22:45 -0500 Subject: [PATCH] Let caches be configurable --- src/config.ts | 24 ++++++++++++++++++++++++ src/utils/favicon.ts | 3 ++- src/utils/nip05.ts | 2 +- src/utils/unfurl.ts | 6 +----- 4 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/config.ts b/src/config.ts index 955371d4..21fbbe01 100644 --- a/src/config.ts +++ b/src/config.ts @@ -247,6 +247,30 @@ class Conf { static get zapSplitsEnabled(): boolean { return optionalBooleanSchema.parse(Deno.env.get('ZAP_SPLITS_ENABLED')) ?? false; } + /** Cache settings. */ + static caches = { + /** NIP-05 cache settings. */ + get nip05(): { max: number; ttl: number } { + return { + max: Number(Deno.env.get('DITTO_CACHE_NIP05_MAX') || 3000), + ttl: Number(Deno.env.get('DITTO_CACHE_NIP05_TTL') || 1 * 60 * 60 * 1000), + }; + }, + /** Favicon cache settings. */ + get favicon(): { max: number; ttl: number } { + return { + max: Number(Deno.env.get('DITTO_CACHE_FAVICON_MAX') || 500), + ttl: Number(Deno.env.get('DITTO_CACHE_FAVICON_TTL') || 1 * 60 * 60 * 1000), + }; + }, + /** Link preview cache settings. */ + get linkPreview(): { max: number; ttl: number } { + return { + max: Number(Deno.env.get('DITTO_CACHE_LINK_PREVIEW_MAX') || 1000), + ttl: Number(Deno.env.get('DITTO_CACHE_LINK_PREVIEW_TTL') || 12 * 60 * 60 * 1000), + }; + }, + }; } const optionalBooleanSchema = z diff --git a/src/utils/favicon.ts b/src/utils/favicon.ts index 49ca525a..2be327a6 100644 --- a/src/utils/favicon.ts +++ b/src/utils/favicon.ts @@ -6,6 +6,7 @@ import { cachedFaviconsSizeGauge } from '@/metrics.ts'; import { SimpleLRU } from '@/utils/SimpleLRU.ts'; import { Time } from '@/utils/time.ts'; import { fetchWorker } from '@/workers/fetch.ts'; +import { Conf } from '@/config.ts'; const debug = Debug('ditto:favicon'); @@ -38,7 +39,7 @@ const faviconCache = new SimpleLRU( throw new Error(`Favicon not found: ${key}`); }, - { max: 500, ttl: Time.hours(1), gauge: cachedFaviconsSizeGauge }, + { ...Conf.caches.favicon, gauge: cachedFaviconsSizeGauge }, ); export { faviconCache }; diff --git a/src/utils/nip05.ts b/src/utils/nip05.ts index 8f453c93..388ec37f 100644 --- a/src/utils/nip05.ts +++ b/src/utils/nip05.ts @@ -44,7 +44,7 @@ const nip05Cache = new SimpleLRU( throw e; } }, - { max: 3000, ttl: Time.hours(1), gauge: cachedNip05sSizeGauge }, + { ...Conf.caches.nip05, gauge: cachedNip05sSizeGauge }, ); async function localNip05Lookup(store: NStore, localpart: string): Promise { diff --git a/src/utils/unfurl.ts b/src/utils/unfurl.ts index 5763c151..b5f5c4eb 100644 --- a/src/utils/unfurl.ts +++ b/src/utils/unfurl.ts @@ -6,7 +6,6 @@ import { unfurl } from 'unfurl.js'; import { Conf } from '@/config.ts'; import { PreviewCard } from '@/entities/PreviewCard.ts'; import { cachedLinkPreviewSizeGauge } from '@/metrics.ts'; -import { Time } from '@/utils/time.ts'; import { fetchWorker } from '@/workers/fetch.ts'; const debug = Debug('ditto:unfurl'); @@ -55,10 +54,7 @@ async function unfurlCard(url: string, signal: AbortSignal): Promise>({ - ttl: Time.hours(12), - max: 1000, -}); +const previewCardCache = new TTLCache>(Conf.caches.linkPreview); /** Unfurl card from cache if available, otherwise fetch it. */ function unfurlCardCached(url: string, signal = AbortSignal.timeout(1000)): Promise {