/
syncedlru.go
180 lines (144 loc) · 4.27 KB
/
syncedlru.go
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package freelru
import (
"sync"
"time"
)
type SyncedLRU[K comparable, V any] struct {
mu sync.RWMutex
lru *LRU[K, V]
}
var _ Cache[int, int] = (*SyncedLRU[int, int])(nil)
// SetLifetime sets the default lifetime of LRU elements.
// Lifetime 0 means "forever".
func (lru *SyncedLRU[K, V]) SetLifetime(lifetime time.Duration) {
lru.mu.Lock()
lru.lru.SetLifetime(lifetime)
lru.mu.Unlock()
}
// SetOnEvict sets the OnEvict callback function.
// The onEvict function is called for each evicted lru entry.
func (lru *SyncedLRU[K, V]) SetOnEvict(onEvict OnEvictCallback[K, V]) {
lru.mu.Lock()
lru.lru.SetOnEvict(onEvict)
lru.mu.Unlock()
}
// NewSynced creates a new thread-safe LRU hashmap with the given capacity.
func NewSynced[K comparable, V any](capacity uint32, hash HashKeyCallback[K]) (*SyncedLRU[K, V],
error) {
return NewSyncedWithSize[K, V](capacity, capacity, hash)
}
func NewSyncedWithSize[K comparable, V any](capacity, size uint32,
hash HashKeyCallback[K]) (*SyncedLRU[K, V], error) {
lru, err := NewWithSize[K, V](capacity, size, hash)
if err != nil {
return nil, err
}
return &SyncedLRU[K, V]{lru: lru}, nil
}
// Len returns the number of elements stored in the cache.
func (lru *SyncedLRU[K, V]) Len() (length int) {
lru.mu.RLock()
length = lru.lru.Len()
lru.mu.RUnlock()
return
}
// AddWithLifetime adds a key:value to the cache with a lifetime.
// Returns true, true if key was updated and eviction occurred.
func (lru *SyncedLRU[K, V]) AddWithLifetime(key K, value V, lifetime time.Duration) (evicted bool) {
hash := lru.lru.hash(key)
lru.mu.Lock()
evicted = lru.lru.addWithLifetime(hash, key, value, lifetime)
lru.mu.Unlock()
return
}
// Add adds a key:value to the cache.
// Returns true, true if key was updated and eviction occurred.
func (lru *SyncedLRU[K, V]) Add(key K, value V) (evicted bool) {
hash := lru.lru.hash(key)
lru.mu.Lock()
evicted = lru.lru.add(hash, key, value)
lru.mu.Unlock()
return
}
// Get looks up a key's value from the cache, setting it as the most
// recently used item.
func (lru *SyncedLRU[K, V]) Get(key K) (value V, ok bool) {
hash := lru.lru.hash(key)
lru.mu.Lock()
value, ok = lru.lru.get(hash, key)
lru.mu.Unlock()
return
}
// Peek looks up a key's value from the cache, without changing its recent-ness.
func (lru *SyncedLRU[K, V]) Peek(key K) (value V, ok bool) {
hash := lru.lru.hash(key)
lru.mu.RLock()
value, ok = lru.lru.peek(hash, key)
lru.mu.RUnlock()
return
}
// Contains checks for the existence of a key, without changing its recent-ness.
func (lru *SyncedLRU[K, V]) Contains(key K) (ok bool) {
hash := lru.lru.hash(key)
lru.mu.RLock()
ok = lru.lru.contains(hash, key)
lru.mu.RUnlock()
return
}
// Remove removes the key from the cache.
// The return value indicates whether the key existed or not.
// The evict function is being called if the key existed.
func (lru *SyncedLRU[K, V]) Remove(key K) (removed bool) {
hash := lru.lru.hash(key)
lru.mu.Lock()
removed = lru.lru.remove(hash, key)
lru.mu.Unlock()
return
}
// RemoveOldest removes the oldest entry from the cache.
// Key, value and an indicator of whether the entry has been removed is returned.
// The evict function is being called if the key existed.
func (lru *SyncedLRU[K, V]) RemoveOldest() (key K, value V, removed bool) {
lru.mu.Lock()
key, value, removed = lru.lru.RemoveOldest()
lru.mu.Unlock()
return
}
// Keys returns a slice of the keys in the cache, from oldest to newest.
func (lru *SyncedLRU[K, V]) Keys() (keys []K) {
lru.mu.RLock()
keys = lru.lru.Keys()
lru.mu.RUnlock()
return
}
// Purge purges all data (key and value) from the LRU.
func (lru *SyncedLRU[K, V]) Purge() {
lru.mu.Lock()
lru.lru.Purge()
lru.mu.Unlock()
}
// Metrics returns the metrics of the cache.
func (lru *SyncedLRU[K, V]) Metrics() Metrics {
lru.mu.Lock()
metrics := lru.lru.Metrics()
lru.mu.Unlock()
return metrics
}
// ResetMetrics resets the metrics of the cache and returns the previous state.
func (lru *SyncedLRU[K, V]) ResetMetrics() Metrics {
lru.mu.Lock()
metrics := lru.lru.ResetMetrics()
lru.mu.Unlock()
return metrics
}
// just used for debugging
func (lru *SyncedLRU[K, V]) dump() {
lru.mu.RLock()
lru.lru.dump()
lru.mu.RUnlock()
}
func (lru *SyncedLRU[K, V]) PrintStats() {
lru.mu.RLock()
lru.lru.PrintStats()
lru.mu.RUnlock()
}