From 6f761435f3b0ca31b70750a4c99931a2547645a4 Mon Sep 17 00:00:00 2001 From: lelo Date: Mon, 26 Jan 2026 17:39:53 +0000 Subject: [PATCH] add cache in ram for top list --- app.py | 2 +- helperfunctions.py | 15 +++++++++++++++ static/app.css | 8 ++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/app.py b/app.py index 04d0d96..11a549e 100755 --- a/app.py +++ b/app.py @@ -620,7 +620,7 @@ def generate_breadcrumbs(subpath=None): for part in parts: path_accum = f"{path_accum}/{part}" if path_accum else part if 'toplist' in part: - part = part.replace('toplist', 'oft angehört') + part = part.replace('toplist', 'häufig angehört') breadcrumbs.append({'name': part, 'path': path_accum}) return breadcrumbs diff --git a/helperfunctions.py b/helperfunctions.py index a63dac9..4b35ff3 100644 --- a/helperfunctions.py +++ b/helperfunctions.py @@ -3,6 +3,7 @@ import re import os import sqlite3 from datetime import datetime, timedelta +import time from typing import Optional import auth @@ -12,6 +13,9 @@ CATEGORY_KEYWORDS = app_config['CATEGORY_KEYWORDS'] log_db = sqlite3.connect("access_log.db", check_same_thread=False) +_TOPLIST_CACHE_TTL = 600 +_TOPLIST_CACHE = {} + # Precompiled regex to find date-like patterns: either dotted X.Y.Z or ISO dashed YYYY-MM-DD @@ -110,6 +114,16 @@ def extract_structure_from_string(input_string): return category, titel, name def generate_top_list(category): + now_ts = time.time() + cache_key = (category, tuple(sorted(session.get('folders', {}).keys()))) + cached = _TOPLIST_CACHE.get(cache_key) + if cached and now_ts - cached["ts"] < _TOPLIST_CACHE_TTL: + return list(cached["data"]) + + # Prune expired cache entries opportunistically. + for key, value in list(_TOPLIST_CACHE.items()): + if now_ts - value["ts"] >= _TOPLIST_CACHE_TTL: + _TOPLIST_CACHE.pop(key, None) now = datetime.now() @@ -169,4 +183,5 @@ def generate_top_list(category): 'file_type': 'music' }) + _TOPLIST_CACHE[cache_key] = {"ts": now_ts, "data": list(filelist)} return filelist diff --git a/static/app.css b/static/app.css index 1656f6d..e53a3e5 100644 --- a/static/app.css +++ b/static/app.css @@ -233,6 +233,12 @@ search { flex-shrink: 0; } +.admin-nav .dropdown { + display: flex; + align-items: center; + flex-shrink: 0; +} + .admin-nav a:hover { color: var(--brand-sky); border-color: var(--border-color); @@ -247,6 +253,8 @@ search { .admin-nav .dropdown-toggle { color: var(--brand-ink); padding: 8px 12px; + display: inline-flex; + align-items: center; } .admin-nav .dropdown-toggle::after {