diff --git a/go/internal/storage/recall.go b/go/internal/storage/recall.go index 3e95255..1c53a41 100644 --- a/go/internal/storage/recall.go +++ b/go/internal/storage/recall.go @@ -11,6 +11,7 @@ import ( "sort" "strconv" "strings" + "sync" "sync/atomic" "time" @@ -589,6 +590,20 @@ func (p *RecallPipeline) Recall(query, namespace string, topK int, diversity flo // ghost ns 在行集里没有行 ⇒ 检索侧无论过滤是否生效都能判掉。 // 实现 = ScanAll(后端接口方法,生产=Rust IPC scan_all;与 ANN 同库同表)。 // 后端不支持/探测失败 ⇒ 保守当存在(旧行为)。 +// +// t_a243aefa(终审实测):ScanAll 是 45k 行全表读,每次带 ns 召回都跑一次, +// 同 query 集 p50 1.23s → 2.13s(+73%,超过 20% 门禁)⇒ 加 TTL 缓存。 +// 复用现成 RedisCache(L2 Redis + 进程内 fallback,60s TTL),不新造缓存层。 +// 权衡:新 ns 首次写入/整库删除后最多 60s 内判据滞后(返回旧值), +// 对召回只影响「ghost 是否并 shared」这一档,不影响已存在 ns 的正常结果。 +var nsRowsCacheOnce sync.Once +var nsRowsCacheInst *RedisCache + +func nsRowsCache() *RedisCache { + nsRowsCacheOnce.Do(func() { nsRowsCacheInst = NewRedisCache(60 * time.Second) }) + return nsRowsCacheInst +} + func namespaceHasRows(ldb LanceDB, namespace string) bool { return NamespaceHasRowsFor(ldb, namespace) } @@ -599,6 +614,12 @@ func NamespaceHasRowsFor(ldb LanceDB, namespace string) bool { if namespace == "" || ldb == nil { return true } + cacheKeyNS := "ns-exists:" + namespace + if c := nsRowsCache(); c != nil { + if v, ok := c.Get(cacheKeyNS); ok { + return string(v) == "1" + } + } type scanAller interface { ScanAll() ([]models.MemoryRecord, error) } @@ -610,12 +631,21 @@ func NamespaceHasRowsFor(ldb LanceDB, namespace string) bool { if err != nil { return true // 探测失败 ⇒ 保守当存在(旧行为) } + found := false for _, r := range rows { if r.Namespace == namespace { - return true + found = true + break } } - return false + if c := nsRowsCache(); c != nil { + v := []byte("0") + if found { + v = []byte("1") + } + c.Set(cacheKeyNS, v) + } + return found } // recallUncached Recall 的非缓存路径。候选池大小在 Step 2.5 过滤后、