From 12d643e150edef61916d35a85cab92e1ffe4af3e Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 10 Oct 2024 14:12:20 -0500 Subject: [PATCH] Add translation cache metrics, let the cache be configurable --- src/caches/translationCache.ts | 13 ++++--------- src/config.ts | 7 +++++++ src/controllers/api/translate.ts | 7 +++++-- src/metrics.ts | 5 +++++ 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/caches/translationCache.ts b/src/caches/translationCache.ts index 88121c1f..7bd27946 100644 --- a/src/caches/translationCache.ts +++ b/src/caches/translationCache.ts @@ -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, }); diff --git a/src/config.ts b/src/config.ts index 806fe196..c0daf894 100644 --- a/src/config.ts +++ b/src/config.ts @@ -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), + }; + }, }; } diff --git a/src/controllers/api/translate.ts b/src/controllers/api/translate.ts index 56bd1b8d..d763c713 100644 --- a/src/controllers/api/translate.ts +++ b/src/controllers/api/translate.ts @@ -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')) { diff --git a/src/metrics.ts b/src/metrics.ts index c005a6c7..2cb3eb2d 100644 --- a/src/metrics.ts +++ b/src/metrics.ts @@ -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",