更新缓存 执行逻辑: 1. 限制消息数量(取最后 N 条) 2. 创建/更新缓存条目 3. 移动到 LRU 末尾 4. 检查容量限制,必要时淘汰最旧的 Args: workflow_id: 工作流 ID messages: 要缓存的消息列表
(self, workflow_id: str, messages: list[ChatMessage])
| 123 | return entry.messages.copy() |
| 124 | |
| 125 | def put(self, workflow_id: str, messages: list[ChatMessage]) -> None: |
| 126 | """ |
| 127 | 更新缓存 |
| 128 | |
| 129 | 执行逻辑: |
| 130 | 1. 限制消息数量(取最后 N 条) |
| 131 | 2. 创建/更新缓存条目 |
| 132 | 3. 移动到 LRU 末尾 |
| 133 | 4. 检查容量限制,必要时淘汰最旧的 |
| 134 | |
| 135 | Args: |
| 136 | workflow_id: 工作流 ID |
| 137 | messages: 要缓存的消息列表 |
| 138 | """ |
| 139 | # 限制消息数量 |
| 140 | trimmed_messages = messages[-self._max_messages :] |
| 141 | |
| 142 | # 更新或插入 |
| 143 | self._cache[workflow_id] = CacheEntry( |
| 144 | messages=trimmed_messages, last_access=datetime.utcnow() |
| 145 | ) |
| 146 | self._cache.move_to_end(workflow_id) |
| 147 | |
| 148 | # LRU 淘汰 |
| 149 | while len(self._cache) > self._max_workflows: |
| 150 | self._cache.popitem(last=False) # 移除最旧的 |
| 151 | |
| 152 | def invalidate(self, workflow_id: str) -> None: |
| 153 | """ |
no test coverage detected