Spaces:
Runtime error
Runtime error
from datetime import datetime, timedelta | |
from functools import wraps | |
CACHE_SIZE = 50 | |
_cache = {} | |
_cache_time = {} | |
def ttl_cache(key_name, ttl_secs=20): | |
def decorator(func): | |
async def wrapper(*args, **kwargs): | |
# Assuming the prompt is the key for caching, change as necessary | |
key = kwargs.get(key_name) | |
ttl = timedelta(seconds=ttl_secs) | |
# Check cache | |
if key in _cache: | |
if datetime.now() - _cache_time[key] > ttl: | |
# Cache has expired | |
del _cache[key] | |
del _cache_time[key] | |
else: | |
return _cache[key] | |
# Call the actual function if not in cache or expired | |
response = await func(*args, **kwargs) | |
# Cache the result | |
_cache[key] = response | |
_cache_time[key] = datetime.now() | |
return response | |
return wrapper | |
return decorator | |