perf(napcat): 为 API 调用添加缓存并优化异步任务

- 为 NapCat 适配器中的 `get_group_info`, `get_member_info` 和 `get_self_info` 函数实现了一个简单的内存缓存(5分钟过期)。此举旨在减少对后端服务的重复 API 请求,从而提升性能并降低被限速的风险。

- 将 `ContextManager` 中对 `start_stream_loop` 的调用修改为 `asyncio.create_task`,使其成为一个非阻塞操作,避免在添加消息时因等待循环启动而造成延迟。
This commit is contained in:
tt-P607
2025-10-01 22:57:59 +08:00
parent 70c056b4fa
commit 91cfc3b8a2
2 changed files with 65 additions and 14 deletions

View File

@@ -59,7 +59,7 @@ class SingleStreamContextManager:
self.total_messages += 1
self.last_access_time = time.time()
# 启动流的循环任务(如果还未启动)
await stream_loop_manager.start_stream_loop(self.stream_id)
asyncio.create_task(stream_loop_manager.start_stream_loop(self.stream_id))
logger.debug(f"添加消息{message.processed_plain_text}到单流上下文: {self.stream_id}")
return True
except Exception as e:
@@ -275,7 +275,7 @@ class SingleStreamContextManager:
self.last_access_time = time.time()
# 启动流的循环任务(如果还未启动)
await stream_loop_manager.start_stream_loop(self.stream_id)
asyncio.create_task(stream_loop_manager.start_stream_loop(self.stream_id))
logger.debug(f"添加消息到单流上下文(异步): {self.stream_id} (兴趣度待计算)")
return True