Prune 清理低价值 Memory
(ctx context.Context, criteria PruneCriteria)
| 414 | |
| 415 | // Prune 清理低价值 Memory |
| 416 | func (s *MySQLStore) Prune(ctx context.Context, criteria PruneCriteria) (int, error) { |
| 417 | if s.closed { |
| 418 | return 0, ErrStoreClosed |
| 419 | } |
| 420 | |
| 421 | // 构建条件 |
| 422 | conditions := []string{} |
| 423 | args := []any{} |
| 424 | |
| 425 | if criteria.MinConfidence > 0 { |
| 426 | conditions = append(conditions, "confidence < ?") |
| 427 | args = append(args, criteria.MinConfidence) |
| 428 | } |
| 429 | |
| 430 | if criteria.SinceLastAccess > 0 { |
| 431 | conditions = append(conditions, "last_accessed < ?") |
| 432 | args = append(args, time.Now().Add(-criteria.SinceLastAccess)) |
| 433 | } |
| 434 | |
| 435 | if criteria.MinAccessCount > 0 && criteria.MaxAge > 0 { |
| 436 | conditions = append(conditions, "(access_count < ? AND created_at < ?)") |
| 437 | args = append(args, criteria.MinAccessCount, time.Now().Add(-criteria.MaxAge)) |
| 438 | } |
| 439 | |
| 440 | if len(conditions) == 0 { |
| 441 | return 0, nil |
| 442 | } |
| 443 | |
| 444 | query := fmt.Sprintf("DELETE FROM %s WHERE %s", s.tableName, conditions[0]) |
| 445 | var querySb443 strings.Builder |
| 446 | for i := 1; i < len(conditions); i++ { |
| 447 | querySb443.WriteString(" OR " + conditions[i]) |
| 448 | } |
| 449 | query += querySb443.String() |
| 450 | |
| 451 | result, err := s.db.ExecContext(ctx, query, args...) |
| 452 | if err != nil { |
| 453 | return 0, NewStoreError("DELETE_ERROR", "failed to prune memories", err) |
| 454 | } |
| 455 | |
| 456 | rowsAffected, _ := result.RowsAffected() |
| 457 | return int(rowsAffected), nil |
| 458 | } |
| 459 | |
| 460 | // Close 关闭存储 |
| 461 | func (s *MySQLStore) Close() error { |
nothing calls this directly
no test coverage detected