From ea05f9c929a5b47e9e6c6c2313fbffd90662c092 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yusuf=20=C4=B0pek?= Date: Thu, 9 Apr 2026 17:18:34 +0300 Subject: [PATCH] feat(cache): implement max entries limit for Bunny download source cache --- .../versions/[versionId]/download/route.ts | 6 +++ app/api/watch/[videoId]/progress/route.ts | 54 +++++-------------- 2 files changed, 20 insertions(+), 40 deletions(-) diff --git a/app/api/versions/[versionId]/download/route.ts b/app/api/versions/[versionId]/download/route.ts index 497fa86..c677893 100644 --- a/app/api/versions/[versionId]/download/route.ts +++ b/app/api/versions/[versionId]/download/route.ts @@ -39,6 +39,7 @@ type BunnyDownloadSourceCacheRecord = { expiresAt: number; }; +const BUNNY_SOURCE_CACHE_MAX_ENTRIES = 500; const bunnyDownloadSourceCache = new Map(); 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, diff --git a/app/api/watch/[videoId]/progress/route.ts b/app/api/watch/[videoId]/progress/route.ts index 2237149..bcf44bc 100644 --- a/app/api/watch/[videoId]/progress/route.ts +++ b/app/api/watch/[videoId]/progress/route.ts @@ -124,55 +124,29 @@ export async function POST(request: NextRequest, { params }: RouteParams) { // Calculate percentage const safeDuration = duration || 0; const percentage = safeDuration > 0 ? Math.min(100, (progress / safeDuration) * 100) : 0; - const tinyProgressDelta = 0.5; - const tinyDurationDelta = 1; - const existingWatchProgress = await db.watchProgress.findUnique({ + // Client already filters tiny deltas (<2s) before sending — safe to upsert directly. + const watchProgress = await db.watchProgress.upsert({ where: { userId_versionId: { userId: session.user.id, versionId: targetVersion.id, }, }, + update: { + progress, + duration: safeDuration, + percentage, + }, + create: { + userId: session.user.id, + versionId: targetVersion.id, + progress, + duration: safeDuration, + percentage, + }, }); - if (existingWatchProgress) { - const progressDiff = Math.abs(existingWatchProgress.progress - progress); - const durationDiff = Math.abs(existingWatchProgress.duration - safeDuration); - if (progressDiff < tinyProgressDelta && durationDiff < tinyDurationDelta) { - return successResponse({ - success: true, - progress: existingWatchProgress.progress, - percentage: existingWatchProgress.percentage, - skipped: true, - }); - } - } - - const watchProgress = existingWatchProgress - ? await db.watchProgress.update({ - where: { - userId_versionId: { - userId: session.user.id, - versionId: targetVersion.id, - }, - }, - data: { - progress, - duration: safeDuration, - percentage, - }, - }) - : await db.watchProgress.create({ - data: { - userId: session.user.id, - versionId: targetVersion.id, - progress, - duration: safeDuration, - percentage, - }, - }); - return successResponse({ success: true, progress: watchProgress.progress,