aboutsummaryrefslogtreecommitdiff
path: root/internal/lru/remove.go
blob: 79735edf9b091b1ea3f5be6c0ca85bf1c99f4aaf (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package lru

import "container/list"

// Remove deletes key from the cache.
func (cache *Cache[K, V]) Remove(key K) (V, bool) {
	elem, ok := cache.items[key]
	if !ok {
		var zero V

		return zero, false
	}

	ent := cache.removeElem(elem)

	return ent.value, true
}

func (cache *Cache[K, V]) removeElem(elem *list.Element) *entry[K, V] {
	//nolint:forcetypeassert
	ent := elem.Value.(*entry[K, V])
	cache.lru.Remove(elem)
	delete(cache.items, ent.key)

	cache.weight -= ent.weight
	if cache.onEvict != nil {
		cache.onEvict(ent.key, ent.value)
	}

	return ent
}