"""cache_service.py Simple JSON-based cache utilities for retrieval index and embeddings. """ from __future__ import annotations import json from pathlib import Path from typing import Any, Optional DEFAULT_CACHE_DIR = ".cache" def ensure_cache_dir(cache_dir: str = DEFAULT_CACHE_DIR) -> Path: p = Path(cache_dir) p.mkdir(parents=True, exist_ok=True) return p def load_json(path: Path) -> Optional[Any]: try: if path.exists(): with path.open("r", encoding="utf-8") as f: return json.load(f) except Exception: return None return None def save_json(path: Path, data: Any) -> bool: try: path.parent.mkdir(parents=True, exist_ok=True) with path.open("w", encoding="utf-8") as f: json.dump(data, f) return True except Exception: return False