stocks / core /shared /cache.py
Arrechenash's picture
Initial Commit
5a75918
Raw
History Blame Contribute Delete
715 Bytes
"""Simple TTL-based in-memory cache for API responses."""
import time as _time
_cache_store: dict[str, tuple[float, dict]] = {}
CACHE_TTL = 300 # 5 minutes default
def cache_get(key: str) -> dict | None:
"""Get value from cache if within TTL."""
entry = _cache_store.get(key)
if entry and _time.time() - entry[0] < CACHE_TTL:
return entry[1]
if entry:
del _cache_store[key]
return None
def cache_set(key: str, data: dict) -> None:
"""Set value in cache."""
_cache_store[key] = (_time.time(), data)
def cache_key(symbol: str, prefix: str = "stock-catalysts") -> str:
"""Generate a namespaced cache key for a symbol."""
return f"{prefix}:{symbol.upper()}"