Save 保存记忆
(ctx context.Context, memory *Memory)
| 48 | |
| 49 | // Save 保存记忆 |
| 50 | func (s *InMemoryStore) Save(ctx context.Context, memory *Memory) error { |
| 51 | s.mu.Lock() |
| 52 | defer s.mu.Unlock() |
| 53 | |
| 54 | // 检查容量 |
| 55 | if len(s.memories) >= s.config.MaxMemories { |
| 56 | // 删除最旧的记忆 |
| 57 | s.removeOldest() |
| 58 | } |
| 59 | |
| 60 | // 保存 |
| 61 | s.memories[memory.ID] = memory |
| 62 | |
| 63 | // 更新项目索引 |
| 64 | if memory.ProjectID != "" { |
| 65 | s.projectIndex[memory.ProjectID] = append(s.projectIndex[memory.ProjectID], memory.ID) |
| 66 | } |
| 67 | |
| 68 | // 更新标签索引 |
| 69 | for _, tag := range memory.Tags { |
| 70 | s.tagIndex[tag] = append(s.tagIndex[tag], memory.ID) |
| 71 | } |
| 72 | |
| 73 | return nil |
| 74 | } |
| 75 | |
| 76 | // Load 加载记忆 |
| 77 | func (s *InMemoryStore) Load(ctx context.Context, id string) (*Memory, error) { |