symphony / api /cache.py
Vaibhav Pharmacy
Comprehensive performance overhaul: eliminate ALL correlated subqueries with CTEs, add 6 composite indexes, add TTL cache, fix N+1 patterns in viral/search/artist endpoints, remove duplicate font import, add lazy loading
689e51a
Raw History Blame Contribute Delete
950 Bytes
"""Simple TTL cache for expensive API responses."""
import time
import threading
from functools import wraps
_cache = {}
_lock = threading.Lock()
def ttl_cache(ttl_seconds=60):
"""Decorator that caches function results for ttl_seconds."""
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
key = (func.__name__, args, tuple(sorted(kwargs.items())))
now = time.time()
with _lock:
if key in _cache:
result, expires = _cache[key]
if now < expires:
return result
result = func(*args, **kwargs)
with _lock:
_cache[key] = (result, now + ttl_seconds)
return result
return wrapper
return decorator
def invalidate_cache():
"""Clear the entire cache (call after data mutations)."""
global _cache
with _lock:
_cache = {}