Explorar o código

Add library caching with optional force-refresh

Introduce a simple cache for the movie library scan: CACHE_FILE (user:/Document/Appdata/Movie/library_cache.json) and STALE_MS (8 hours). getLibrary.js main() now reads and parses the cache and returns cached data when present and fresh, unless a POST parameter forceRefresh=1 is provided to bypass the cache. Also update the frontend refreshLibrary() to send { forceRefresh: '1' } when the manual Refresh button is used so it triggers a full rescan.
Toby Chui hai 2 semanas
pai
achega
89a7425133
Modificáronse 2 ficheiros con 23 adicións e 2 borrados
  1. 21 0
      src/web/Movie/backend/getLibrary.js
  2. 2 2
      src/web/Movie/index.html

+ 21 - 0
src/web/Movie/backend/getLibrary.js

@@ -332,7 +332,28 @@ function scanRoot(rootPath) {
 
 // ── Entry point ───────────────────────────────────────────────────────────────
 
+var CACHE_FILE = "user:/Document/Appdata/Movie/library_cache.json";
+var STALE_MS   = 8 * 60 * 60 * 1000;  // 8 hours
+
 function main() {
+    // ── Serve from cache if it is still fresh and no force-refresh was requested ──
+    // POST param forceRefresh=1 bypasses this check (sent by the manual Refresh button).
+    var forced = (typeof forceRefresh !== "undefined" && forceRefresh === "1");
+
+    if (!forced && filelib.fileExists(CACHE_FILE)) {
+        var raw = filelib.readFile(CACHE_FILE);
+        if (raw && raw !== false && raw.length > 10) {
+            try {
+                var cached = JSON.parse(raw);
+                if (cached && cached.ts && Array.isArray(cached.data) &&
+                    (new Date().getTime() - cached.ts) < STALE_MS) {
+                    sendJSONResp(JSON.stringify(cached.data));
+                    return;  // skip the full scan — cache is still fresh
+                }
+            } catch (e) {}
+        }
+    }
+
     var allAlbums = [];
     var roots     = filelib.glob("/");
     if (!roots) { roots = []; }

+ 2 - 2
src/web/Movie/index.html

@@ -1742,11 +1742,11 @@ function loadLibrary() {
 }
 
 // ─── Manual refresh (Refresh button) ──────────────────────────────────────────
-// Triggers a full scan; getLibrary.js saves the cache file automatically.
+// Passes forceRefresh=1 to bypass the 8-hour staleness guard in getLibrary.js.
 function refreshLibrary() {
     $('#library-refresh-btn').prop('disabled', true);
     setLibraryStatus('Refreshing…', true);
-    ao_module_agirun(SCRIPT_GET_LIBRARY, {}, function (data) {
+    ao_module_agirun(SCRIPT_GET_LIBRARY, { forceRefresh: '1' }, function (data) {
         $('#library-refresh-btn').prop('disabled', false);
         if (!data || data.error) {
             showToast('Failed to refresh library');