一、3 个 recall 命名空间测试长期红灯(真因:测试对全局缓存不设防) RecallWithMeta 命中缓存即提前返回,而 SearchCacheInstance 是带 Redis L2 的包级单例 (跨 test 运行持久)⇒ 一旦缓存里有同 (query,namespace) 条目,Search 永不被调用。 用例断言「Search 被调用几次」,于是恒红。修:用例内隔离该全局(存值→置 nil→Cleanup 还原), 并补一条反证用例 TestRecallCachedPathSkipsSearch 固证「命中缓存就不查库」这一机制。 (三次反证均确认有鉴别力:撤掉隔离后两次都假红。) 二、缓存键缺 topK/diversity(真实缺陷,非测试问题) 键原为 (query,namespace),但召回结果随 topK 变化(rerank/MMR 取 topK*2、最终按 topK 截断), 而调用方 topK 各异(eval 用 3 / 5,HTTP 用客户端 Limit)⇒ 不同调用方串用同一条缓存、 拿到条数不对的结果(金标集评分会被静默少算;客户端要 50 条可能只拿到 5 条)。 修:键布局改为 <namespace>\x00<queryHash>\x00<topK>\x00<diversity>,可前缀寻址; RecallWithMeta 在查缓存前先归一 topK(否则 topK<=0 的调用永远命中不了自己写的条目)。 三、Invalidate(namespace) 是静默空操作(真实缺陷) 旧实现把 entry.Results 反序列化成 map 再读 ["namespace"],而 Set 存的是 []RecallResult (JSON 数组)⇒ 反序列化恒失败 ⇒ 一个键都删不掉。后果:记忆写入后调用方以为缓存已刷新, 实际最长 1h TTL 内持续返回陈旧召回。修:改为按键前缀匹配 + L2 DelPrefix(RedisCache.DelPrefix 用 SCAN 游标 + 批量 DEL,不用会阻塞 Redis 的 KEYS)。 验证:main 上 `go test ./... -count=1` 失败包 2→0、失败测试 5→0、通过包 7→9; 新增 8 条回归用例;反证 A(键退回不含 topK)、B(失效退回 payload 解析)、C(撤掉缓存隔离) 均确认会让对应用例变红。
286 lines
7.4 KiB
Go
286 lines
7.4 KiB
Go
// 织忆 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() {
|
||
redisCacheInstance = NewRedisCache(1 * time.Hour)
|
||
})
|
||
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
|
||
}
|
||
|
||
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.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.mu.Unlock()
|
||
return data, true
|
||
}
|
||
}
|
||
|
||
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,
|
||
}
|
||
}
|
||
|
||
// 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()
|
||
for key := range sc.entries {
|
||
if strings.HasPrefix(key, prefix) {
|
||
delete(sc.entries, key)
|
||
}
|
||
}
|
||
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()
|
||
for key := range sc.entries {
|
||
if strings.HasPrefix(key, prefix) {
|
||
delete(sc.entries, key)
|
||
}
|
||
}
|
||
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 构造缓存键。布局刻意做成「可前缀寻址」:
|
||
//
|
||
// <namespace> \x00 <queryHash(16B hex)> \x00 <topK> \x00 <diversity>
|
||
//
|
||
// 为什么 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() {
|
||
SearchCacheInstance = NewSearchCacheWithRedis(1000, 1*time.Hour)
|
||
}
|