Files
ZNJJ-api-server/src/services/repository.py
T

41 lines
1.2 KiB
Python
Raw Normal View History

"""异步业务字段存储接口;字段值保留 bool/int/str 的区别。"""
from typing import Protocol
class CheckpointNotFoundError(Exception):
"""存储适配器明确确认线程不存在。"""
class SessionRepository(Protocol):
async def load(self, session_id: str) -> dict:
"""读取业务字段;不存在返回空 dict。"""
...
async def apply_update(self, session_id: str, patch: dict) -> dict:
"""提交差量后返回完整业务字段。"""
...
async def delete(self, session_id: str) -> None:
"""幂等删除;真实存储错误必须传播。"""
...
class InMemorySessionRepository:
def __init__(self):
self._store: dict[str, dict] = {}
async def load(self, session_id: str) -> dict:
return dict(self._store.get(session_id) or {})
async def apply_update(self, session_id: str, patch: dict) -> dict:
state = self._store.setdefault(session_id, {})
state.update(patch)
return dict(state)
async def delete(self, session_id: str) -> None:
self._store.pop(session_id, None)
__all__ = ["CheckpointNotFoundError", "SessionRepository", "InMemorySessionRepository"]