Spaces:
Running
Running
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 Download api/cache.py from vvaibsmusic/symphony: direct link, hf CLI and curl.
- Browser
- Download file 950 Bytes
-
https://huggingface.co/spaces/vvaibsmusic/symphony/resolve/main/api/cache.py
- Command line
-
hf download hf://spaces/vvaibsmusic/symphony/api/cache.py
-
curl -L -o cache.py https://huggingface.co/spaces/vvaibsmusic/symphony/resolve/main/api/cache.py
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): | |
| 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 = {} | |