39 lines
1.1 KiB
Python
39 lines
1.1 KiB
Python
from __future__ import annotations
|
|
|
|
import time
|
|
from collections import OrderedDict
|
|
|
|
|
|
class GoogleChatDedupeStore:
|
|
def __init__(self, ttl_seconds: int = 300, max_entries: int = 10000):
|
|
self._store: OrderedDict[str, float] = OrderedDict()
|
|
self._ttl_seconds = ttl_seconds
|
|
self._max_entries = max_entries
|
|
|
|
def is_duplicate(self, key: str) -> bool:
|
|
self._evict_expired()
|
|
return key in self._store
|
|
|
|
def mark_seen(self, key: str) -> None:
|
|
self._evict_expired()
|
|
self._store[key] = time.monotonic()
|
|
if len(self._store) > self._max_entries:
|
|
self._store.popitem(last=False)
|
|
|
|
def reset(self) -> None:
|
|
self._store.clear()
|
|
|
|
@property
|
|
def ttl_seconds(self) -> int:
|
|
return self._ttl_seconds
|
|
|
|
@property
|
|
def max_entries(self) -> int:
|
|
return self._max_entries
|
|
|
|
def _evict_expired(self) -> None:
|
|
now = time.monotonic()
|
|
cutoff = now - self._ttl_seconds
|
|
stale = [k for k, v in self._store.items() if v < cutoff]
|
|
for k in stale:
|
|
del self._store[k] |