add cache in ram for top list

This commit is contained in:
lelo 2026-01-26 17:39:53 +00:00
parent 9b3d594f1d
commit 6f761435f3
3 changed files with 24 additions and 1 deletions

2
app.py
View File

@ -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

View File

@ -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

View File

@ -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 {