Prune 清理低价值 Memory
(ctx context.Context, criteria PruneCriteria)
| 224 | |
| 225 | // Prune 清理低价值 Memory |
| 226 | func (s *InMemoryStore) Prune(ctx context.Context, criteria PruneCriteria) (int, error) { |
| 227 | s.mu.Lock() |
| 228 | defer s.mu.Unlock() |
| 229 | |
| 230 | if s.closed { |
| 231 | return 0, ErrStoreClosed |
| 232 | } |
| 233 | |
| 234 | var toDelete []string |
| 235 | now := time.Now() |
| 236 | |
| 237 | for storeKey, memory := range s.memories { |
| 238 | shouldPrune := false |
| 239 | |
| 240 | // 置信度过低 |
| 241 | if memory.Provenance != nil && memory.Provenance.Confidence < criteria.MinConfidence { |
| 242 | shouldPrune = true |
| 243 | } |
| 244 | |
| 245 | // 太久未访问 |
| 246 | if criteria.SinceLastAccess > 0 && now.Sub(memory.LastAccessed) > criteria.SinceLastAccess { |
| 247 | shouldPrune = true |
| 248 | } |
| 249 | |
| 250 | // 访问次数过少且年龄过大 |
| 251 | if criteria.MinAccessCount > 0 && criteria.MaxAge > 0 { |
| 252 | if memory.AccessCount < criteria.MinAccessCount && now.Sub(memory.CreatedAt) > criteria.MaxAge { |
| 253 | shouldPrune = true |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | if shouldPrune { |
| 258 | toDelete = append(toDelete, storeKey) |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | for _, key := range toDelete { |
| 263 | delete(s.memories, key) |
| 264 | } |
| 265 | |
| 266 | return len(toDelete), nil |
| 267 | } |
| 268 | |
| 269 | // Close 关闭存储 |
| 270 | func (s *InMemoryStore) Close() error { |