Refactor storage initialization to separate object creation from data loading

• Split __post_init__ and initialize()
• Move data loading to initialize()
• Add FastAPI lifespan integration
This commit is contained in:
yangdx
2025-03-01 03:48:19 +08:00
parent b3328542c7
commit fd76e00c6a
5 changed files with 40 additions and 21 deletions

View File

@@ -20,11 +20,14 @@ from .shared_storage import (
@final
@dataclass
class JsonKVStorage(BaseKVStorage):
async def __post_init__(self):
def __post_init__(self):
working_dir = self.global_config["working_dir"]
self._file_name = os.path.join(working_dir, f"kv_store_{self.namespace}.json")
self._storage_lock = get_storage_lock()
self._data = None
async def initialize(self):
"""Initialize storage data"""
# check need_init must before get_namespace_data
need_init = try_initialize_namespace(self.namespace)
self._data = await get_namespace_data(self.namespace)