Skip to content

Commit

Permalink
Add Purge() function
Browse files Browse the repository at this point in the history
  • Loading branch information
rockdaboot committed Mar 27, 2023
1 parent dae9a8c commit 482a97d
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func NewWithSize[K comparable, V any](cap, size uint32, hash HashKeyCallback[K])
}

// Mark all slots as free.
for i := range lru.elements {
for i := range lru.buckets {
lru.buckets[i] = emptyBucket
}

Expand Down Expand Up @@ -446,6 +446,24 @@ func (lru *LRU[K, V]) Keys() []K {
return keys
}

// Purge purges all data (key and value) from the LRU.
func (lru *LRU[K, V]) Purge() {
for i := range lru.buckets {
lru.buckets[i] = emptyBucket
}

for i := range lru.elements {
lru.elements[i].key = lru.emptyKey
lru.elements[i].value = lru.emptyValue
}

lru.len = 0
lru.collisions = 0
lru.inserts = 0
lru.evictions = 0
lru.removals = 0
}

// just used for debugging
func (lru *LRU[K, V]) dump() {
fmt.Printf("head %d len %d cap %d size %d mask 0x%X\n",
Expand Down

0 comments on commit 482a97d

Please sign in to comment.