import json import os from typing import Dict, Any, Optional import diskcache class Cache: def __init__(self, cache_dir: str = "./cache", size_limit_gb: int = 100): self.cache_dir = cache_dir self.size_limit_bytes = size_limit_gb * 1024 * 1024 * 1024 self.cache = diskcache.Cache( directory=cache_dir, size_limit=self.size_limit_bytes, eviction_policy='least-recently-used' ) def _make_key(self, method: str, params: Dict[str, Any]) -> str: return f"{method}:{json.dumps(params, sort_keys=True)}" def get(self, method: str, params: Dict[str, Any]) -> Optional[Dict[str, Any]]: key = self._make_key(method, params) return self.cache.get(key) def set(self, method: str, params: Dict[str, Any], response: Dict[str, Any]) -> None: key = self._make_key(method, params) self.cache.set(key, response) def size_check(self) -> Dict[str, Any]: stats = self.cache.stats() return { "size_bytes": stats[1], "size_gb": stats[1] / (1024 * 1024 * 1024), "count": stats[0], "limit_gb": self.size_limit_bytes / (1024 * 1024 * 1024) }