From bd4b5f9866cdcba0b380cdb6ecbfefe211dcd808 Mon Sep 17 00:00:00 2001 From: ayn2op Date: Thu, 21 May 2026 20:34:12 -0400 Subject: [PATCH] fix(cache): avoid panic in Invalidate and collapse double lookup --- internal/cache/cache.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/internal/cache/cache.go b/internal/cache/cache.go index cfd1b28..3668607 100644 --- a/internal/cache/cache.go +++ b/internal/cache/cache.go @@ -10,7 +10,7 @@ type Cache struct { } func NewCache() *Cache { - return &Cache{items: sync.Map{}} + return &Cache{} } func (c *Cache) Create(query string, value uint) { @@ -34,11 +34,14 @@ func (c *Cache) Get(query string) uint { // returned by the search results because of the search limit. func (c *Cache) Invalidate(name string, limit uint) { for name != "" { - if c.Exists(name) && c.Get(name) >= limit { + if v, ok := c.items.Load(name); ok && v.(uint) >= limit { + // This query, and every shorter prefix of it, may now be missing + // results, so drop the whole chain to force a fresh search. for name != "" { c.items.Delete(name) name = name[:len(name)-1] } + return } name = name[:len(name)-1] }