forked from dgraph-io/dgraph
/
lru.go
240 lines (206 loc) · 4.56 KB
/
lru.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
Copyright 2013 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Modified by Dgraph Labs, Inc.
// Package lru implements an LRU cache.
package posting
import (
"container/list"
"context"
"sync"
"sync/atomic"
"time"
"github.com/dgraph-io/dgraph/x"
)
// listCache is an LRU cache.
type listCache struct {
sync.Mutex
ctx context.Context
// MaxSize is the maximum size of cache before an item is evicted.
MaxSize uint64
curSize uint64
evicts uint64
ll *list.List
cache map[string]*list.Element
done int32
}
type CacheStats struct {
Length int
Size uint64
NumEvicts uint64
}
type entry struct {
key string
pl *List
size uint64
}
// New creates a new Cache.
func newListCache(maxSize uint64) *listCache {
lc := &listCache{
ctx: context.Background(),
MaxSize: maxSize,
ll: list.New(),
cache: make(map[string]*list.Element),
}
go lc.removeOldestLoop()
return lc
}
func (c *listCache) UpdateMaxSize(size uint64) uint64 {
c.Lock()
defer c.Unlock()
if size == 0 {
size = c.curSize
}
if size < (50 << 20) {
size = 50 << 20
}
c.MaxSize = size
x.LcacheCapacity.Set(int64(c.MaxSize))
return c.MaxSize
}
// Add adds a value to the cache.
func (c *listCache) PutIfMissing(key string, pl *List) (res *List) {
c.Lock()
defer c.Unlock()
if ee, ok := c.cache[key]; ok {
c.ll.MoveToFront(ee)
res = ee.Value.(*entry).pl
return res
}
e := &entry{
key: key,
pl: pl,
size: uint64(pl.EstimatedSize()),
}
if e.size < 100 {
e.size = 100
}
c.curSize += e.size
ele := c.ll.PushFront(e)
c.cache[key] = ele
return e.pl
}
func (c *listCache) removeOldestLoop() {
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for range ticker.C {
c.removeOldest()
if atomic.LoadInt32(&c.done) > 0 {
return
}
}
}
func (c *listCache) removeOldest() {
c.Lock()
defer c.Unlock()
ele := c.ll.Back()
for c.curSize > c.MaxSize && atomic.LoadInt32(&c.done) == 0 {
if ele == nil {
if c.curSize < 0 {
c.curSize = 0
}
break
}
e := ele.Value.(*entry)
if !e.pl.SetForDeletion() {
ele = ele.Prev()
continue
}
// If length of mutation layer is zero, then we won't call pstore.SetAsync and the
// key wont be deleted from cache. So lets delete it now if SyncIfDirty returns false.
if committed, err := e.pl.SyncIfDirty(true); err != nil {
ele = ele.Prev()
continue
} else if !committed {
delete(c.cache, e.key)
}
// ele gets Reset once it's passed to Remove, so store the prev.
prev := ele.Prev()
c.ll.Remove(ele)
c.evicts++
c.curSize -= e.size
ele = prev
}
}
// Get looks up a key's value from the cache.
func (c *listCache) Get(key string) (pl *List) {
c.Lock()
defer c.Unlock()
if ele, hit := c.cache[key]; hit {
c.ll.MoveToFront(ele)
e := ele.Value.(*entry)
est := uint64(e.pl.EstimatedSize())
c.curSize += est - e.size
e.size = est
return e.pl
}
return nil
}
// Len returns the number of items in the cache.
func (c *listCache) Stats() CacheStats {
c.Lock()
defer c.Unlock()
return CacheStats{
Length: c.ll.Len(),
Size: c.curSize,
NumEvicts: c.evicts,
}
}
func (c *listCache) Each(f func(key []byte, val *List)) {
c.Lock()
defer c.Unlock()
ele := c.ll.Front()
for ele != nil {
e := ele.Value.(*entry)
f(e.pl.key, e.pl)
ele = ele.Next()
}
}
func (c *listCache) Reset() {
c.Lock()
defer c.Unlock()
c.ll = list.New()
c.cache = make(map[string]*list.Element)
c.curSize = 0
}
func (c *listCache) iterate(cont func(l *List) bool) {
c.Lock()
defer c.Unlock()
for _, e := range c.cache {
kv := e.Value.(*entry)
if !cont(kv.pl) {
return
}
}
}
// Doesn't sync to disk, call this function only when you are deleting the pls.
func (c *listCache) clear(remove func(key []byte) bool) {
c.Lock()
defer c.Unlock()
for k, e := range c.cache {
kv := e.Value.(*entry)
if !remove(kv.pl.key) {
continue
}
c.ll.Remove(e)
delete(c.cache, k)
}
}
// delete removes a key from cache
func (c *listCache) delete(key []byte) {
c.Lock()
defer c.Unlock()
if ele, ok := c.cache[string(key)]; ok {
c.ll.Remove(ele)
delete(c.cache, string(key))
}
}