34 lines
797 B
Python
34 lines
797 B
Python
|
|
import time
|
||
|
|
from collections import OrderedDict
|
||
|
|
|
||
|
|
|
||
|
|
class AlexaDeduplicator:
|
||
|
|
|
||
|
|
MAX_ENTRIES = 500
|
||
|
|
TTL_SECONDS = 300
|
||
|
|
|
||
|
|
def __init__(self):
|
||
|
|
self._cache: OrderedDict[str, float] = OrderedDict()
|
||
|
|
|
||
|
|
def is_duplicate(self, request_id: str) -> bool:
|
||
|
|
if not request_id:
|
||
|
|
return False
|
||
|
|
|
||
|
|
now = time.time()
|
||
|
|
|
||
|
|
while self._cache:
|
||
|
|
oldest_key, oldest_time = next(iter(self._cache.items()))
|
||
|
|
if now - oldest_time > self.TTL_SECONDS:
|
||
|
|
self._cache.popitem(last=False)
|
||
|
|
else:
|
||
|
|
break
|
||
|
|
|
||
|
|
if request_id in self._cache:
|
||
|
|
return True
|
||
|
|
|
||
|
|
self._cache[request_id] = now
|
||
|
|
|
||
|
|
while len(self._cache) > self.MAX_ENTRIES:
|
||
|
|
self._cache.popitem(last=False)
|
||
|
|
|
||
|
|
return False
|