mirror of
https://github.com/yusufipk/OpenFrame.git
synced 2026-09-12 01:46:08 +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;
|
expiresAt: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const BUNNY_SOURCE_CACHE_MAX_ENTRIES = 500;
|
||||||
const bunnyDownloadSourceCache = new Map<string, BunnyDownloadSourceCacheRecord>();
|
const bunnyDownloadSourceCache = new Map<string, BunnyDownloadSourceCacheRecord>();
|
||||||
|
|
||||||
function sanitizeFileName(value: string): string {
|
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 {
|
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, {
|
bunnyDownloadSourceCache.set(cacheKey, {
|
||||||
source,
|
source,
|
||||||
expiresAt: now + BUNNY_SOURCE_RESOLUTION_CACHE_TTL_MS,
|
expiresAt: now + BUNNY_SOURCE_RESOLUTION_CACHE_TTL_MS,
|
||||||
|
|||||||
@@ -124,55 +124,29 @@ export async function POST(request: NextRequest, { params }: RouteParams) {
|
|||||||
// Calculate percentage
|
// Calculate percentage
|
||||||
const safeDuration = duration || 0;
|
const safeDuration = duration || 0;
|
||||||
const percentage = safeDuration > 0 ? Math.min(100, (progress / safeDuration) * 100) : 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: {
|
where: {
|
||||||
userId_versionId: {
|
userId_versionId: {
|
||||||
userId: session.user.id,
|
userId: session.user.id,
|
||||||
versionId: targetVersion.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({
|
return successResponse({
|
||||||
success: true,
|
success: true,
|
||||||
progress: watchProgress.progress,
|
progress: watchProgress.progress,
|
||||||
|
|||||||
Reference in New Issue
Block a user