""" cache.py — In-memory TTL cache keyed by (repo_url, day-string). Same repo queried within 24h returns the cached result. Skips: - GitHub API call - Gemini API call (~$0.0001 saved) - GPU function invocation (saves Zero-GPU quota) - PR body generation Storage: small dict in process memory. Sufficient for Spaces traffic (small cardinality). Cleared on container restart. For production scale: swap to redis with a TTL key. """ from __future__ import annotations import time from typing import Any _TTL_SECONDS = 24 * 3600 _cache: dict[str, tuple[float, Any]] = {} def _key(repo_url: str) -> str: return repo_url.strip().lower().rstrip("/") def get(repo_url: str) -> Any | None: entry = _cache.get(_key(repo_url)) if entry is None: return None ts, value = entry if time.time() - ts > _TTL_SECONDS: _cache.pop(_key(repo_url), None) return None return value def put(repo_url: str, value: Any) -> None: _cache[_key(repo_url)] = (time.time(), value) def size() -> int: return len(_cache)