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
+14 -40
View File
@@ -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,