-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender_cache.py
More file actions
34 lines (23 loc) · 1 KB
/
Copy pathrender_cache.py
File metadata and controls
34 lines (23 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
"""Tiny in-process cache for derived render data (goal progress, set tallies).
These values are recomputed often during menu rendering but the underlying data
only changes on sync or an explicit edit — so we memoize by key and invalidate
explicitly instead of expiring by time.
Entries are namespaced by the active database path, so switching profiles (which
swaps ``config.DB_PATH``) and the per-test isolated databases never read each
other's cached values.
"""
from collections.abc import Callable
from typing import Any
_store: dict[tuple[str, str], Any] = {}
def _ns() -> str:
import config
return str(config.DB_PATH)
def cached(key: str, producer: Callable[[], Any]) -> Any:
"""Return the cached value for ``key``, computing it via ``producer()`` on a miss."""
full = (_ns(), key)
if full not in _store:
_store[full] = producer()
return _store[full]
def invalidate() -> None:
"""Drop all cached derived data — call after sync or any data mutation."""
_store.clear()