-
Notifications
You must be signed in to change notification settings - Fork 0
/
low_lruk.go
173 lines (160 loc) · 3.27 KB
/
low_lruk.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
package gcache
type kValue struct {
Count int
Key interface{}
Value interface{}
}
// A low-level implementation of lruk, use LRUK unless you know exactly what you are doing.
type LowLRUK struct {
opts lowLRUKOptions
history, lru LowCache
}
// NewLowLRUK create a low-level lru, use NewLRUK unless you know exactly what you are doing.
func NewLowLRUK(history, lru LowCache, opt ...LowLRUKOption) *LowLRUK {
opts := defaultLowLRUKOptions
for _, o := range opt {
o.apply(&opts)
}
if opts.k < 2 {
history = nil
}
return &LowLRUK{
opts: opts,
lru: lru,
history: history,
}
}
// Clear Expired cache
func (l *LowLRUK) ClearExpired() {
l.lru.ClearExpired()
if l.history != nil {
l.history.ClearExpired()
}
}
// Add the value to the cache, only when the key does not exist
func (l *LowLRUK) Add(key, value interface{}) (added bool) {
_, exists := l.lru.Get(key)
if exists {
return
} else if l.history == nil {
added = l.lru.Add(key, value)
return
}
v, exists := l.history.Get(key)
if exists {
kv := v.(kValue)
kv.Count++
if kv.Count >= l.opts.k {
l.history.Delete(key)
added = l.lru.Add(key, value)
} else {
l.history.Put(key, kv)
}
} else {
kv := kValue{
Count: 1,
Key: key,
}
if !l.opts.historyOnlyKey {
kv.Value = value
added = true
}
l.history.Put(key, kv)
}
return
}
// Put key value to cache
func (l *LowLRUK) Put(key, value interface{}) (delkey, delval interface{}, deleted bool) {
_, exists := l.lru.Get(key)
if exists {
delkey, delval, deleted = l.lru.Put(key, value)
return
} else if l.history == nil {
delkey, delval, deleted = l.lru.Put(key, value)
return
}
v, exists := l.history.Get(key)
if exists {
kv := v.(kValue)
kv.Count++
if kv.Count >= l.opts.k {
l.history.Delete(key)
delkey, delval, deleted = l.lru.Put(key, value)
} else {
l.history.Put(key, kv)
}
} else {
kv := kValue{
Count: 1,
Key: key,
}
if l.opts.historyOnlyKey {
l.history.Put(key, kv)
} else {
kv.Value = value
delkey, delval, deleted = l.history.Put(key, kv)
}
}
return
}
// Get return cache value
func (l *LowLRUK) Get(key interface{}) (value interface{}, exists bool) {
value, exists = l.lru.Get(key)
if exists || l.history == nil {
return
}
v, ok := l.history.Get(key)
if ok {
kv := v.(kValue)
value = kv.Value
if l.opts.historyOnlyKey {
// mov to hot
if kv.Count < l.opts.k-1 {
kv.Count++
}
l.history.Put(key, kv)
} else {
exists = true
kv.Count++
if kv.Count >= l.opts.k {
l.history.Delete(key)
l.lru.Put(key, kv.Value)
} else {
l.history.Put(key, kv)
}
}
} else if l.opts.historyOnlyKey {
l.history.Put(key, kValue{
Count: 1,
Key: key,
})
}
return
}
// Delete key from cache
func (l *LowLRUK) Delete(key ...interface{}) (changed int) {
changed = l.lru.Delete(key...)
if l.history != nil {
if l.opts.historyOnlyKey {
l.history.Delete(key...)
} else {
changed += l.history.Delete(key...)
}
}
return
}
// Len returns the number of cached data
func (l *LowLRUK) Len() int {
count := l.lru.Len()
if l.history != nil && !l.opts.historyOnlyKey {
count += l.history.Len()
}
return count
}
// Clear all cached data
func (l *LowLRUK) Clear() {
l.lru.Clear()
if l.history != nil {
l.history.Clear()
}
}