Files
jbod-monitor/services/cache.py

30 lines
709 B
Python

import time
from typing import Any
class TTLCache:
"""Simple in-memory TTL cache."""
def __init__(self, ttl_seconds: int = 60):
self._ttl = ttl_seconds
self._store: dict[str, tuple[float, Any]] = {}
def get(self, key: str) -> Any | None:
entry = self._store.get(key)
if entry is None:
return None
ts, value = entry
if time.monotonic() - ts > self._ttl:
del self._store[key]
return None
return value
def set(self, key: str, value: Any) -> None:
self._store[key] = (time.monotonic(), value)
def clear(self) -> None:
self._store.clear()
smart_cache = TTLCache(ttl_seconds=60)