Spaces:
Runtime error
Runtime error
Download src/nepse_intelligence/common/cache.py from paulhemb/NepseAlphaAI: direct link, hf CLI and curl.
- Browser
- Download file 921 Bytes
-
https://huggingface.co/spaces/paulhemb/NepseAlphaAI/resolve/main/src/nepse_intelligence/common/cache.py
- Command line
-
hf download hf://spaces/paulhemb/NepseAlphaAI/src/nepse_intelligence/common/cache.py
-
curl -L -o cache.py https://huggingface.co/spaces/paulhemb/NepseAlphaAI/resolve/main/src/nepse_intelligence/common/cache.py
921 Bytes
| """Generic TTL cache decorator.""" | |
| import time | |
| from functools import wraps | |
| from typing import Any, Callable, Dict | |
| import structlog | |
| logger = structlog.get_logger() | |
| def ttl_cache(seconds: int): | |
| """ | |
| Decorator that caches the return value of a function for `seconds` seconds. | |
| The cache is keyed by the function's arguments. | |
| """ | |
| def decorator(func: Callable) -> Callable: | |
| cache: Dict[Any, Dict[str, Any]] = {} | |
| def wrapper(*args, **kwargs): | |
| key = (args, frozenset(kwargs.items())) | |
| now = time.time() | |
| if key in cache and now - cache[key]["time"] < seconds: | |
| logger.debug(f"Cache hit for {func.__name__}") | |
| return cache[key]["value"] | |
| result = func(*args, **kwargs) | |
| cache[key] = {"value": result, "time": now} | |
| return result | |
| return wrapper | |
| return decorator |