Merge branch 'inc-cache' into 'main'

Make cache settings configurable, increase default values

See merge request soapbox-pub/ditto!506
This commit is contained in:
Alex Gleason 2024-09-20 18:29:29 +00:00
commit 15372ceb43
4 changed files with 28 additions and 9 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

@ -2,9 +2,9 @@ import { DOMParser } from '@b-fuze/deno-dom';
import Debug from '@soapbox/stickynotes/debug';
import tldts from 'tldts';
import { Conf } from '@/config.ts';
import { cachedFaviconsSizeGauge } from '@/metrics.ts';
import { SimpleLRU } from '@/utils/SimpleLRU.ts';
import { Time } from '@/utils/time.ts';
import { fetchWorker } from '@/workers/fetch.ts';
const debug = Debug('ditto:favicon');
@ -38,7 +38,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

@ -7,7 +7,6 @@ import { Conf } from '@/config.ts';
import { cachedNip05sSizeGauge } from '@/metrics.ts';
import { Storages } from '@/storages.ts';
import { SimpleLRU } from '@/utils/SimpleLRU.ts';
import { Time } from '@/utils/time.ts';
import { Nip05, parseNip05 } from '@/utils.ts';
import { fetchWorker } from '@/workers/fetch.ts';
@ -44,7 +43,7 @@ const nip05Cache = new SimpleLRU<string, nip19.ProfilePointer>(
throw e;
}
},
{ max: 500, 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: 500,
});
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> {