Spaces:
Runtime error
Runtime error
Download eugene/cache.py from Rex165/eugene-intelligence: direct link, hf CLI and curl.
- Browser
- Download file 788 Bytes
-
https://huggingface.co/spaces/Rex165/eugene-intelligence/resolve/main/eugene/cache.py
- Command line
-
hf download hf://spaces/Rex165/eugene-intelligence/eugene/cache.py
-
curl -L -o cache.py https://huggingface.co/spaces/Rex165/eugene-intelligence/resolve/main/eugene/cache.py
788 Bytes
| """In-memory TTL cache. Replace with Redis when needed.""" | |
| import time | |
| import hashlib | |
| import json | |
| from functools import wraps | |
| _CACHE = {} | |
| def cached(ttl: int = 3600): | |
| """Decorator: cache function result for `ttl` seconds.""" | |
| def decorator(fn): | |
| def wrapper(*args, **kwargs): | |
| key = f"{fn.__name__}:{hashlib.md5(json.dumps([args, kwargs], default=str).encode()).hexdigest()}" | |
| now = time.time() | |
| if key in _CACHE: | |
| val, expires = _CACHE[key] | |
| if now < expires: | |
| return val | |
| result = fn(*args, **kwargs) | |
| _CACHE[key] = (result, now + ttl) | |
| return result | |
| return wrapper | |
| return decorator | |
| def cache_clear(): | |
| _CACHE.clear() | |