refactor
This commit is contained in:
@@ -50,14 +50,26 @@ async def load_learnstate() -> tuple[list[dict], dict[str, dict[str, set[str]]]]
|
||||
return await list_guides(), levels
|
||||
|
||||
|
||||
_content_cache: dict[str, tuple[float, dict | None]] = {}
|
||||
|
||||
|
||||
def _content_json(topic: str, fmt: str) -> dict | None:
|
||||
"""Guide-Content-JSON (kann MB groß sein), mtime-gecacht — /stats und /topics/progress
|
||||
lasen die Datei bei JEDEM Frontend-Poll neu und synchron im Event-Loop."""
|
||||
path = guide_content_path(topic, fmt)
|
||||
if not path.exists():
|
||||
return None
|
||||
try:
|
||||
return json.loads(path.read_text(encoding="utf-8"))
|
||||
except ValueError:
|
||||
mtime = path.stat().st_mtime
|
||||
except OSError:
|
||||
_content_cache.pop(str(path), None)
|
||||
return None
|
||||
cached = _content_cache.get(str(path))
|
||||
if cached is None or cached[0] != mtime:
|
||||
try:
|
||||
data = json.loads(path.read_text(encoding="utf-8"))
|
||||
except ValueError:
|
||||
data = None
|
||||
_content_cache[str(path)] = (mtime, data)
|
||||
return _content_cache[str(path)][1]
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user