-
Notifications
You must be signed in to change notification settings - Fork 55
/
table.go
480 lines (420 loc) · 10.5 KB
/
table.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
package zcache
import (
"path/filepath"
"sort"
"sync"
"time"
"encoding/gob"
"github.com/sohaha/zlsgo/zfile"
"github.com/sohaha/zlsgo/zjson"
"github.com/sohaha/zlsgo/zlog"
"github.com/sohaha/zlsgo/zstring"
// "sync/atomic"
)
type (
// CacheItemPair maps key to access counter
CacheItemPair struct {
Key string
AccessCount int64
}
// CacheItemPairList CacheItemPairList
CacheItemPairList []CacheItemPair
// Table Table
Table struct {
sync.RWMutex
name string
items map[string]*Item
accessCount bool
cleanupTimer *time.Timer
cleanupInterval time.Duration
logger *zlog.Logger
loadNotCallback func(key string, args ...interface{}) *Item
addCallback func(item *Item)
deleteCallback func(key string) bool
}
persistenceSt struct {
Data interface{}
LifeSpan time.Duration
IntervalLifeSpan bool
}
)
// Count get the number of caches
func (table *Table) Count() int {
table.RLock()
defer table.RUnlock()
return len(table.items)
}
func (table *Table) PersistenceToFile(file string, DisableAutoLoad bool, register ...interface{}) func() error {
gob.Register(&persistenceSt{})
file = zfile.RealPath(file)
for k := range register {
gob.Register(register[k])
}
if zfile.FileExist(file) {
content, err := zfile.ReadFile(file)
if err == nil {
zjson.ParseBytes(content).ForEach(func(k, v zjson.Res) (b bool) {
b = true
key := k.String()
base64 := zstring.String2Bytes(v.String())
base64, err := zstring.Base64Decode(base64)
if err != nil {
return
}
value, err := zstring.UnSerialize(base64)
if err != nil {
return
}
if persistence, ok := value.(*persistenceSt); ok {
zlog.Debug(key, persistence)
table.SetRaw(key, persistence.Data, persistence.LifeSpan, persistence.IntervalLifeSpan)
}
return
})
}
}
return func() error {
jsonData := table.exportJSON()
_ = zfile.RealPathMkdir(filepath.Dir(file))
return zfile.WriteFile(file, zstring.String2Bytes(jsonData))
}
}
// ForEach traversing the cache
func (table *Table) ForEach(trans func(key string, value interface{}) bool) {
table.ForEachRaw(func(k string, v *Item) bool {
return trans(k, v.Data())
})
}
// ForEachRaw traversing the cache
func (table *Table) ForEachRaw(trans func(key string, value *Item) bool) {
count := table.Count()
table.RLock()
items := make(map[string]*Item, count)
for k, v := range table.items {
items[k] = v
}
table.RUnlock()
for k, v := range items {
if !trans(k, v) {
break
}
}
}
func (table *Table) exportJSON(registers ...interface{}) string {
for i := range registers {
gob.Register(registers[i])
}
jsonData := "{}"
table.ForEachRaw(func(key string, item *Item) bool {
item.RLock()
v := &persistenceSt{
Data: item.data,
LifeSpan: item.lifeSpan,
IntervalLifeSpan: item.intervalLifeSpan,
}
item.RUnlock()
value, err := zstring.Serialize(v)
if err != nil {
return true
}
jsonData, _ = zjson.Set(jsonData, key, zstring.Base64Encode(value))
return true
})
return jsonData
}
// SetLoadNotCallback SetLoadNotCallback
func (table *Table) SetLoadNotCallback(f func(key string, args ...interface{}) *Item) {
table.Lock()
defer table.Unlock()
table.loadNotCallback = f
}
// SetAddCallback SetAddCallback
func (table *Table) SetAddCallback(f func(*Item)) {
table.Lock()
defer table.Unlock()
table.addCallback = f
}
// SetDeleteCallback SetDeleteCallback
func (table *Table) SetDeleteCallback(f func(key string) bool) {
table.Lock()
defer table.Unlock()
table.deleteCallback = f
}
// SetLogger SetLogger
func (table *Table) SetLogger(logger *zlog.Logger) {
table.Lock()
defer table.Unlock()
table.logger = logger
}
func (table *Table) expirationCheck() {
now := time.Now()
smallestDuration := 0 * time.Second
table.Lock()
if table.cleanupTimer != nil {
table.cleanupTimer.Stop()
}
if table.cleanupInterval > 0 {
table.log("Expiration check triggered after", table.cleanupInterval, "for table", table.name)
} else {
table.log("Expiration check installed for table", table.name)
}
for key, item := range table.items {
item.RLock()
lifeSpan := item.lifeSpan
accessedOn := item.accessedTime
intervalLifeSpan := item.intervalLifeSpan
item.RUnlock()
if lifeSpan == 0 {
continue
}
remainingLift := item.RemainingLife()
if table.accessCount && intervalLifeSpan {
lastTime := now.Sub(accessedOn)
table.log(lastTime, lifeSpan, accessedOn)
if lastTime >= lifeSpan {
_, _ = table.deleteInternal(key)
} else {
lifeSpan = lifeSpan * 2
// table.Lock()
item.Lock()
item.lifeSpan = lifeSpan
item.Unlock()
// table.Unlock()
nextDuration := lifeSpan - lastTime
if smallestDuration == 0 || nextDuration < smallestDuration {
smallestDuration = nextDuration
}
}
} else if remainingLift <= 0 {
_, _ = table.deleteInternal(key)
} else {
if smallestDuration == 0 || smallestDuration > remainingLift {
smallestDuration = remainingLift
}
}
}
table.cleanupInterval = smallestDuration
if smallestDuration > 0 {
table.cleanupTimer = time.AfterFunc(smallestDuration, func() {
go table.expirationCheck()
})
}
table.Unlock()
}
func (table *Table) addInternal(item *Item) {
table.log("Adding item with key", item.key, "and lifespan of", item.lifeSpan, "to table", table.name)
table.items[item.key] = item
expDur := table.cleanupInterval
addedItem := table.addCallback
table.Unlock()
if addedItem != nil {
addedItem(item)
}
item.RLock()
lifeSpan := item.lifeSpan
item.RUnlock()
if lifeSpan > 0 && (expDur == 0 || lifeSpan < expDur) {
go table.expirationCheck()
}
}
// SetRaw set cache
func (table *Table) SetRaw(key string, data interface{}, lifeSpan time.Duration,
intervalLifeSpan ...bool) *Item {
item := NewCacheItem(key, data, lifeSpan)
table.Lock()
if len(intervalLifeSpan) > 0 && intervalLifeSpan[0] {
if !table.accessCount {
table.accessCount = true
}
item.intervalLifeSpan = intervalLifeSpan[0]
}
table.addInternal(item)
return item
}
// Set set cache whether to automatically renew
func (table *Table) Set(key string, data interface{}, lifeSpan uint,
interval ...bool) *Item {
return table.SetRaw(key, data, time.Duration(lifeSpan)*time.Second, interval...)
}
func (table *Table) deleteInternal(key string) (*Item, error) {
r, ok := table.items[key]
if !ok {
return nil, ErrKeyNotFound
}
table.log("Deleting item with key", key, "created on", r.createdTime, "and hit", r.accessCount, "times from table", table.name)
deleteCallback := table.deleteCallback
table.Unlock()
if deleteCallback != nil && !deleteCallback(r.key) {
table.Lock()
r.RLock()
defer r.RUnlock()
r.accessedTime = time.Now()
return r, nil
}
r.RLock()
defer r.RUnlock()
if r.deleteCallback != nil && !r.deleteCallback(r.key) {
table.Lock()
r.RLock()
defer r.RUnlock()
r.accessedTime = time.Now()
return r, nil
}
table.Lock()
delete(table.items, key)
return r, nil
}
// Delete Delete cache
func (table *Table) Delete(key string) (*Item, error) {
table.Lock()
defer table.Unlock()
return table.deleteInternal(key)
}
// Exists Exists
func (table *Table) Exists(key string) bool {
table.RLock()
defer table.RUnlock()
_, ok := table.items[key]
return ok
}
// Add if the cache does not exist then adding does not take effect
func (table *Table) Add(key string, data interface{}, lifeSpan time.Duration, intervalLifeSpan ...bool) bool {
table.Lock()
if _, ok := table.items[key]; ok {
table.Unlock()
return false
}
item := NewCacheItem(key, data, lifeSpan)
if len(intervalLifeSpan) > 0 {
item.intervalLifeSpan = intervalLifeSpan[0]
}
table.addInternal(item)
return true
}
// MustGet get the data of the specified key, set if it does not exist
func (table *Table) MustGet(key string, do func(set func(data interface{},
lifeSpan time.Duration, interval ...bool)) (
err error)) (data interface{}, err error) {
table.Lock()
r, ok := table.items[key]
if ok {
table.Unlock()
r.keepAlive()
return r.Data(), nil
}
item := NewCacheItem(key, "", 0)
item.Lock()
table.items[key] = item
table.Unlock()
err = do(func(data interface{},
lifeSpan time.Duration, interval ...bool) {
item.data = data
item.lifeSpan = lifeSpan
if len(interval) > 0 {
item.intervalLifeSpan = interval[0]
}
})
item.Unlock()
table.Lock()
if err != nil {
delete(table.items, key)
table.Unlock()
return
}
data = item.data
table.addInternal(item)
return
}
// GetT GetT
func (table *Table) GetT(key string, args ...interface{}) (*Item, error) {
table.RLock()
r, ok := table.items[key]
table.RUnlock()
if ok {
if table.accessCount {
r.keepAlive()
}
return r, nil
}
loadData := table.loadNotCallback
if loadData != nil {
item := loadData(key, args...)
if item != nil {
table.SetRaw(key, item.data, item.lifeSpan)
return item, nil
}
return nil, ErrKeyNotFoundAndNotCallback
}
return nil, ErrKeyNotFound
}
// Get get the data of the specified key
func (table *Table) Get(key string, args ...interface{}) (value interface{}, err error) {
var data *Item
data, err = table.GetT(key, args...)
if err != nil {
return
}
value = data.Data()
return
}
func (table *Table) GetString(key string, args ...interface{}) (value string, err error) {
data, err := table.Get(key, args...)
if err != nil {
return
}
value, _ = data.(string)
return
}
func (table *Table) GetInt(key string, args ...interface{}) (value int, err error) {
data, err := table.Get(key, args...)
if err != nil {
return
}
value, _ = data.(int)
return
}
// Clear Clear
func (table *Table) Clear() {
table.Lock()
defer table.Unlock()
table.log("Flushing table", table.name)
table.items = make(map[string]*Item)
table.cleanupInterval = 0
if table.cleanupTimer != nil {
table.cleanupTimer.Stop()
}
}
func (p CacheItemPairList) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p CacheItemPairList) Len() int { return len(p) }
func (p CacheItemPairList) Less(i, j int) bool { return p[i].AccessCount > p[j].AccessCount }
// MostAccessed MostAccessed
func (table *Table) MostAccessed(count int64) []*Item {
table.RLock()
defer table.RUnlock()
p := make(CacheItemPairList, len(table.items))
i := 0
for k, v := range table.items {
p[i] = CacheItemPair{k, v.accessCount}
i++
}
sort.Sort(p)
var r []*Item
c := int64(0)
for _, v := range p {
if c >= count {
break
}
item, ok := table.items[v.Key]
if ok {
r = append(r, item)
}
c++
}
return r
}
func (table *Table) log(v ...interface{}) {
if table.logger == nil {
return
}
table.logger.Debug(v...)
}