Add translation cache metrics, let the cache be configurable

This commit is contained in:
Alex Gleason 2024-10-10 14:12:20 -05:00
parent d639d9a14d
commit 12d643e150
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
4 changed files with 21 additions and 11 deletions

View file

@ -1,16 +1,11 @@
import { LanguageCode } from 'iso-639-1';
import { LRUCache } from 'lru-cache';
import { Conf } from '@/config.ts';
import { MastodonTranslation } from '@/entities/MastodonTranslation.ts';
import { Time } from '@/utils/time.ts';
/** Entity returned by DittoTranslator and LRUCache */
interface DittoTranslation {
data: MastodonTranslation;
}
/** Translations LRU cache. */
export const translationCache = new LRUCache<`${LanguageCode}-${string}`, DittoTranslation>({
max: 1000,
ttl: Time.hours(6),
export const translationCache = new LRUCache<`${LanguageCode}-${string}`, MastodonTranslation>({
max: Conf.caches.translation.max,
ttl: Conf.caches.translation.ttl,
});

View file

@ -314,6 +314,13 @@ class Conf {
ttl: Number(Deno.env.get('DITTO_CACHE_LINK_PREVIEW_TTL') || 12 * 60 * 60 * 1000),
};
},
/** Translation cache settings. */
get translation(): { max: number; ttl: number } {
return {
max: Number(Deno.env.get('DITTO_CACHE_TRANSLATION_MAX') || 1000),
ttl: Number(Deno.env.get('DITTO_CACHE_TRANSLATION_TTL') || 6 * 60 * 60 * 1000),
};
},
};
}

View file

@ -4,6 +4,7 @@ import { z } from 'zod';
import { AppController } from '@/app.ts';
import { translationCache } from '@/caches/translationCache.ts';
import { MastodonTranslation } from '@/entities/MastodonTranslation.ts';
import { cachedTranslationsSizeGauge } from '@/metrics.ts';
import { getEvent } from '@/queries.ts';
import { localeSchema } from '@/schema.ts';
import { parseBody } from '@/utils/api.ts';
@ -50,7 +51,7 @@ const translateController: AppController = async (c) => {
const cached = translationCache.get(cacheKey);
if (cached) {
return c.json(cached.data, 200);
return c.json(cached, 200);
}
const mediaAttachments = status?.media_attachments.map((value) => {
@ -131,7 +132,9 @@ const translateController: AppController = async (c) => {
mastodonTranslation.detected_source_language = data.source_lang;
translationCache.set(cacheKey, { data: mastodonTranslation });
translationCache.set(cacheKey, mastodonTranslation);
cachedTranslationsSizeGauge.set(translationCache.size);
return c.json(mastodonTranslation, 200);
} catch (e) {
if (e instanceof Error && e.message.includes('not supported')) {

View file

@ -121,6 +121,11 @@ export const cachedLinkPreviewSizeGauge = new Gauge({
help: 'Number of link previews in cache',
});
export const cachedTranslationsSizeGauge = new Gauge({
name: 'ditto_cached_translations_size',
help: 'Number of translated statuses in cache',
});
export const internalSubscriptionsSizeGauge = new Gauge({
name: 'ditto_internal_subscriptions_size',
help: "Number of active subscriptions to Ditto's internal relay",