diff --git a/util/lru/lru.go b/util/lru/lru.go index aab175156..8e4dd417b 100644 --- a/util/lru/lru.go +++ b/util/lru/lru.go @@ -68,6 +68,12 @@ func (c *Cache[K, V]) Set(key K, value V) { } } +// Clear removes all items from the cache. +func (c *Cache[K, V]) Clear() { + c.head = nil + c.lookup = nil +} + // Get looks up a key's value from the cache, returning either // the value or the zero value if it not present. // diff --git a/util/lru/lru_test.go b/util/lru/lru_test.go index 3c4228a3f..fb538efbe 100644 --- a/util/lru/lru_test.go +++ b/util/lru/lru_test.go @@ -47,6 +47,10 @@ func TestLRU(t *testing.T) { if c.Contains(3) { t.Errorf("contains 3; should not") } + c.Clear() + if g, w := c.Len(), 0; g != w { + t.Errorf("Len = %d; want %d", g, w) + } } func TestLRUDeleteCorruption(t *testing.T) {