// 织忆 MemoryWeave — 搜索引擎缓存层(多 Agent 支持) // G9: L1 内存 + L2 Redis 两级缓存 package storage import ( "crypto/sha256" "encoding/json" "fmt" "strconv" "strings" "sync" "time" ) // L2CacheBackend 提供可选的 L2 持久化缓存接口 type L2CacheBackend interface { Get(key string) ([]byte, bool) Set(key string, data []byte, ttl time.Duration) Del(key string) // DelPrefix 按前缀批量删除(按 namespace / query 失效用,避免逐键枚举) DelPrefix(prefix string) int } // redisL2Adapter 将同包的 RedisCache 适配为 L2CacheBackend type redisL2Adapter struct{} func (a *redisL2Adapter) Get(key string) ([]byte, bool) { // 延迟获取 RedisCache 实例(避免循环 init) rc := getRedisCacheInstance() if rc == nil { return nil, false } return rc.Get(key) } func (a *redisL2Adapter) Set(key string, data []byte, ttl time.Duration) { rc := getRedisCacheInstance() if rc == nil { return } rc.Set(key, data) } func (a *redisL2Adapter) Del(key string) { rc := getRedisCacheInstance() if rc == nil { return } rc.Del(key) } func (a *redisL2Adapter) DelPrefix(prefix string) int { rc := getRedisCacheInstance() if rc == nil { return 0 } return rc.DelPrefix(prefix) } // getRedisCacheInstance 单例获取 RedisCache(延迟初始化) var ( redisCacheInstance *RedisCache redisCacheOnce sync.Once ) func getRedisCacheInstance() *RedisCache { redisCacheOnce.Do(func() { // 2026-10-13 t_1c12e000:L2 TTL 与 L1 同步 60s。 // 只改 L1 不改 L2 ⇒ L2 里 1h 的陈旧快照会在 L1 重建(重启/驱逐回填 // 重置 CreatedAt=now)后继续续命 1h ⇒ 写后盲区的陈旧窗口仍远超 60s。 redisCacheInstance = NewRedisCache(60 * time.Second) }) return redisCacheInstance } // SearchCache 基于 LRU + TTL 的搜索缓存(支持可选 L2 Redis) // Get 时:L1 miss → 查 Redis L2 → 回填 L1 // Set 时:写 L1 + 写 Redis L2(TTL 同步) type SearchCache struct { mu sync.RWMutex entries map[string]*CacheEntry maxSize int ttl time.Duration l2 L2CacheBackend // 可选 L2 缓存(Redis),nil 时只有 L1 // 2026-10-13 t_1c12e000:命中/未命中/失效计数(mu 保护下自增,Stats 在 RLock 下读取)。 hits uint64 misses uint64 invalidated uint64 } type CacheEntry struct { Key string `json:"key"` Results []byte `json:"results"` CreatedAt time.Time `json:"created_at"` Hits int `json:"hits"` } // newSearchCache 内部构造器(l2 可为 nil) func newSearchCache(maxSize int, ttl time.Duration, l2 L2CacheBackend) *SearchCache { sc := &SearchCache{ entries: make(map[string]*CacheEntry), maxSize: maxSize, ttl: ttl, l2: l2, } go sc.reaper() return sc } // NewSearchCache 创建纯 L1 内存 SearchCache(向后兼容) func NewSearchCache(maxSize int, ttl time.Duration) *SearchCache { return newSearchCache(maxSize, ttl, nil) } // NewSearchCacheWithRedis 创建带 Redis L2 的 SearchCache(G9 多级缓存) func NewSearchCacheWithRedis(maxSize int, ttl time.Duration) *SearchCache { return newSearchCache(maxSize, ttl, &redisL2Adapter{}) } // Get 获取缓存结果:L1 miss → 查 L2 → 回填 L1 // // topK / diversity 参与键构造:召回结果随它们变化(rerank/MMR 取 topK*2、 // 最终按 topK 截断),而调用方 topK 各不相同(eval 用 3 / 5,HTTP 用客户端 Limit) // ⇒ 键里不含它们会让不同调用方串用同一条缓存、拿到条数不对的结果。 func (sc *SearchCache) Get(query, namespace string, topK int, diversity float64) ([]byte, bool) { key := cacheKey(query, namespace, topK, diversity) // L1 查找 sc.mu.RLock() entry, ok := sc.entries[key] sc.mu.RUnlock() if ok && time.Since(entry.CreatedAt) <= sc.ttl { sc.mu.Lock() entry.Hits++ sc.hits++ // t_1c12e000:命中计数(L1 命中) sc.mu.Unlock() return entry.Results, true } // L1 miss,尝试 L2 if sc.l2 != nil { if data, found := sc.l2.Get(key); found { // 回填 L1 sc.mu.Lock() if len(sc.entries) >= sc.maxSize { sc.evictLRU() } sc.entries[key] = &CacheEntry{ Key: key, Results: data, CreatedAt: time.Now(), Hits: 1, } sc.hits++ // t_1c12e000:L2 命中(回填 L1)也计为命中 sc.mu.Unlock() return data, true } } // 两级全 miss sc.mu.Lock() sc.misses++ // t_1c12e000:未命中计数(走回源扫表) sc.mu.Unlock() return nil, false } // Set 写入缓存:写 L1 + 写 L2 func (sc *SearchCache) Set(query, namespace string, topK int, diversity float64, results interface{}) { key := cacheKey(query, namespace, topK, diversity) data, err := json.Marshal(results) if err != nil { return } sc.mu.Lock() defer sc.mu.Unlock() // L1 驱逐 if len(sc.entries) >= sc.maxSize { sc.evictLRU() } sc.entries[key] = &CacheEntry{ Key: key, Results: data, CreatedAt: time.Now(), Hits: 0, } // L2 写入 if sc.l2 != nil { sc.l2.Set(key, data, sc.ttl) } } // Stats 返回缓存统计 func (sc *SearchCache) Stats() map[string]interface{} { sc.mu.RLock() defer sc.mu.RUnlock() return map[string]interface{}{ "size": len(sc.entries), "max_size": sc.maxSize, "ttl": sc.ttl.String(), "l2_enabled": sc.l2 != nil, // t_1c12e000:命中/未命中/失效计数(命中率 = hits/(hits+misses),写频影响可观测) "hits": sc.hits, "misses": sc.misses, "invalidated": sc.invalidated, } } // Invalidate 使指定 namespace 的缓存失效(L1 + L2)。 // // 旧实现把 entry.Results 反序列化成 map[string]interface{} 再取 ["namespace"], // 但 Set 存进去的是 []RecallResult(JSON 数组)⇒ 反序列化恒失败 ⇒ parsed 为 nil // ⇒ 一个键都删不掉:失效是**静默空操作**(探针实测:Invalidate 后缓存仍命中, // 记忆写入后最长 1h TTL 内持续返回陈旧召回)。 // 现改为按键布局前缀匹配,不再依赖 payload 结构。 func (sc *SearchCache) Invalidate(namespace string) { prefix := cacheKeyNSPrefix(namespace) sc.mu.Lock() removed := 0 for key := range sc.entries { if strings.HasPrefix(key, prefix) { delete(sc.entries, key) removed++ } } if removed > 0 { sc.invalidated += uint64(removed) // t_1c12e000:失效计数(写路径失效的量可观测) } sc.mu.Unlock() if sc.l2 != nil { sc.l2.DelPrefix(prefix) } } // InvalidateKey 使指定 (query, namespace) 的**全部 topK/diversity 变体**失效(L1 + L2) func (sc *SearchCache) InvalidateKey(query, namespace string) { prefix := cacheKeyQueryPrefix(query, namespace) sc.mu.Lock() removed := 0 for key := range sc.entries { if strings.HasPrefix(key, prefix) { delete(sc.entries, key) removed++ } } if removed > 0 { sc.invalidated += uint64(removed) } sc.mu.Unlock() if sc.l2 != nil { sc.l2.DelPrefix(prefix) } } func (sc *SearchCache) evictLRU() { var oldestKey string var oldestTime time.Time for key, entry := range sc.entries { if oldestKey == "" || entry.CreatedAt.Before(oldestTime) { oldestKey = key oldestTime = entry.CreatedAt } } if oldestKey != "" { delete(sc.entries, oldestKey) } } func (sc *SearchCache) reaper() { ticker := time.NewTicker(5 * time.Minute) for range ticker.C { sc.mu.Lock() now := time.Now() for key, entry := range sc.entries { if now.Sub(entry.CreatedAt) > sc.ttl { delete(sc.entries, key) if sc.l2 != nil { sc.l2.Del(key) } } } sc.mu.Unlock() } } // cacheKey 构造缓存键。布局刻意做成「可前缀寻址」: // // \x00 \x00 \x00 // // 为什么 namespace 放最前:失效按 namespace 做前缀匹配,无需解析 payload。 func cacheKey(query, namespace string, topK int, diversity float64) string { h := sha256.Sum256([]byte(query)) return fmt.Sprintf("%s\x00%x\x00%d\x00%s", namespace, h[:16], topK, strconv.FormatFloat(diversity, 'f', -1, 64)) } // cacheKeyNSPrefix 某 namespace 下全部键的前缀 func cacheKeyNSPrefix(namespace string) string { return namespace + "\x00" } // cacheKeyQueryPrefix 某 (query, namespace) 下全部 topK/diversity 变体的前缀 func cacheKeyQueryPrefix(query, namespace string) string { h := sha256.Sum256([]byte(query)) return fmt.Sprintf("%s\x00%x\x00", namespace, h[:16]) } // ─── 全局搜索缓存实例(G9:带 Redis L2) ───────────────────── var SearchCacheInstance *SearchCache func init() { // 2026-10-13 t_1c12e000:TTL 1h → 60s(陈旧窗口上限)。 // 写路径(commit→蒸馏→落库)已加 Invalidate(方案 A),60s 是双保险: // 任何**漏网**写点(新调用方/异常路径)最多让同一 query 读 60s 旧快照, // 而不是 1h。命中率代价由 Stats() 的 hits/misses 观测后再决定是否收窄。 SearchCacheInstance = NewSearchCacheWithRedis(1000, 60*time.Second) }