ayjays132's picture
Upload 478 files
101858b verified
import hashlib
import json
from pathlib import Path
from typing import Any
class GuidanceCache:
def __init__(self, cache_dir: str | Path):
self.cache_dir = Path(cache_dir).resolve()
self.cache_dir.mkdir(parents=True, exist_ok=True)
def key_for(self, payload: dict[str, Any]) -> str:
raw = json.dumps(payload, sort_keys=True, ensure_ascii=False)
return hashlib.sha256(raw.encode("utf-8")).hexdigest()
def get(self, key: str) -> dict[str, Any] | None:
path = self.cache_dir / f"{key}.json"
if not path.exists():
return None
return json.loads(path.read_text(encoding="utf-8"))
def put(self, key: str, value: dict[str, Any]) -> Path:
path = self.cache_dir / f"{key}.json"
path.write_text(json.dumps(value, indent=2, ensure_ascii=False), encoding="utf-8")
return path