Spaces:
Sleeping
Sleeping
Download app/core/cache.py from Rolaficus/ViralVelocity-api: direct link, hf CLI and curl.
- Browser
- Download file 1.14 kB
-
https://huggingface.co/spaces/Rolaficus/ViralVelocity-api/resolve/main/app/core/cache.py
- Command line
-
hf download hf://spaces/Rolaficus/ViralVelocity-api/app/core/cache.py
-
curl -L -o cache.py https://huggingface.co/spaces/Rolaficus/ViralVelocity-api/resolve/main/app/core/cache.py
1.14 kB
| import time | |
| from functools import wraps | |
| from typing import Any, Dict, Tuple | |
| # Simple in-memory storage | |
| _memory_cache: Dict[str, Tuple[Any, float]] = {} | |
| def time_based_cache(seconds: int = 60): | |
| """ | |
| A lightweight decorator to cache function results in memory. | |
| Useful for things like 'get_model_status' or frequent tag lookups. | |
| """ | |
| def decorator(func): | |
| def wrapper(*args, **kwargs): | |
| # Create a unique key based on function name and arguments | |
| key = f"{func.__name__}:{str(args)}:{str(kwargs)}" | |
| current_time = time.time() | |
| # Check if key exists and is still valid | |
| if key in _memory_cache: | |
| result, timestamp = _memory_cache[key] | |
| if current_time - timestamp < seconds: | |
| return result | |
| # Compute and store | |
| result = func(*args, **kwargs) | |
| _memory_cache[key] = (result, current_time) | |
| return result | |
| return wrapper | |
| return decorator | |
| def clear_cache(): | |
| """Clears the internal memory cache.""" | |
| _memory_cache.clear() | |