mirror of
https://github.com/kurbezz/discord-bot.git
synced 2025-12-10 18:30:23 +01:00
Refactor stream notifier
This commit is contained in:
33
src/core/redis.py
Normal file
33
src/core/redis.py
Normal file
@@ -0,0 +1,33 @@
|
||||
import contextlib
|
||||
|
||||
from redis.asyncio import from_url
|
||||
|
||||
from core.config import config
|
||||
|
||||
|
||||
def create_redis_pool():
|
||||
return from_url(config.REDIS_URI)
|
||||
|
||||
|
||||
class RedisSessionManager:
|
||||
def __init__(self):
|
||||
self.pool = None
|
||||
|
||||
async def init(self):
|
||||
self.pool = await create_redis_pool()
|
||||
|
||||
async def close(self):
|
||||
if self.pool is not None:
|
||||
await self.pool.close()
|
||||
|
||||
@contextlib.asynccontextmanager
|
||||
async def connect(self):
|
||||
if self.pool is None:
|
||||
await self.init()
|
||||
|
||||
assert self.pool is not None
|
||||
|
||||
yield self.pool
|
||||
|
||||
|
||||
redis_manager = RedisSessionManager()
|
||||
Reference in New Issue
Block a user