mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
Add translation cache metrics, let the cache be configurable
This commit is contained in:
parent
d639d9a14d
commit
12d643e150
4 changed files with 21 additions and 11 deletions
|
|
@ -1,16 +1,11 @@
|
||||||
import { LanguageCode } from 'iso-639-1';
|
import { LanguageCode } from 'iso-639-1';
|
||||||
import { LRUCache } from 'lru-cache';
|
import { LRUCache } from 'lru-cache';
|
||||||
|
|
||||||
|
import { Conf } from '@/config.ts';
|
||||||
import { MastodonTranslation } from '@/entities/MastodonTranslation.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. */
|
/** Translations LRU cache. */
|
||||||
export const translationCache = new LRUCache<`${LanguageCode}-${string}`, DittoTranslation>({
|
export const translationCache = new LRUCache<`${LanguageCode}-${string}`, MastodonTranslation>({
|
||||||
max: 1000,
|
max: Conf.caches.translation.max,
|
||||||
ttl: Time.hours(6),
|
ttl: Conf.caches.translation.ttl,
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -314,6 +314,13 @@ class Conf {
|
||||||
ttl: Number(Deno.env.get('DITTO_CACHE_LINK_PREVIEW_TTL') || 12 * 60 * 60 * 1000),
|
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),
|
||||||
|
};
|
||||||
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import { z } from 'zod';
|
||||||
import { AppController } from '@/app.ts';
|
import { AppController } from '@/app.ts';
|
||||||
import { translationCache } from '@/caches/translationCache.ts';
|
import { translationCache } from '@/caches/translationCache.ts';
|
||||||
import { MastodonTranslation } from '@/entities/MastodonTranslation.ts';
|
import { MastodonTranslation } from '@/entities/MastodonTranslation.ts';
|
||||||
|
import { cachedTranslationsSizeGauge } from '@/metrics.ts';
|
||||||
import { getEvent } from '@/queries.ts';
|
import { getEvent } from '@/queries.ts';
|
||||||
import { localeSchema } from '@/schema.ts';
|
import { localeSchema } from '@/schema.ts';
|
||||||
import { parseBody } from '@/utils/api.ts';
|
import { parseBody } from '@/utils/api.ts';
|
||||||
|
|
@ -50,7 +51,7 @@ const translateController: AppController = async (c) => {
|
||||||
const cached = translationCache.get(cacheKey);
|
const cached = translationCache.get(cacheKey);
|
||||||
|
|
||||||
if (cached) {
|
if (cached) {
|
||||||
return c.json(cached.data, 200);
|
return c.json(cached, 200);
|
||||||
}
|
}
|
||||||
|
|
||||||
const mediaAttachments = status?.media_attachments.map((value) => {
|
const mediaAttachments = status?.media_attachments.map((value) => {
|
||||||
|
|
@ -131,7 +132,9 @@ const translateController: AppController = async (c) => {
|
||||||
|
|
||||||
mastodonTranslation.detected_source_language = data.source_lang;
|
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);
|
return c.json(mastodonTranslation, 200);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof Error && e.message.includes('not supported')) {
|
if (e instanceof Error && e.message.includes('not supported')) {
|
||||||
|
|
|
||||||
|
|
@ -121,6 +121,11 @@ export const cachedLinkPreviewSizeGauge = new Gauge({
|
||||||
help: 'Number of link previews in cache',
|
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({
|
export const internalSubscriptionsSizeGauge = new Gauge({
|
||||||
name: 'ditto_internal_subscriptions_size',
|
name: 'ditto_internal_subscriptions_size',
|
||||||
help: "Number of active subscriptions to Ditto's internal relay",
|
help: "Number of active subscriptions to Ditto's internal relay",
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue