CoolFace
Apppublic

blizzarman/polyglot-tutor

sourceHugging Faceupdated 3mo agoView on Hugging Face
0likes
cache.py37 linesDownload Raw Back to services
1"""Content-addressed file cache for LLM products.2 3Keys are sha256 hashes of (prompt version, model, inputs...), so a cache entry4is invalidated exactly when one of those changes — bump PROMPT_VERSION in the5calling service to retire stale generations. Values are small JSON documents.6On the Space the directory is ephemeral (rebuilt on restart), which is fine:7the cache is a quota saver, not a store of record (that's M3 / Supabase).8"""9 10import hashlib11import json12from pathlib import Path13 14 15class FileCache:16    def __init__(self, root: Path | str) -> None:17        self.root = Path(root)18        self.root.mkdir(parents=True, exist_ok=True)19 20    @staticmethod21    def key(*parts: str) -> str:22        return hashlib.sha256("\x1f".join(parts).encode("utf-8")).hexdigest()23 24    def get(self, key: str) -> dict | None:25        path = self.root / f"{key}.json"26        if not path.exists():27            return None28        try:29            return json.loads(path.read_text(encoding="utf-8"))30        except (OSError, json.JSONDecodeError):31            return None  # a corrupt entry behaves like a miss32 33    def set(self, key: str, value: dict) -> None:34        (self.root / f"{key}.json").write_text(35            json.dumps(value, ensure_ascii=False), encoding="utf-8"36        )37 
blizzarman/polyglot-tutor · CoolFace