summaryrefslogtreecommitdiff
path: root/lib/containers/lrucache.go
blob: 94094b9827d8b4882d231793765d7fcc0cc18146 (plain)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright (C) 2023  Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later

package containers

type lruEntry[K comparable, V any] struct {
	key K
	val V
}

// lruCache is a NON-thread-safe cache with Least Recently Used
// eviction.
//
// lruCache is non-thread-safe and unexported because it only exists
// for internal use by the more sophisticated ARCache.
//
// Similarly, it does not implement some common features of an LRU
// cache, such as specifying a maximum size (instead requiring the
// `.EvictOldest` method to be called), because it is only meant for
// use by the ARCache; which does not need such features.
type lruCache[K comparable, V any] struct {
	// OnRemove is (if non-nil) called *after* removal whenever
	// an entry is removed, for any reason:
	//
	//  - evicted by .EvictOldest()
	//  - replaced by .Store(k, v)
	//  - deleted by .Delete(k)
	OnRemove func(K, V)
	// OnEvict is (if non-nil) called *after* removal whenever an
	// entry is evicted by .EvictOldest().  If both OnEvict and
	// OnRemove are set, OnRemove is called first.
	OnEvict func(K, V)

	byAge  LinkedList[lruEntry[K, V]]
	byName map[K]*LinkedListEntry[lruEntry[K, V]]
}

var _ Map[int, string] = (*lruCache[int, string])(nil)

func (c *lruCache[K, V]) rem(entry *LinkedListEntry[lruEntry[K, V]]) {
	k, v := entry.Value.key, entry.Value.val
	delete(c.byName, entry.Value.key)
	c.byAge.Delete(entry)
	if c.OnRemove != nil {
		c.OnRemove(k, v)
	}
}

func (c *lruCache[K, V]) evict(entry *LinkedListEntry[lruEntry[K, V]]) {
	k, v := entry.Value.key, entry.Value.val
	c.rem(entry)
	if c.OnEvict != nil {
		c.OnEvict(k, v)
	}
}

// EvictOldest deletes the oldest entry in the cache.
//
// It is a panic to call EvictOldest if the cache is empty.
func (c *lruCache[K, V]) EvictOldest() {
	c.evict(c.byAge.Oldest())
}

// Store a key/value pair in to the cache.
func (c *lruCache[K, V]) Store(k K, v V) {
	if c.byName == nil {
		c.byName = make(map[K]*LinkedListEntry[lruEntry[K, V]])
	} else if old, ok := c.byName[k]; ok {
		c.rem(old)
	}
	c.byName[k] = c.byAge.Store(lruEntry[K, V]{key: k, val: v})
}

// Load an entry from the cache, recording a "use" for the purposes of
// "least-recently-used" eviction.
func (c *lruCache[K, V]) Load(k K) (v V, ok bool) {
	entry, ok := c.byName[k]
	if !ok {
		var zero V
		return zero, false
	}
	c.byAge.MoveToNewest(entry)
	return entry.Value.val, true
}

// Peek is like Load, but doesn't count as a "use" for
// "least-recently-used".
func (c *lruCache[K, V]) Peek(k K) (v V, ok bool) {
	entry, ok := c.byName[k]
	if !ok {
		var zero V
		return zero, false
	}
	return entry.Value.val, true
}

// Has returns whether an entry is present in the cache.  Calling Has
// does not count as a "use" for "least-recently-used".
func (c *lruCache[K, V]) Has(k K) bool {
	_, ok := c.byName[k]
	return ok
}

// Delete an entry from the cache.
func (c *lruCache[K, V]) Delete(k K) {
	if entry, ok := c.byName[k]; ok {
		c.rem(entry)
	}
}

// Len returns the number of entries in the cache.
func (c *lruCache[K, V]) Len() int {
	return len(c.byName)
}