From 5de3ac2210a7a8c49d1ab48150e2db75121d7c61 Mon Sep 17 00:00:00 2001 From: sergey Date: Wed, 9 Oct 2024 00:14:07 +0300 Subject: [PATCH] refactoring redis and redis manager --- .gitignore | 4 ++- config/.env-template | 1 - config/config.py | 5 ++-- docker/local_redis_file/.keep | 0 docker/only_redis-compose.yaml | 9 +++---- redis_db/crud.py | 17 +++++++----- redis_db/r_helper.py | 49 ++++++++++++++++------------------ 7 files changed, 41 insertions(+), 44 deletions(-) create mode 100644 docker/local_redis_file/.keep diff --git a/.gitignore b/.gitignore index 0555bb7..6cf1702 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,6 @@ __pycache__ .venv/ .log .idea/ -*.idea \ No newline at end of file +*.idea +docker/local_redis_file/data +docker/local_redis_file/redis.conf \ No newline at end of file diff --git a/config/.env-template b/config/.env-template index ad9fda6..e5ae175 100644 --- a/config/.env-template +++ b/config/.env-template @@ -12,5 +12,4 @@ OAA_CFG__SWAGGER__PWD=P@ssw0rd! OAA_CFG__REDIS__HOST=localhost OAA_CFG__REDIS__PORT=6379 OAA_CFG__REDIS__DB=0 -OAA_CFG__REDIS__LOGIN=admin OAA_CFG__REDIS__PWD=P@ssw0rd! \ No newline at end of file diff --git a/config/config.py b/config/config.py index dc76662..17b30bd 100644 --- a/config/config.py +++ b/config/config.py @@ -43,8 +43,7 @@ class RedisConfig(BaseModel): host: str = "localhost" port: int = 6379 db: int = 0 - login: str - pwd: str + pwd: str | None = None class SwaggerConfig(BaseModel): @@ -69,7 +68,7 @@ class Settings(BaseSettings): env_prefix="OAA_CFG__", ) run: RunConfig = RunConfig() - swagger: SwaggerConfig = SwaggerConfig() + swagger: SwaggerConfig log: LogConfig = LogConfig() prefix: PrefixConfig = PrefixConfig() token: TokenConfig diff --git a/docker/local_redis_file/.keep b/docker/local_redis_file/.keep new file mode 100644 index 0000000..e69de29 diff --git a/docker/only_redis-compose.yaml b/docker/only_redis-compose.yaml index 1bfee16..c403f65 100644 --- a/docker/only_redis-compose.yaml +++ b/docker/only_redis-compose.yaml @@ -7,9 +7,6 @@ services: ports: - "6379:6379" volumes: - - ./local_redis_file/dаta:/root/redis_db - - ./local_redis_file/redis_db.conf:/usr/local/etc/redis_db/redis_db.conf - environment: - - REDIS_PASSWORD=my-password - - REDIS_PORT=6379 - - REDIS_DATABASES=16 \ No newline at end of file + - ./local_redis_file/data:/root/redis + - ./local_redis_file/redis.conf:/usr/local/etc/redis/redis.conf + command: ["redis-server", --protected-mode yes, --port 6379,"--requirepass", P@ssw0rd!] \ No newline at end of file diff --git a/redis_db/crud.py b/redis_db/crud.py index ee7b0c3..62d0a6e 100644 --- a/redis_db/crud.py +++ b/redis_db/crud.py @@ -1,16 +1,19 @@ import logging as log - -from r_helper import RedisClient +import asyncio +from r_helper import RedisManager async def set_value(key, value): - async with RedisClient() as redis_client: - await redis_client.set_value(key, value) + async with RedisManager() as redis_connect: + await redis_connect.client.set(key, value) log.info("Set %s = %s", key, value) async def get_value(key): - async with RedisClient() as redis_client: - value = await redis_client.get_value(key) + async with RedisManager() as redis_connect: + value = await redis_connect.client.get(key) log.info("Get %s = %s", key, value) - return value + + +asyncio.run(set_value("q", "sefesf")) +asyncio.run(get_value("q")) diff --git a/redis_db/r_helper.py b/redis_db/r_helper.py index 9513207..62e816c 100644 --- a/redis_db/r_helper.py +++ b/redis_db/r_helper.py @@ -1,35 +1,32 @@ from redis.asyncio import Redis +from redis import ConnectionError from config import conf import logging as log -class RedisClient: - log.info("Connecting to redis_db. %s:%s", conf.redis.host, conf.redis.port) - connection_params = { - "host": conf.redis.host, - "port": conf.redis.port, - } - if conf.redis.login and conf.redis.pwd: - connection_params["username"] = conf.redis.login - connection_params["password"] = conf.redis.pwd - if conf.redis.db: - connection_params["db"] = conf.redis.db +class RedisManager: + def __init__(self): + self.client = None + self.connect_params = { + "host": conf.redis.host, + "port": conf.redis.port, + } + if conf.redis.pwd: + self.connect_params["password"] = conf.redis.pwd async def __aenter__(self): - self.client = Redis(**self.connection_params) - log.info("Create client") - return self.client + self.client = Redis(**self.connect_params) + + try: + await self.client.ping() # Проверяем, что соединение успешно + log.info("connected to Redis") + return self + except ConnectionError: + log.warning("failed to connect to Redis") + self.client = None + raise ConnectionError async def __aexit__(self, exc_type, exc_val, exc_tb): - log.info("Close client") - await self.client.close() - - async def set_value(self, key, value): - async with self as client: - await client.set(key, value) - log.info("Set %s = %s", key, value) - - async def get_value(self, key): - async with self as client: - log.info("Get %s", key) - return await client.get(key) + if self.client: + await self.client.close() + log.info("closed connection to Redis")