replyflow-rag / core /cache.py
Qurrah's picture
REFINE: rename inventory_cache and query_cache to instant_reply_cache and product_result_cache for clarity
af700e4
Raw History Blame Contribute Delete
1.98 kB
import time
from collections import defaultdict
from collections.abc import Callable
from threading import Lock
from typing import Any
class TTLCache:
def __init__(self, ttl: int = 300):
self._store: dict[str, tuple[Any, float]] = {}
self._ttl = ttl
self._lock = Lock()
def get(self, key: str):
with self._lock:
entry = self._store.get(key)
if not entry:
return None
value, expires_at = entry
if time.time() > expires_at:
del self._store[key]
return None
return value
def set(self, key: str, value):
with self._lock:
self._store[key] = (value, time.time() + self._ttl)
def delete(self, key: str):
with self._lock:
self._store.pop(key, None)
def clear(self) -> None:
with self._lock:
self._store.clear()
def delete_matching(self, predicate: Callable[[str], bool]) -> int:
with self._lock:
keys_to_delete = [key for key in self._store if predicate(key)]
for key in keys_to_delete:
self._store.pop(key, None)
return len(keys_to_delete)
class RateLimiter:
def __init__(self, max_calls: int = 20, period: int = 60):
self._calls = defaultdict(list)
self._max_calls = max_calls
self._period = period
self._lock = Lock()
def is_allowed(self, key: str) -> bool:
with self._lock:
now = time.time()
window = now - self._period
self._calls[key] = [t for t in self._calls[key] if t > window]
if len(self._calls[key]) >= self._max_calls:
return False
self._calls[key].append(now)
return True
instant_reply_cache = TTLCache(ttl=300)
product_result_cache = TTLCache(ttl=600)
credentials_cache = TTLCache(ttl=600)
rate_limiter = RateLimiter(max_calls=20, period=60)