From ecbb71b89683c1506c374b34d0fefd87ae6144e6 Mon Sep 17 00:00:00 2001 From: Ganesh Vanahalli Date: Thu, 7 Nov 2024 11:27:50 +0530 Subject: [PATCH 1/2] Add Clear method to SizeConstrainedCache --- common/lru/blob_lru.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/common/lru/blob_lru.go b/common/lru/blob_lru.go index c9b3398503..e782761c4b 100644 --- a/common/lru/blob_lru.go +++ b/common/lru/blob_lru.go @@ -82,3 +82,17 @@ func (c *SizeConstrainedCache[K, V]) Get(key K) (V, bool) { return c.lru.Get(key) } + +func (c *SizeConstrainedCache[K, V]) Remove(key K) { + c.lock.Lock() + defer c.lock.Unlock() + + c.lru.Remove(key) +} + +func (c *SizeConstrainedCache[K, V]) Clear() { + c.lock.Lock() + defer c.lock.Unlock() + + c.lru.Purge() +} From 68cb86d1eca03353375c24befaa7814f145d425a Mon Sep 17 00:00:00 2001 From: Ganesh Vanahalli Date: Tue, 12 Nov 2024 11:44:15 +0530 Subject: [PATCH 2/2] address PR comments --- common/lru/blob_lru.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/common/lru/blob_lru.go b/common/lru/blob_lru.go index e782761c4b..eecfea8db3 100644 --- a/common/lru/blob_lru.go +++ b/common/lru/blob_lru.go @@ -87,7 +87,10 @@ func (c *SizeConstrainedCache[K, V]) Remove(key K) { c.lock.Lock() defer c.lock.Unlock() - c.lru.Remove(key) + if v, ok := c.lru.Peek(key); ok { + c.size -= uint64(len(v)) + c.lru.Remove(key) + } } func (c *SizeConstrainedCache[K, V]) Clear() { @@ -95,4 +98,5 @@ func (c *SizeConstrainedCache[K, V]) Clear() { defer c.lock.Unlock() c.lru.Purge() + c.size = 0 }