File tree 1 file changed +9
-2
lines changed
1 file changed +9
-2
lines changed Original file line number Diff line number Diff line change @@ -18,16 +18,19 @@ package lru
18
18
19
19
import (
20
20
"math"
21
+ "sync"
21
22
22
23
"github.com/hashicorp/golang-lru/simplelru"
23
24
)
24
25
25
- // SizeConstrainedLRU is a wrapper around simplelru.LRU, which
26
- // adds a byte-size constraint.
26
+ // SizeConstrainedLRU is a wrapper around simplelru.LRU. The simplelru.LRU is capable
27
+ // of item-count constraints, but is not capable of enforcing a byte-size constraint,
28
+ // hence this wrapper.
27
29
type SizeConstrainedLRU struct {
28
30
size uint64
29
31
maxSize uint64
30
32
lru * simplelru.LRU
33
+ lock sync.RWMutex
31
34
}
32
35
33
36
// NewSizeConstraiedLRU creates a new SizeConstrainedLRU.
@@ -50,6 +53,8 @@ func (c *SizeConstrainedLRU) Set(key []byte, value []byte) (evicted bool) {
50
53
51
54
// Add adds a value to the cache. Returns true if an eviction occurred.
52
55
func (c * SizeConstrainedLRU ) Add (key string , value string ) (evicted bool ) {
56
+ c .lock .Lock ()
57
+ defer c .lock .Unlock ()
53
58
targetSize := c .size + uint64 (len (value ))
54
59
for targetSize > c .maxSize {
55
60
evicted = true
@@ -67,6 +72,8 @@ func (c *SizeConstrainedLRU) Add(key string, value string) (evicted bool) {
67
72
68
73
// Get looks up a key's value from the cache.
69
74
func (c * SizeConstrainedLRU ) Get (key []byte ) []byte {
75
+ c .lock .RLock ()
76
+ defer c .lock .RUnlock ()
70
77
if v , ok := c .lru .Get (string (key )); ok {
71
78
return []byte (v .(string ))
72
79
}
You can’t perform that action at this time.
0 commit comments