From 47b3476eb75d10d6578eb0bd27fe7e6d4fa8459d Mon Sep 17 00:00:00 2001 From: Andrew Dunham Date: Fri, 17 May 2024 19:50:28 -0400 Subject: [PATCH] util/lru: add Clear method Updates tailscale/corp#20109 Signed-off-by: Andrew Dunham Change-Id: I751a669251a70f0134dd1540c19b274a97608a93 --- util/lru/lru.go | 6 ++++++ util/lru/lru_test.go | 4 ++++ 2 files changed, 10 insertions(+) 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) {