Let caches be configurable

This commit is contained in:
Alex Gleason 2024-09-20 13:22:45 -05:00
parent 099fec6e31
commit 807bc78472
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
4 changed files with 28 additions and 7 deletions

View file

@ -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

View file

@ -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<string, URL>(
throw new Error(`Favicon not found: ${key}`);
},
{ max: 500, ttl: Time.hours(1), gauge: cachedFaviconsSizeGauge },
{ ...Conf.caches.favicon, gauge: cachedFaviconsSizeGauge },
);
export { faviconCache };

View file

@ -44,7 +44,7 @@ const nip05Cache = new SimpleLRU<string, nip19.ProfilePointer>(
throw e;
}
},
{ max: 3000, ttl: Time.hours(1), gauge: cachedNip05sSizeGauge },
{ ...Conf.caches.nip05, gauge: cachedNip05sSizeGauge },
);
async function localNip05Lookup(store: NStore, localpart: string): Promise<nip19.ProfilePointer | undefined> {

View file

@ -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<PreviewCard
}
/** TTL cache for preview cards. */
const previewCardCache = new TTLCache<string, Promise<PreviewCard | null>>({
ttl: Time.hours(12),
max: 1000,
});
const previewCardCache = new TTLCache<string, Promise<PreviewCard | null>>(Conf.caches.linkPreview);
/** Unfurl card from cache if available, otherwise fetch it. */
function unfurlCardCached(url: string, signal = AbortSignal.timeout(1000)): Promise<PreviewCard | null> {