feat: refresh material names in background every two days

This commit is contained in:
ar51
2026-10-09 10:44:03 +08:00
parent cf31549415
commit 512e434a2f
11 changed files with 406 additions and 37 deletions

View File

@@ -39,9 +39,19 @@ def material_names_from_records(records: list[dict[str, Any]]) -> dict[str, str]
return dict(sorted(materials.items()))
@lru_cache(maxsize=4)
def load_material_names(path: Path = MATERIAL_NAME_CACHE_FILE) -> dict[str, str]:
"""Load once per process; a Git update takes effect after the bot restarts."""
"""Reuse memory until the local file changes; never access ERP."""
try:
stat = path.stat()
except FileNotFoundError as exc:
raise MaterialNameCacheError("本地物料名称缓存文件缺失") from exc
except OSError as exc:
raise MaterialNameCacheError("本地物料名称缓存文件不可读取") from exc
return _load_material_names(path, (stat.st_mtime_ns, stat.st_size, stat.st_ino))
@lru_cache(maxsize=4)
def _load_material_names(path: Path, version: tuple[int, int, int]) -> dict[str, str]:
try:
payload = json.loads(path.read_text(encoding="utf-8"))
except FileNotFoundError as exc:
@@ -62,3 +72,6 @@ def load_material_names(path: Path = MATERIAL_NAME_CACHE_FILE) -> dict[str, str]
if payload.get("material_count") != len(materials) or payload.get("content_sha256") != material_names_hash(materials):
raise MaterialNameCacheError("本地物料名称缓存计数或完整性校验失败")
return materials
load_material_names.cache_clear = _load_material_names.cache_clear