1
0
Эх сурвалжийг харах

further optimize page load

tuanchris 4 сар өмнө
parent
commit
fdd83a71e1

+ 11 - 3
modules/core/pattern_manager.py

@@ -289,14 +289,22 @@ async def cleanup_pattern_manager():
 
 def list_theta_rho_files():
     files = []
-    for root, _, filenames in os.walk(THETA_RHO_DIR):
-        for file in filenames:
+    for root, dirs, filenames in os.walk(THETA_RHO_DIR):
+        # Skip cached_images directories to avoid scanning thousands of WebP files
+        if 'cached_images' in dirs:
+            dirs.remove('cached_images')
+
+        # Filter .thr files during traversal for better performance
+        thr_files = [f for f in filenames if f.endswith('.thr')]
+
+        for file in thr_files:
             relative_path = os.path.relpath(os.path.join(root, file), THETA_RHO_DIR)
             # Normalize path separators to always use forward slashes for consistency across platforms
             relative_path = relative_path.replace(os.sep, '/')
             files.append(relative_path)
+
     logger.debug(f"Found {len(files)} theta-rho files")
-    return [file for file in files if file.endswith('.thr')]
+    return files
 
 def parse_theta_rho_file(file_path):
     """Parse a theta-rho file and return a list of (theta, rho) pairs."""

+ 49 - 14
static/js/base.js

@@ -1,41 +1,76 @@
 // Player status bar functionality - Updated to fix logMessage errors
 
-// Pattern files cache for improved performance
-let patternFilesCache = null;
-let patternFilesCacheTimestamp = null;
-const PATTERN_CACHE_EXPIRY = 30000; // 30 seconds cache
+// Pattern files cache for improved performance with localStorage persistence
+const PATTERN_CACHE_KEY = 'dune_weaver_pattern_files_cache';
+const PATTERN_CACHE_EXPIRY = 30 * 60 * 1000; // 30 minutes cache (longer since it persists)
 
 // Function to get cached pattern files or fetch fresh data
 async function getCachedPatternFiles(forceRefresh = false) {
     const now = Date.now();
 
-    // Check if cache is valid and not forced to refresh
-    if (!forceRefresh && patternFilesCache && patternFilesCacheTimestamp &&
-        (now - patternFilesCacheTimestamp) < PATTERN_CACHE_EXPIRY) {
-        return patternFilesCache;
+    // Try to load from localStorage first
+    if (!forceRefresh) {
+        try {
+            const cachedData = localStorage.getItem(PATTERN_CACHE_KEY);
+            if (cachedData) {
+                const { files, timestamp } = JSON.parse(cachedData);
+                if (files && timestamp && (now - timestamp) < PATTERN_CACHE_EXPIRY) {
+                    console.log('Using cached pattern files from localStorage');
+                    return files;
+                }
+            }
+        } catch (error) {
+            console.warn('Error reading pattern files cache from localStorage:', error);
+        }
     }
 
     try {
+        console.log('Fetching fresh pattern files from server');
         const response = await fetch('/list_theta_rho_files');
         if (!response.ok) {
             throw new Error(`Failed to fetch pattern files: ${response.status}`);
         }
 
         const files = await response.json();
-        patternFilesCache = files;
-        patternFilesCacheTimestamp = now;
+
+        // Store in localStorage
+        try {
+            const cacheData = { files, timestamp: now };
+            localStorage.setItem(PATTERN_CACHE_KEY, JSON.stringify(cacheData));
+        } catch (error) {
+            console.warn('Error storing pattern files cache in localStorage:', error);
+        }
+
         return files;
     } catch (error) {
         console.error('Error fetching pattern files:', error);
-        // Return cached data if available, even if expired
-        return patternFilesCache || [];
+
+        // Try to return any cached data as fallback, even if expired
+        try {
+            const cachedData = localStorage.getItem(PATTERN_CACHE_KEY);
+            if (cachedData) {
+                const { files } = JSON.parse(cachedData);
+                if (files) {
+                    console.log('Using expired cached pattern files as fallback');
+                    return files;
+                }
+            }
+        } catch (fallbackError) {
+            console.warn('Error reading fallback cache:', fallbackError);
+        }
+
+        return [];
     }
 }
 
 // Function to invalidate pattern files cache
 function invalidatePatternFilesCache() {
-    patternFilesCache = null;
-    patternFilesCacheTimestamp = null;
+    try {
+        localStorage.removeItem(PATTERN_CACHE_KEY);
+        console.log('Pattern files cache invalidated');
+    } catch (error) {
+        console.warn('Error invalidating pattern files cache:', error);
+    }
 }
 
 // Helper function to normalize file paths for cross-platform compatibility