import json import redis.asyncio as redis class RedisClient: SET_NAME = "queue" TASKS_NAME = "tasks_working" TASKS_DONE_NAME = "tasks_done" def __init__(self): self.connection = redis.Redis(host="localhost", port=6379, db=0) async def _set_task(self, task: dict) -> int: async with self.connection as connection: res = await connection.set(f'{self.TASKS_NAME}:{task["link"]}', json.dumps(task, indent=4).encode('utf-8')) return res async def _set_task_done(self, task: dict) -> int: async with self.connection as connection: res = await connection.set( f'{self.TASKS_DONE_NAME}:1:{task["link"]}', json.dumps(task, indent=4).encode('utf-8') ) return res async def _del_task(self, task: dict) -> int: async with self.connection as connection: res = await connection.delete(f'{self.TASKS_NAME}:{task["link"]}') return res async def set_task_to_queue(self, task: dict) -> int: async with self.connection as connection: res = await connection.sadd(self.SET_NAME, json.dumps(task, indent=4).encode('utf-8')) return res async def get_queue(self) -> set: async with self.connection as connection: res = await connection.smembers(self.SET_NAME) return res async def del_task_from_queue(self, task: dict) -> int: async with self.connection as connection: res = await connection.srem(self.SET_NAME, json.dumps(task, indent=4).encode('utf-8')) return res async def del_task_from_queue_and_add_to_tasks(self, task: dict) -> int: await self.del_task_from_queue(task) return await self._set_task(task) async def del_task_from_tasks_and_add_to_task_done(self, task: dict) -> int: await self._del_task(task) return await self._set_task_done(task)