Files
tg_post_min/app/infra/redis_client.py
Andrey K. Choi c16ec54891 Bot become a Community Guard & Post send manager
added: dictionary support for censore
message/user management with dict triggers
2025-08-22 21:44:14 +09:00

26 lines
668 B
Python

import os
try:
from redis import asyncio as aioredis
except Exception:
aioredis = None
_redis = None
async def get_redis():
"""
Возвращает подключение к Redis (async) или None, если REDIS_URL не задан
или библиотека не установлена.
"""
global _redis
if _redis is not None:
return _redis
url = os.getenv("REDIS_URL", "").strip()
if not url or aioredis is None:
return None
_redis = aioredis.from_url(url, encoding="utf-8", decode_responses=True)
try:
await _redis.ping()
except Exception:
_redis = None
return _redis