Skip to content

Commit

Permalink
util/lru: add Clear method
Browse files Browse the repository at this point in the history
Updates tailscale/corp#20109

Signed-off-by: Andrew Dunham <andrew@du.nham.ca>
Change-Id: I751a669251a70f0134dd1540c19b274a97608a93
  • Loading branch information
andrew-d committed May 18, 2024
1 parent c56e0c4 commit 47b3476
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
6 changes: 6 additions & 0 deletions util/lru/lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand Down
4 changes: 4 additions & 0 deletions util/lru/lru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 47b3476

Please sign in to comment.