ls-engine / app /services /cache.py
Godswill-IoT's picture
Upload 69 files
9b3014a verified
Raw
History Blame Contribute Delete
585 Bytes
import time
from typing import Any, Dict, Tuple
class TTLCache:
def __init__(self, ttl_seconds: int = 600):
self.ttl = ttl_seconds
self._store: Dict[str, Tuple[float, Any]] = {}
def get(self, key: str):
item = self._store.get(key)
if not item:
return None
expires_at, value = item
if time.time() > expires_at:
self._store.pop(key, None)
return None
return value
def set(self, key: str, value: Any):
self._store[key] = (time.time() + self.ttl, value)