Files
GMW/services/discord-gateway/src/modules/ai-moderation/mediaCache.ts
T
asepharyana d3cb5f6756 refactor: rombak cache AI analisis image — pakai CDN URL langsung, hapus phash+sha
- Cache key image = CDN URL (query params stripped), bukan SHA data URL
  → re-analysis SAME attachment selalu cache-hit, berbeda attachment tidak kolisi
- Hapus perceptual hash (imghash dep + phash get/upsert/compute) sepenuhnya
- Hapus makeImageCacheKey hashing, ganti makeImageCacheKey yang return CDN URL
- textCacheStore, visionAnalyzer, mediaCache, mediaAnalysisClient updated
- imghash dependency removed from package.json
- Purge 82 stale cache rows (image: + phash:) dari DB
2026-08-12 22:31:08 +07:00

46 lines
1.4 KiB
TypeScript

/**
* mediaCache.ts
*
* LRU cache and DB-backed caching layer for vision analysis results,
* including phash-based deduplication and distributed locking.
*/
import { LRUCache } from "lru-cache";
import {
acquireMediaAnalysisLock,
deleteCachedMediaAnalysis,
getCachedMediaAnalysis,
makeCustomEmojiCacheKey,
makeImageCacheKey,
makeStickerCacheKey,
upsertCachedMediaAnalysis,
} from "./textCacheStore.js";
export {
acquireMediaAnalysisLock,
deleteCachedMediaAnalysis,
getCachedMediaAnalysis,
makeCustomEmojiCacheKey,
makeImageCacheKey,
makeStickerCacheKey,
upsertCachedMediaAnalysis,
};
/** Convenience alias for upsertCachedMediaAnalysis. */
export const setCachedMediaAnalysis = upsertCachedMediaAnalysis;
/** In-memory LRU cache for vision analysis text results. */
export const visionLruCache = new LRUCache<string, string>({
max: 500,
ttl: 24 * 60 * 60 * 1000,
});
/** Deduplicate in-flight vision analysis calls per cache key. */
export const inFlightVisionCalls = new Map<string, Promise<string>>();
/**
* Sentinel value returned when image download or vision analysis fails
* after exhausting all retries.
*/
export const FAILED_ANALYSIS_PREFIX =
"GAGAL DIANALISIS — gambar tidak dapat diunduh atau vision API gagal setelah 3x percobaan. JANGAN mengasumsikan gambar aman hanya karena gagal dianalisis. Gunakan metadata URL/nama file saja sebagai petunjuk.";