forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lru_cache.go
110 lines (86 loc) · 2.19 KB
/
lru_cache.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
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package cache
import (
"sync"
"github.com/ava-labs/avalanchego/utils"
"github.com/ava-labs/avalanchego/utils/linkedhashmap"
)
var _ Cacher[struct{}, struct{}] = (*LRU[struct{}, struct{}])(nil)
// LRU is a key value store with bounded size. If the size is attempted to be
// exceeded, then an element is removed from the cache before the insertion is
// done, based on evicting the least recently used value.
type LRU[K comparable, V any] struct {
lock sync.Mutex
elements linkedhashmap.LinkedHashmap[K, V]
// If set to < 0, will be set internally to 1.
Size int
}
func (c *LRU[K, V]) Put(key K, value V) {
c.lock.Lock()
defer c.lock.Unlock()
c.put(key, value)
}
func (c *LRU[K, V]) Get(key K) (V, bool) {
c.lock.Lock()
defer c.lock.Unlock()
return c.get(key)
}
func (c *LRU[K, _]) Evict(key K) {
c.lock.Lock()
defer c.lock.Unlock()
c.evict(key)
}
func (c *LRU[_, _]) Flush() {
c.lock.Lock()
defer c.lock.Unlock()
c.flush()
}
func (c *LRU[_, _]) PortionFilled() float64 {
c.lock.Lock()
defer c.lock.Unlock()
return c.portionFilled()
}
func (c *LRU[K, V]) put(key K, value V) {
c.resize()
if c.elements.Len() == c.Size {
oldestKey, _, _ := c.elements.Oldest()
c.elements.Delete(oldestKey)
}
c.elements.Put(key, value)
}
func (c *LRU[K, V]) get(key K) (V, bool) {
c.resize()
val, ok := c.elements.Get(key)
if !ok {
return utils.Zero[V](), false
}
c.elements.Put(key, val) // Mark [k] as MRU.
return val, true
}
func (c *LRU[K, _]) evict(key K) {
c.resize()
c.elements.Delete(key)
}
func (c *LRU[K, V]) flush() {
c.elements = linkedhashmap.New[K, V]()
}
func (c *LRU[_, _]) portionFilled() float64 {
return float64(c.elements.Len()) / float64(c.Size)
}
// Initializes [c.elements] if it's nil.
// Sets [c.size] to 1 if it's <= 0.
// Removes oldest elements to make number of elements
// in the cache == [c.size] if necessary.
func (c *LRU[K, V]) resize() {
if c.elements == nil {
c.elements = linkedhashmap.New[K, V]()
}
if c.Size <= 0 {
c.Size = 1
}
for c.elements.Len() > c.Size {
oldestKey, _, _ := c.elements.Oldest()
c.elements.Delete(oldestKey)
}
}