-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_sync.py
More file actions
53 lines (37 loc) · 1.56 KB
/
cache_sync.py
File metadata and controls
53 lines (37 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from typing import Optional, Protocol
from pydantic import Field
from src.core.mq.message import AbstractMessage, MessagePayload
class CacheSyncPayload(MessagePayload):
"""缓存同步载荷。"""
user_id: str = Field(..., title="用户ID", description="需要同步缓存的用户标识")
config_id: Optional[str] = Field(None, title="配置ID", description="具体的配置标识")
action: str = Field("refresh", title="操作类型", description="缓存操作类型: refresh / invalidate / warmup")
model_config = {"title": "缓存同步载荷"}
class CacheSyncMessage(AbstractMessage):
"""缓存同步 MQ 消息。"""
MQ_NAME = "tolink.rag.cache_sync"
MQ_TYPE = "CACHE_SYNC"
def __init__(self, payload: CacheSyncPayload):
self._payload = payload
@classmethod
def get_mq_name(cls) -> str:
return cls.MQ_NAME
@classmethod
def get_mq_type(cls) -> str:
return cls.MQ_TYPE
def get_payload(self) -> CacheSyncPayload:
return self._payload
@classmethod
def build(
cls,
user_id: str,
action: str = "refresh",
config_id: Optional[str] = None,
) -> "CacheSyncMessage":
return cls(payload=CacheSyncPayload(user_id=user_id, action=action, config_id=config_id))
@classmethod
def parse_msg(cls, raw: str) -> CacheSyncPayload:
envelope = cls.deserialize_envelope(raw)
return CacheSyncPayload(**envelope["payload"])
class MQReceiver(Protocol):
async def on_cache_sync(self, payload: "CacheSyncPayload") -> None: ...