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
|
package clock
// Add inserts or replaces key, marking it recently used.
//
// It reports whether the entry was admitted;
// an entry heavier than the per-shard budget is rejected
// and leaves the cache unchanged.
func (cache *Cache[K, V]) Add(key K, value V) bool {
return cache.shardFor(key).add(key, value, cache.weightFn(key, value))
}
// Get returns the value for key and marks it recently used.
//
//nolint:ireturn
func (cache *Cache[K, V]) Get(key K) (V, bool) {
return cache.shardFor(key).get(key)
}
// Peek returns the value for key without changing its recency.
//
//nolint:ireturn
func (cache *Cache[K, V]) Peek(key K) (V, bool) {
return cache.shardFor(key).peek(key)
}
// Len returns the number of cached entries.
func (cache *Cache[K, V]) Len() int {
total := 0
for _, shard := range cache.shards {
total += shard.len()
}
return total
}
// Weight returns the current total weight across all shards.
func (cache *Cache[K, V]) Weight() uint64 {
var total uint64
for _, shard := range cache.shards {
total += shard.loadWeight()
}
return total
}
// Clear removes all entries.
func (cache *Cache[K, V]) Clear() {
for _, shard := range cache.shards {
shard.clear()
}
}
|