BlueGit

timfa filemanager

Merge branch 'feat/smart-eventcache-pruning'

timfa Tim Falken · pushed by timfa

Authored 2026-05-20 · 6 months ago

Hash
0d6588e7fef0f074941937b79b6faca34fb39958
1 file changed +40 −13
server/image-subtitles.js +40 −13
@@ -14,7 +14,8 @@ const FRAME_CACHE_TTL_MS = toPositiveInt(process.env.IMAGE_SUBTITLE_FRAME_CACHE_
14 14 const MAX_EVENT_CACHE_ITEMS = toPositiveInt(process.env.IMAGE_SUBTITLE_EVENT_CACHE_MAX_ITEMS, 3);
15 15 const MAX_FRAME_CACHE_ITEMS = toPositiveInt(process.env.IMAGE_SUBTITLE_FRAME_CACHE_MAX_ITEMS, 50);
16 16 // Removed byte size cap for event cache to prevent thrashing
17 const MAX_EVENT_CACHE_BYTES = Infinity;
17 const MAX_EVENT_CACHE_BYTES = toPositiveInt(process.env.IMAGE_SUBTITLE_EVENT_CACHE_MAX_BYTES, 50 * 1024 * 1024);
18 const MIN_EVENT_WINDOW_SECONDS = 30; // Always keep at least this much for seeking
18 19 const MAX_FRAME_CACHE_BYTES = toPositiveInt(process.env.IMAGE_SUBTITLE_FRAME_CACHE_MAX_BYTES, 48 * 1024 * 1024);
19 20 const IMAGE_SUBTITLE_RENDER_SCALE = toBoundedNumber(process.env.IMAGE_SUBTITLE_RENDER_SCALE, 0.75, 0.1, 1);
20 21 const PGS_TIMEBASE = 90000;
@@ -585,32 +586,58 @@ function pruneOldImageSubtitleCaches (filePath, slowestViewerTimeSeconds)
585 586 const prefix = cachePrefixForFile(filePath);
586 587 if (!prefix || !Number.isFinite(slowestViewerTimeSeconds)) return;
587 588
589 // Prune frameCache as before
588 590 const keysToDelete = [];
589 for (const key of frameCache.map.keys())
590 {
591 for (const key of frameCache.map.keys()) {
591 592 if (!key.startsWith(prefix)) continue;
592
593 593 const entry = frameCache.map.get(key);
594 594 if (!entry) continue;
595
596 595 const eventEntry = entry.value || {};
597 596 const { startTimeSeconds, endTimeSeconds } = eventEntry;
598
599 597 if (!Number.isFinite(startTimeSeconds) || !Number.isFinite(endTimeSeconds)) continue;
600
601 598 const duration = Math.max(0, endTimeSeconds - startTimeSeconds);
602 599 const dropThreshold = endTimeSeconds + duration + IMAGE_SUBTITLE_PRUNE_GRACE_SECONDS;
603
604 if (slowestViewerTimeSeconds >= dropThreshold)
605 {
600 if (slowestViewerTimeSeconds >= dropThreshold) {
606 601 keysToDelete.push(key);
607 602 }
608 603 }
609
610 for (const key of keysToDelete)
611 {
604 for (const key of keysToDelete) {
612 605 deleteCachedValue(frameCache, key);
613 606 }
607
608 // Smart eventCache pruning: only prune if over cap
609 let totalBytes = eventCache.totalBytes;
610 if (totalBytes <= MAX_EVENT_CACHE_BYTES) return;
611 // For each entry, prune events that end before (slowestViewerTimeSeconds - MIN_EVENT_WINDOW_SECONDS)
612 for (const [key, entry] of eventCache.map.entries()) {
613 if (!key.startsWith(prefix)) continue;
614 const value = entry.value;
615 if (!value || !Array.isArray(value.events)) continue;
616 let events = value.events;
617 // Find first event that ends after the retention threshold
618 const minKeepTime = Math.max(0, slowestViewerTimeSeconds - MIN_EVENT_WINDOW_SECONDS);
619 let pruneIdx = 0;
620 while (pruneIdx < events.length && events[pruneIdx].endTimeSeconds < minKeepTime) {
621 pruneIdx++;
622 }
623 if (pruneIdx > 0) {
624 // Remove pruned events and update memory usage
625 const removed = events.splice(0, pruneIdx);
626 for (const ev of removed) {
627 if (ev && Buffer.isBuffer(ev.imagePng)) {
628 entry.sizeBytes -= ev.imagePng.length + 160;
629 eventCache.totalBytes -= ev.imagePng.length + 160;
630 }
631 }
632 }
633 // If all events are pruned, remove the entry
634 if (events.length === 0) {
635 deleteSupFileForKey(key);
636 deleteCachedValue(eventCache, key);
637 }
638 // Stop pruning if under cap
639 if (eventCache.totalBytes <= MAX_EVENT_CACHE_BYTES) break;
640 }
614 641 }
615 642
616 643 function clearCachesForFile (filePath)
@@ -14,7 +14,8 @@ const FRAME_CACHE_TTL_MS = toPositiveInt(process.env.IMAGE_SUBTITLE_FRAME_CACHE_
14const MAX_EVENT_CACHE_ITEMS = toPositiveInt(process.env.IMAGE_SUBTITLE_EVENT_CACHE_MAX_ITEMS, 3);
14const MAX_EVENT_CACHE_ITEMS = toPositiveInt(process.env.IMAGE_SUBTITLE_EVENT_CACHE_MAX_ITEMS, 3);
15const MAX_FRAME_CACHE_ITEMS = toPositiveInt(process.env.IMAGE_SUBTITLE_FRAME_CACHE_MAX_ITEMS, 50);
15const MAX_FRAME_CACHE_ITEMS = toPositiveInt(process.env.IMAGE_SUBTITLE_FRAME_CACHE_MAX_ITEMS, 50);
16// Removed byte size cap for event cache to prevent thrashing
16// Removed byte size cap for event cache to prevent thrashing
17const MAX_EVENT_CACHE_BYTES = Infinity;
17const MAX_EVENT_CACHE_BYTES = toPositiveInt(process.env.IMAGE_SUBTITLE_EVENT_CACHE_MAX_BYTES, 50 * 1024 * 1024);
18const MIN_EVENT_WINDOW_SECONDS = 30; // Always keep at least this much for seeking
18const MAX_FRAME_CACHE_BYTES = toPositiveInt(process.env.IMAGE_SUBTITLE_FRAME_CACHE_MAX_BYTES, 48 * 1024 * 1024);
19const MAX_FRAME_CACHE_BYTES = toPositiveInt(process.env.IMAGE_SUBTITLE_FRAME_CACHE_MAX_BYTES, 48 * 1024 * 1024);
19const IMAGE_SUBTITLE_RENDER_SCALE = toBoundedNumber(process.env.IMAGE_SUBTITLE_RENDER_SCALE, 0.75, 0.1, 1);
20const IMAGE_SUBTITLE_RENDER_SCALE = toBoundedNumber(process.env.IMAGE_SUBTITLE_RENDER_SCALE, 0.75, 0.1, 1);
20const PGS_TIMEBASE = 90000;
21const PGS_TIMEBASE = 90000;
@@ -585,32 +586,58 @@ function pruneOldImageSubtitleCaches (filePath, slowestViewerTimeSeconds)
585 const prefix = cachePrefixForFile(filePath);
586 const prefix = cachePrefixForFile(filePath);
586 if (!prefix || !Number.isFinite(slowestViewerTimeSeconds)) return;
587 if (!prefix || !Number.isFinite(slowestViewerTimeSeconds)) return;
587
588
589 // Prune frameCache as before
588 const keysToDelete = [];
590 const keysToDelete = [];
589 for (const key of frameCache.map.keys())
591 for (const key of frameCache.map.keys()) {
590 {
591 if (!key.startsWith(prefix)) continue;
592 if (!key.startsWith(prefix)) continue;
592
593 const entry = frameCache.map.get(key);
593 const entry = frameCache.map.get(key);
594 if (!entry) continue;
594 if (!entry) continue;
595
596 const eventEntry = entry.value || {};
595 const eventEntry = entry.value || {};
597 const { startTimeSeconds, endTimeSeconds } = eventEntry;
596 const { startTimeSeconds, endTimeSeconds } = eventEntry;
598
599 if (!Number.isFinite(startTimeSeconds) || !Number.isFinite(endTimeSeconds)) continue;
597 if (!Number.isFinite(startTimeSeconds) || !Number.isFinite(endTimeSeconds)) continue;
600
601 const duration = Math.max(0, endTimeSeconds - startTimeSeconds);
598 const duration = Math.max(0, endTimeSeconds - startTimeSeconds);
602 const dropThreshold = endTimeSeconds + duration + IMAGE_SUBTITLE_PRUNE_GRACE_SECONDS;
599 const dropThreshold = endTimeSeconds + duration + IMAGE_SUBTITLE_PRUNE_GRACE_SECONDS;
603
600 if (slowestViewerTimeSeconds >= dropThreshold) {
604 if (slowestViewerTimeSeconds >= dropThreshold)
605 {
606 keysToDelete.push(key);
601 keysToDelete.push(key);
607 }
602 }
608 }
603 }
609
604 for (const key of keysToDelete) {
610 for (const key of keysToDelete)
611 {
612 deleteCachedValue(frameCache, key);
605 deleteCachedValue(frameCache, key);
613 }
606 }
607
608 // Smart eventCache pruning: only prune if over cap
609 let totalBytes = eventCache.totalBytes;
610 if (totalBytes <= MAX_EVENT_CACHE_BYTES) return;
611 // For each entry, prune events that end before (slowestViewerTimeSeconds - MIN_EVENT_WINDOW_SECONDS)
612 for (const [key, entry] of eventCache.map.entries()) {
613 if (!key.startsWith(prefix)) continue;
614 const value = entry.value;
615 if (!value || !Array.isArray(value.events)) continue;
616 let events = value.events;
617 // Find first event that ends after the retention threshold
618 const minKeepTime = Math.max(0, slowestViewerTimeSeconds - MIN_EVENT_WINDOW_SECONDS);
619 let pruneIdx = 0;
620 while (pruneIdx < events.length && events[pruneIdx].endTimeSeconds < minKeepTime) {
621 pruneIdx++;
622 }
623 if (pruneIdx > 0) {
624 // Remove pruned events and update memory usage
625 const removed = events.splice(0, pruneIdx);
626 for (const ev of removed) {
627 if (ev && Buffer.isBuffer(ev.imagePng)) {
628 entry.sizeBytes -= ev.imagePng.length + 160;
629 eventCache.totalBytes -= ev.imagePng.length + 160;
630 }
631 }
632 }
633 // If all events are pruned, remove the entry
634 if (events.length === 0) {
635 deleteSupFileForKey(key);
636 deleteCachedValue(eventCache, key);
637 }
638 // Stop pruning if under cap
639 if (eventCache.totalBytes <= MAX_EVENT_CACHE_BYTES) break;
640 }
614}
641}
615
642
616function clearCachesForFile (filePath)
643function clearCachesForFile (filePath)