29 lines
834 B
Python
29 lines
834 B
Python
import time
|
|
|
|
from yuxi.channel.extensions.alipay.constants import (
|
|
ALIPAY_DEDUPE_MAX_SIZE,
|
|
ALIPAY_DEDUPE_TTL_SECONDS,
|
|
)
|
|
|
|
|
|
class AlipayMessageDeduplicator:
|
|
def __init__(self):
|
|
self._cache: dict[str, float] = {}
|
|
|
|
def is_duplicate(self, msg_id: str) -> bool:
|
|
if not msg_id:
|
|
return False
|
|
now = time.time()
|
|
if msg_id in self._cache:
|
|
if now - self._cache[msg_id] < ALIPAY_DEDUPE_TTL_SECONDS:
|
|
return True
|
|
self._cache[msg_id] = now
|
|
self._evict_expired(now)
|
|
return False
|
|
|
|
def _evict_expired(self, now: float):
|
|
if len(self._cache) > ALIPAY_DEDUPE_MAX_SIZE:
|
|
expired = [k for k, v in self._cache.items() if now - v > ALIPAY_DEDUPE_TTL_SECONDS]
|
|
for k in expired:
|
|
del self._cache[k]
|