mirror of
https://github.com/yusufipk/OpenFrame.git
synced 2026-09-11 17:46:06 +00:00
feat(cache): implement max entries limit for Bunny download source cache
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -124,47 +124,21 @@ 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,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
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: {
|
||||
update: {
|
||||
progress,
|
||||
duration: safeDuration,
|
||||
percentage,
|
||||
},
|
||||
})
|
||||
: await db.watchProgress.create({
|
||||
data: {
|
||||
create: {
|
||||
userId: session.user.id,
|
||||
versionId: targetVersion.id,
|
||||
progress,
|
||||
|
||||
Reference in New Issue
Block a user