Get 获取知识项
(ctx context.Context, id string)
| 292 | |
| 293 | // Get 获取知识项 |
| 294 | func (m *manager) Get(ctx context.Context, id string) (*KnowledgeItem, error) { |
| 295 | m.mu.RLock() |
| 296 | defer m.mu.RUnlock() |
| 297 | |
| 298 | // 先检查缓存 |
| 299 | if m.config.CacheEnabled { |
| 300 | if entry, exists := m.cache[id]; exists { |
| 301 | if time.Now().Before(entry.expire) { |
| 302 | return entry.item, nil |
| 303 | } |
| 304 | delete(m.cache, id) |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | // 从内存获取 |
| 309 | item, err := m.getItemFromMemory(ctx, id) |
| 310 | if err != nil { |
| 311 | return nil, fmt.Errorf("knowledge: failed to get item: %w", err) |
| 312 | } |
| 313 | |
| 314 | // 更新缓存 |
| 315 | if m.config.CacheEnabled { |
| 316 | m.updateCache(item) |
| 317 | } |
| 318 | |
| 319 | // 记录审计 |
| 320 | if m.config.EnableAudit { |
| 321 | m.audit("get", "", item.ID, "Retrieved knowledge item: "+item.Title) |
| 322 | } |
| 323 | |
| 324 | return item, nil |
| 325 | } |
| 326 | |
| 327 | // Update 更新知识项 |
| 328 | func (m *manager) Update(ctx context.Context, item *KnowledgeItem) error { |
no test coverage detected