feat(redis): add subscriber for pub/sub mode

This commit is contained in:
MingxuanGame
2025-08-09 12:09:23 +00:00
parent 832a6fc95d
commit 8531e67423
3 changed files with 26 additions and 1 deletions

20
app/service/subscriber.py Normal file
View File

@@ -0,0 +1,20 @@
from __future__ import annotations
from collections.abc import Awaitable, Callable
from typing import Any
from app.dependencies.database import get_redis_pubsub
class RedisSubscriber:
def __init__(self, channel: str):
self.pubsub = get_redis_pubsub(channel)
self.handlers: dict[str, list[Callable[[str, str], Awaitable[Any]]]] = {}
async def listen(self):
async for message in self.pubsub.listen():
if message is not None and message["type"] == "message":
method = self.handlers.get(message["channel"])
if method:
for handler in method:
await handler(message["channel"], message["data"])