-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.go
292 lines (259 loc) · 6.22 KB
/
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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
* Copyright 2012 Nan Deng
*
* 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.
*
*/
package cache2
import (
"container/list"
"fmt"
"sync"
"time"
)
type Flusher interface {
Add(key string, value interface{})
Remove(key string)
}
type dirtyElement struct {
modified bool
removed bool
key string
value interface{}
}
type cacheItem struct {
key string
value interface{}
}
type CacheInterface interface {
Len() int
Set(key string, value interface{})
Get(key string) interface{}
Delete(key string) interface{}
Flush()
debug()
}
type SimpleCache struct {
mu sync.Mutex
data map[string]*list.Element
list *list.List
capacity int
}
var _ CacheInterface = &SimpleCache{}
type Cache struct {
mu sync.Mutex
flushPeriod time.Duration
data map[string]*list.Element
dirtyList *list.List
list *list.List
capacity int
flusher Flusher
maxNrDirty int
}
var _ CacheInterface = &Cache{}
func (c *SimpleCache) Len() int {
return len(c.data)
}
func (c *Cache) Len() int {
return len(c.data)
}
func (c *SimpleCache) Flush() {}
func (c *Cache) Flush() {
c.mu.Lock()
defer c.mu.Unlock()
for e := c.dirtyList.Front(); e != nil; e = e.Next() {
if de, ok := e.Value.(*dirtyElement); ok {
if de.removed {
c.flusher.Remove(de.key)
} else if de.modified {
c.flusher.Add(de.key, de.value)
}
}
}
c.dirtyList = list.New()
}
func (c *SimpleCache) debug() {
fmt.Printf("nr elems %v <= %v", c.list.Len(), c.capacity)
fmt.Println("-----------------elements------------")
for e := c.list.Front(); e != nil; e = e.Next() {
item := e.Value.(*cacheItem)
fmt.Printf("%v: %v\n", item.key, item.value)
}
fmt.Println("-------------------------------------")
}
func (c *Cache) debug() {
fmt.Printf("nr elems %v <= %v, nr dirty elems %v < %v\n",
c.list.Len(), c.capacity, c.dirtyList.Len(), c.maxNrDirty)
fmt.Println("-----------------elements------------")
for e := c.list.Front(); e != nil; e = e.Next() {
item := e.Value.(*cacheItem)
fmt.Printf("%v: %v\n", item.key, item.value)
}
fmt.Println("-----------dirty elements------------")
for e := c.dirtyList.Front(); e != nil; e = e.Next() {
de := e.Value.(*dirtyElement)
fmt.Printf("%v: %v; modified: %v; removed %v\n",
de.key, de.value, de.modified, de.removed)
}
fmt.Println("-------------------------------------")
}
func (c *Cache) checkAndFlush() {
c.mu.Lock()
if c.maxNrDirty >= 0 && c.dirtyList.Len() >= c.maxNrDirty {
c.mu.Unlock()
c.Flush()
} else {
c.mu.Unlock()
}
}
// capacity: nr elements in the cache.
// capacity < 0 means always in memory;
// capacity = 0 means no cache.
//
// maxNrDirty: < 0 means no flush.
//
// flushPeriod:
// flushPeriod > 1 second means periodically flush;
// flushPeriod = 0 second means no periodically flush;
// undefined in range (0, 1).
func New(capacity int, maxNrDirty int, flushPeriod time.Duration, flusher Flusher) *Cache {
initialCapacity := capacity
if initialCapacity < 0 {
initialCapacity = 1024
}
if flusher == nil {
panic("Should use NewSimple")
}
cache := new(Cache)
cache.flushPeriod = flushPeriod
cache.capacity = initialCapacity
cache.data = make(map[string]*list.Element, initialCapacity)
cache.list = list.New()
cache.dirtyList = list.New()
cache.flusher = flusher
cache.maxNrDirty = maxNrDirty
if flushPeriod.Seconds() > 0.9 {
go func() {
for {
time.Sleep(flushPeriod)
cache.Flush()
}
}()
}
return cache
}
func NewSimple(capacity int) *SimpleCache {
initialCapacity := capacity
if initialCapacity < 0 {
initialCapacity = 1024
}
return &SimpleCache{
data: make(map[string]*list.Element, initialCapacity),
list: list.New(),
capacity: initialCapacity,
}
}
func (c *SimpleCache) Get(key string) interface{} {
c.mu.Lock()
defer c.mu.Unlock()
if elem, ok := c.data[key]; ok {
item := elem.Value.(*cacheItem)
c.list.MoveToFront(elem)
return item.value
}
return nil
}
func (c *Cache) Get(key string) interface{} {
c.mu.Lock()
defer c.mu.Unlock()
if elem, ok := c.data[key]; ok {
item := elem.Value.(*cacheItem)
c.list.MoveToFront(elem)
return item.value
}
return nil
}
func (c *SimpleCache) Set(key string, value interface{}) {
c.mu.Lock()
defer c.mu.Unlock()
if e, ok := c.data[key]; ok {
item := e.Value.(*cacheItem)
item.value = value
c.list.MoveToFront(e)
} else {
elem := c.list.PushFront(&cacheItem{key: key, value: value})
c.data[key] = elem
if c.capacity >= 0 && len(c.data) > c.capacity {
last := c.list.Back()
item := last.Value.(*cacheItem)
c.list.Remove(last)
delete(c.data, item.key)
}
}
return
}
func (c *Cache) Set(key string, value interface{}) {
c.mu.Lock()
defer c.checkAndFlush()
defer c.mu.Unlock()
de := &dirtyElement{
modified: true,
removed: false,
key: key,
value: value,
}
if e, ok := c.data[key]; ok {
item := e.Value.(*cacheItem)
item.value = value
c.list.MoveToFront(e)
c.dirtyList.PushBack(de)
} else {
elem := c.list.PushFront(&cacheItem{key: key, value: value})
c.data[key] = elem
c.dirtyList.PushBack(de)
if c.capacity >= 0 && len(c.data) > c.capacity {
last := c.list.Back()
item := last.Value.(*cacheItem)
c.list.Remove(last)
delete(c.data, item.key)
}
}
return
}
func (c *SimpleCache) Delete(key string) interface{} {
c.mu.Lock()
defer c.mu.Unlock()
if elem, ok := c.data[key]; ok {
delete(c.data, key)
item := elem.Value.(*cacheItem)
return item.value
}
return nil
}
func (c *Cache) Delete(key string) interface{} {
c.mu.Lock()
defer c.mu.Unlock()
de := &dirtyElement{
modified: false,
removed: true,
key: key,
value: nil,
}
c.dirtyList.PushBack(de)
if elem, ok := c.data[key]; ok {
delete(c.data, key)
item := elem.Value.(*cacheItem)
return item.value
}
return nil
}