Spaces:
Paused
Paused
File size: 1,065 Bytes
f4145b9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | """
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)
|