42 lines
1.2 KiB
Python
42 lines
1.2 KiB
Python
"""开发期剧目清单缓存。
|
|
|
|
只缓存分类和剧目清单,不缓存封面。封面仍由 cache/covers 负责。
|
|
手动刷新会删掉这份缓存并重新请求。
|
|
正式打包时 Tauri 会把 RAPTD_CATALOG_CACHE 设为 0,从而关闭这里。
|
|
"""
|
|
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
|
|
PATH = Path(__file__).resolve().parents[2] / "cache" / "catalog.json"
|
|
|
|
|
|
def enabled() -> bool:
|
|
flag = os.environ.get("RAPTD_CATALOG_CACHE", "1").strip().lower()
|
|
return flag not in ("0", "false", "no")
|
|
|
|
|
|
def read() -> list[dict] | None:
|
|
if not PATH.is_file():
|
|
return None
|
|
try:
|
|
data = json.loads(PATH.read_text(encoding="utf-8"))
|
|
except (OSError, json.JSONDecodeError):
|
|
return None
|
|
events = data.get("events") if isinstance(data, dict) else None
|
|
if not isinstance(events, list) or not events:
|
|
return None
|
|
return events
|
|
|
|
|
|
def write(events: list[dict]) -> None:
|
|
PATH.parent.mkdir(parents=True, exist_ok=True)
|
|
temporary = PATH.with_suffix(".json.tmp")
|
|
temporary.write_text(json.dumps({"events": events}, ensure_ascii=False), encoding="utf-8")
|
|
temporary.replace(PATH)
|
|
|
|
|
|
def clear() -> None:
|
|
PATH.unlink(missing_ok=True)
|