refactoring redis and redis manager

This commit is contained in:
sergey 2024-10-09 00:14:07 +03:00
parent f80b90a1a6
commit 5de3ac2210
7 changed files with 41 additions and 44 deletions

4
.gitignore vendored
View File

@ -5,4 +5,6 @@ __pycache__
.venv/ .venv/
.log .log
.idea/ .idea/
*.idea *.idea
docker/local_redis_file/data
docker/local_redis_file/redis.conf

View File

@ -12,5 +12,4 @@ OAA_CFG__SWAGGER__PWD=P@ssw0rd!
OAA_CFG__REDIS__HOST=localhost OAA_CFG__REDIS__HOST=localhost
OAA_CFG__REDIS__PORT=6379 OAA_CFG__REDIS__PORT=6379
OAA_CFG__REDIS__DB=0 OAA_CFG__REDIS__DB=0
OAA_CFG__REDIS__LOGIN=admin
OAA_CFG__REDIS__PWD=P@ssw0rd! OAA_CFG__REDIS__PWD=P@ssw0rd!

View File

@ -43,8 +43,7 @@ class RedisConfig(BaseModel):
host: str = "localhost" host: str = "localhost"
port: int = 6379 port: int = 6379
db: int = 0 db: int = 0
login: str pwd: str | None = None
pwd: str
class SwaggerConfig(BaseModel): class SwaggerConfig(BaseModel):
@ -69,7 +68,7 @@ class Settings(BaseSettings):
env_prefix="OAA_CFG__", env_prefix="OAA_CFG__",
) )
run: RunConfig = RunConfig() run: RunConfig = RunConfig()
swagger: SwaggerConfig = SwaggerConfig() swagger: SwaggerConfig
log: LogConfig = LogConfig() log: LogConfig = LogConfig()
prefix: PrefixConfig = PrefixConfig() prefix: PrefixConfig = PrefixConfig()
token: TokenConfig token: TokenConfig

View File

View File

@ -7,9 +7,6 @@ services:
ports: ports:
- "6379:6379" - "6379:6379"
volumes: volumes:
- ./local_redis_file/dаta:/root/redis_db - ./local_redis_file/data:/root/redis
- ./local_redis_file/redis_db.conf:/usr/local/etc/redis_db/redis_db.conf - ./local_redis_file/redis.conf:/usr/local/etc/redis/redis.conf
environment: command: ["redis-server", --protected-mode yes, --port 6379,"--requirepass", P@ssw0rd!]
- REDIS_PASSWORD=my-password
- REDIS_PORT=6379
- REDIS_DATABASES=16

View File

@ -1,16 +1,19 @@
import logging as log import logging as log
import asyncio
from r_helper import RedisClient from r_helper import RedisManager
async def set_value(key, value): async def set_value(key, value):
async with RedisClient() as redis_client: async with RedisManager() as redis_connect:
await redis_client.set_value(key, value) await redis_connect.client.set(key, value)
log.info("Set %s = %s", key, value) log.info("Set %s = %s", key, value)
async def get_value(key): async def get_value(key):
async with RedisClient() as redis_client: async with RedisManager() as redis_connect:
value = await redis_client.get_value(key) value = await redis_connect.client.get(key)
log.info("Get %s = %s", key, value) log.info("Get %s = %s", key, value)
return value
asyncio.run(set_value("q", "sefesf"))
asyncio.run(get_value("q"))

View File

@ -1,35 +1,32 @@
from redis.asyncio import Redis from redis.asyncio import Redis
from redis import ConnectionError
from config import conf from config import conf
import logging as log import logging as log
class RedisClient: class RedisManager:
log.info("Connecting to redis_db. %s:%s", conf.redis.host, conf.redis.port) def __init__(self):
connection_params = { self.client = None
"host": conf.redis.host, self.connect_params = {
"port": conf.redis.port, "host": conf.redis.host,
} "port": conf.redis.port,
if conf.redis.login and conf.redis.pwd: }
connection_params["username"] = conf.redis.login if conf.redis.pwd:
connection_params["password"] = conf.redis.pwd self.connect_params["password"] = conf.redis.pwd
if conf.redis.db:
connection_params["db"] = conf.redis.db
async def __aenter__(self): async def __aenter__(self):
self.client = Redis(**self.connection_params) self.client = Redis(**self.connect_params)
log.info("Create client")
return self.client 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): async def __aexit__(self, exc_type, exc_val, exc_tb):
log.info("Close client") if self.client:
await self.client.close() await self.client.close()
log.info("closed connection to Redis")
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)