fix(cache): avoid panic in Invalidate and collapse double lookup

This commit is contained in:
ayn2op 2026-05-21 20:34:12 -04:00
parent 7d08cdcdef
commit bd4b5f9866

View file

@ -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]
}