From 8520e307213969815817edaf455a8189c12c6f2f Mon Sep 17 00:00:00 2001 From: Steven Date: Tue, 27 May 2025 23:44:39 +0800 Subject: [PATCH] fix: handle type assertion safely --- store/cache/cache.go | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/store/cache/cache.go b/store/cache/cache.go index 69d9cfe57..d1d68bded 100644 --- a/store/cache/cache.go +++ b/store/cache/cache.go @@ -152,8 +152,9 @@ func (c *Cache) Delete(_ context.Context, key string) { atomic.AddInt64(&c.itemCount, -1) if c.config.OnEviction != nil { - itm, _ := value.(item) - c.config.OnEviction(key, itm.value) + if itm, ok := value.(item); ok { + c.config.OnEviction(key, itm.value) + } } } } @@ -162,8 +163,13 @@ func (c *Cache) Delete(_ context.Context, key string) { func (c *Cache) Clear(_ context.Context) { if c.config.OnEviction != nil { c.data.Range(func(key, value any) bool { - itm, _ := value.(item) - c.config.OnEviction(key.(string), itm.value) + itm, ok := value.(item) + if !ok { + return true + } + if keyStr, ok := key.(string); ok { + c.config.OnEviction(keyStr, itm.value) + } return true }) } @@ -214,13 +220,18 @@ func (c *Cache) cleanup() { count := 0 c.data.Range(func(key, value any) bool { - itm, _ := value.(item) + itm, ok := value.(item) + if !ok { + return true + } if time.Now().After(itm.expiration) { c.data.Delete(key) count++ if c.config.OnEviction != nil { - evicted[key.(string)] = itm.value + if keyStr, ok := key.(string); ok { + evicted[keyStr] = itm.value + } } } return true @@ -261,9 +272,12 @@ func (c *Cache) cleanupOldest() { candidates := make([]keyExpPair, 0, threshold) c.data.Range(func(key, value any) bool { - itm := value.(item) - if len(candidates) < threshold { - candidates = append(candidates, keyExpPair{key.(string), itm.value, itm.expiration}) + itm, ok := value.(item) + if !ok { + return true + } + if keyStr, ok := key.(string); ok && len(candidates) < threshold { + candidates = append(candidates, keyExpPair{keyStr, itm.value, itm.expiration}) return true }