200 lines
4.4 KiB
Python
200 lines
4.4 KiB
Python
#!/usr/bin/env python3
|
||
"""
|
||
存储接口基类
|
||
"""
|
||
|
||
from abc import ABC, abstractmethod
|
||
from typing import Any, List, Dict, Optional
|
||
from dataclasses import dataclass
|
||
from enum import Enum
|
||
|
||
class StorageType(Enum):
|
||
"""存储类型"""
|
||
JSON = "json"
|
||
DATABASE = "database"
|
||
MONGODB = "mongodb"
|
||
REDIS = "redis"
|
||
|
||
@dataclass
|
||
class StorageConfig:
|
||
"""存储配置"""
|
||
storage_type: StorageType
|
||
connection_string: Optional[str] = None
|
||
base_path: Optional[str] = None
|
||
database_name: Optional[str] = None
|
||
table_prefix: Optional[str] = None
|
||
|
||
# JSON存储配置
|
||
json_indent: int = 2
|
||
json_ensure_ascii: bool = False
|
||
|
||
# 数据库配置
|
||
pool_size: int = 10
|
||
max_overflow: int = 20
|
||
|
||
# MongoDB配置
|
||
collection_prefix: str = ""
|
||
|
||
# Redis配置
|
||
redis_db: int = 0
|
||
redis_expire: Optional[int] = None
|
||
|
||
class StorageInterface(ABC):
|
||
"""存储接口基类"""
|
||
|
||
def __init__(self, config: StorageConfig):
|
||
self.config = config
|
||
|
||
@abstractmethod
|
||
def save(self, collection: str, key: str, data: Any) -> bool:
|
||
"""保存数据
|
||
|
||
Args:
|
||
collection: 集合/表名
|
||
key: 数据键
|
||
data: 要保存的数据
|
||
|
||
Returns:
|
||
bool: 保存是否成功
|
||
"""
|
||
pass
|
||
|
||
@abstractmethod
|
||
def load(self, collection: str, key: str) -> Any:
|
||
"""加载数据
|
||
|
||
Args:
|
||
collection: 集合/表名
|
||
key: 数据键
|
||
|
||
Returns:
|
||
Any: 加载的数据,不存在时返回None
|
||
"""
|
||
pass
|
||
|
||
@abstractmethod
|
||
def delete(self, collection: str, key: str) -> bool:
|
||
"""删除数据
|
||
|
||
Args:
|
||
collection: 集合/表名
|
||
key: 数据键
|
||
|
||
Returns:
|
||
bool: 删除是否成功
|
||
"""
|
||
pass
|
||
|
||
@abstractmethod
|
||
def exists(self, collection: str, key: str) -> bool:
|
||
"""检查数据是否存在
|
||
|
||
Args:
|
||
collection: 集合/表名
|
||
key: 数据键
|
||
|
||
Returns:
|
||
bool: 数据是否存在
|
||
"""
|
||
pass
|
||
|
||
@abstractmethod
|
||
def list_keys(self, collection: str, pattern: str = "*") -> List[str]:
|
||
"""列出所有键
|
||
|
||
Args:
|
||
collection: 集合/表名
|
||
pattern: 键的模式匹配
|
||
|
||
Returns:
|
||
List[str]: 键列表
|
||
"""
|
||
pass
|
||
|
||
@abstractmethod
|
||
def save_batch(self, collection: str, data: Dict[str, Any]) -> bool:
|
||
"""批量保存数据
|
||
|
||
Args:
|
||
collection: 集合/表名
|
||
data: 键值对数据
|
||
|
||
Returns:
|
||
bool: 保存是否成功
|
||
"""
|
||
pass
|
||
|
||
@abstractmethod
|
||
def load_batch(self, collection: str, keys: List[str]) -> Dict[str, Any]:
|
||
"""批量加载数据
|
||
|
||
Args:
|
||
collection: 集合/表名
|
||
keys: 键列表
|
||
|
||
Returns:
|
||
Dict[str, Any]: 键值对数据
|
||
"""
|
||
pass
|
||
|
||
@abstractmethod
|
||
def clear_collection(self, collection: str) -> bool:
|
||
"""清空集合
|
||
|
||
Args:
|
||
collection: 集合/表名
|
||
|
||
Returns:
|
||
bool: 清空是否成功
|
||
"""
|
||
pass
|
||
|
||
@abstractmethod
|
||
def get_collections(self) -> List[str]:
|
||
"""获取所有集合名称
|
||
|
||
Returns:
|
||
List[str]: 集合名称列表
|
||
"""
|
||
pass
|
||
|
||
@abstractmethod
|
||
def get_stats(self, collection: str) -> Dict[str, Any]:
|
||
"""获取集合统计信息
|
||
|
||
Args:
|
||
collection: 集合/表名
|
||
|
||
Returns:
|
||
Dict[str, Any]: 统计信息
|
||
"""
|
||
pass
|
||
|
||
def close(self):
|
||
"""关闭存储连接"""
|
||
pass
|
||
|
||
def __enter__(self):
|
||
"""上下文管理器入口"""
|
||
return self
|
||
|
||
def __exit__(self, exc_type, exc_val, exc_tb):
|
||
"""上下文管理器出口"""
|
||
self.close()
|
||
|
||
class StorageException(Exception):
|
||
"""存储异常基类"""
|
||
pass
|
||
|
||
class StorageConnectionError(StorageException):
|
||
"""存储连接错误"""
|
||
pass
|
||
|
||
class StorageOperationError(StorageException):
|
||
"""存储操作错误"""
|
||
pass
|
||
|
||
class StorageNotFoundError(StorageException):
|
||
"""存储数据未找到错误"""
|
||
pass
|