feat(cache): implement max entries limit for Bunny download source cache

This commit is contained in:
Yusuf İpek
2026-04-09 17:18:34 +03:00
parent 26cf58a28c
commit ea05f9c929
2 changed files with 20 additions and 40 deletions
@@ -39,6 +39,7 @@ type BunnyDownloadSourceCacheRecord = {
expiresAt: number;
};
const BUNNY_SOURCE_CACHE_MAX_ENTRIES = 500;
const bunnyDownloadSourceCache = new Map<string, BunnyDownloadSourceCacheRecord>();
function sanitizeFileName(value: string): string {
@@ -95,6 +96,11 @@ function getCachedBunnyDownloadSource(cacheKey: string, now: number): BunnyDownl
}
function setCachedBunnyDownloadSource(cacheKey: string, source: BunnyDownloadSource | null, now: number): void {
if (bunnyDownloadSourceCache.size >= BUNNY_SOURCE_CACHE_MAX_ENTRIES) {
// Evict the oldest entry (Maps preserve insertion order)
const firstKey = bunnyDownloadSourceCache.keys().next().value;
if (firstKey !== undefined) bunnyDownloadSourceCache.delete(firstKey);
}
bunnyDownloadSourceCache.set(cacheKey, {
source,
expiresAt: now + BUNNY_SOURCE_RESOLUTION_CACHE_TTL_MS,