MCPcopy Create free account
hub / github.com/su-kaka/gcli2api / MongoDBManager

Class MongoDBManager

src/storage/mongodb_manager.py:16–1535  ·  view source on GitHub ↗

MongoDB 数据库管理器

Source from the content-addressed store, hash-verified

14
15
16class MongoDBManager:
17 """MongoDB 数据库管理器"""
18
19 # 状态字段常量
20 STATE_FIELDS = {
21 "error_codes",
22 "error_messages",
23 "disabled",
24 "last_success",
25 "user_email",
26 "model_cooldowns",
27 "preview",
28 "tier",
29 "enable_credit",
30 }
31
32 @staticmethod
33 def _escape_model_name(model_name: str) -> str:
34 """
35 转义模型名中的点号,避免 MongoDB 将其解释为嵌套结构
36
37 Args:
38 model_name: 原始模型名 (如 "gemini-2.5-flash")
39
40 Returns:
41 转义后的模型名 (如 "gemini-2-5-flash")
42 """
43 return model_name.replace(".", "-")
44
45 def __init__(self):
46 self._client: Optional[AsyncIOMotorClient] = None
47 self._db: Optional[AsyncIOMotorDatabase] = None
48 self._initialized = False
49
50 # 内存配置缓存 - 初始化时加载一次
51 self._config_cache: Dict[str, Any] = {}
52 self._config_loaded = False
53
54 # Redis 缓存(仅当 REDIS_URL 环境变量存在时启用)
55 self._redis = None
56 self._redis_enabled: bool = False
57
58 async def initialize(self) -> None:
59 """初始化 MongoDB 连接"""
60 if self._initialized:
61 return
62
63 try:
64 mongodb_uri = os.getenv("MONGODB_URI")
65 if not mongodb_uri:
66 raise ValueError("MONGODB_URI environment variable not set")
67
68 database_name = os.getenv("MONGODB_DATABASE", "gcli2api")
69
70 self._client = AsyncIOMotorClient(mongodb_uri)
71 self._db = self._client[database_name]
72
73 # 测试连接

Callers 1

initializeMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected