From 2ef7828fb0971fd366668d4db06c315728685b98 Mon Sep 17 00:00:00 2001 From: sergey Date: Tue, 8 Oct 2024 22:29:50 +0300 Subject: [PATCH] fix reddis to redis --- config/.env-template | 10 ++++----- config/config.py | 4 ++-- docker/only_redis-compose.yaml | 15 ++++++++++++++ reddis/r_helper.py | 25 ----------------------- {reddis => redis_db}/__init__.py | 0 {reddis => redis_db}/crud.py | 0 redis_db/r_helper.py | 35 ++++++++++++++++++++++++++++++++ 7 files changed, 57 insertions(+), 32 deletions(-) create mode 100644 docker/only_redis-compose.yaml delete mode 100644 reddis/r_helper.py rename {reddis => redis_db}/__init__.py (100%) rename {reddis => redis_db}/crud.py (100%) create mode 100644 redis_db/r_helper.py diff --git a/config/.env-template b/config/.env-template index a9b286d..66d4a08 100644 --- a/config/.env-template +++ b/config/.env-template @@ -9,8 +9,8 @@ OAA_CFG__TOKEN__USER=A3mojtenQqYwJAgrSiiFaVHX6fRoVW15 OAA_CFG__ADMIN_USER__LOGIN=admin OAA_CFG__ADMIN_USER__PWD=P@ssw0rd! -OAA_CFG__REDDIS__HOST=localhost -OAA_CFG__REDDIS__PORT=6379 -OAA_CFG__REDDIS__DB=0 -OAA_CFG__REDDIS__LOGIN=admin -OAA_CFG__REDDIS__PWD=P@ssw0rd! \ No newline at end of file +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 53f8c5c..c8557c2 100644 --- a/config/config.py +++ b/config/config.py @@ -44,7 +44,7 @@ class AdminUser(BaseModel): pwd: str -class ReddisConfig(BaseModel): +class RedisConfig(BaseModel): host: str = "localhost" port: int = 6379 db: int = 0 @@ -77,7 +77,7 @@ class Settings(BaseSettings): prefix: PrefixConfig = PrefixConfig() token: TokenConfig admin_user: AdminUser - reddis: ReddisConfig + redis: RedisConfig conf = Settings() diff --git a/docker/only_redis-compose.yaml b/docker/only_redis-compose.yaml new file mode 100644 index 0000000..1bfee16 --- /dev/null +++ b/docker/only_redis-compose.yaml @@ -0,0 +1,15 @@ +version: '3.3' + +services: + redis: + image: redis:latest + restart: always + 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 diff --git a/reddis/r_helper.py b/reddis/r_helper.py deleted file mode 100644 index b08cf19..0000000 --- a/reddis/r_helper.py +++ /dev/null @@ -1,25 +0,0 @@ -from redis.asyncio import Redis -from config import conf - - -class RedisClient: - async def __aenter__(self): - self.client = Redis( - host=conf.reddis.host, - port=conf.reddis.port, - db=conf.reddis.db, - username=conf.reddis.login, - password=conf.reddis.pwd, - ) - return self.client - - async def __aexit__(self, exc_type, exc_val, exc_tb): - await self.client.close() - - async def set_value(self, key, value): - async with self as client: - await client.set(key, value) - - async def get_value(self, key): - async with self as client: - return await client.get(key) diff --git a/reddis/__init__.py b/redis_db/__init__.py similarity index 100% rename from reddis/__init__.py rename to redis_db/__init__.py diff --git a/reddis/crud.py b/redis_db/crud.py similarity index 100% rename from reddis/crud.py rename to redis_db/crud.py diff --git a/redis_db/r_helper.py b/redis_db/r_helper.py new file mode 100644 index 0000000..9513207 --- /dev/null +++ b/redis_db/r_helper.py @@ -0,0 +1,35 @@ +from redis.asyncio import Redis +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 + + async def __aenter__(self): + self.client = Redis(**self.connection_params) + log.info("Create client") + return self.client + + 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)