import time from datetime import datetime from redis import Redis from send_push_notifications.constants import APP_NAME, REDIS_KEY_TTL def get_key() -> str: """Compose a Redis key. Returns: str: Complete key constructed by concatenated prefix + key + arg. """ return f'{APP_NAME}/MESSAGE_IDS' def trim_redis_set(redis_client: Redis) -> None: """Trim redis set values if they are already expired""" key = get_key() # datetime rounded to hour now_datetime = datetime.now().replace(minute=0, second=0, microsecond=0) pivot_timestamp = int(now_datetime.timestamp()) - REDIS_KEY_TTL redis_client.zremrangebyscore(key, '-inf', pivot_timestamp) def is_new_message_id(message_id: str, redis_client: Redis) -> bool: """Checking message id in redis set to avoid duplicates Args: message_id: Unique message ID from SQS. redis_client: Redis client. Returns: Boolean """ key = get_key() return bool(redis_client.zadd(key, {message_id: int(time.time())}))