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/
.log
.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__PORT=6379
OAA_CFG__REDIS__DB=0
OAA_CFG__REDIS__LOGIN=admin
OAA_CFG__REDIS__PWD=P@ssw0rd!

View File

@ -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

View File

View File

@ -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
- ./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!]

View File

@ -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"))

View File

@ -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")