-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrapper.go
110 lines (98 loc) · 1.86 KB
/
wrapper.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
package gcache
import (
"sync"
"time"
)
type wrapper struct {
impl LowCache
ticker *time.Ticker
closed chan struct{}
m sync.Mutex
}
// Add the value to the cache, only when the key does not exist
func (w *wrapper) Add(key, value interface{}) (added bool) {
w.m.Lock()
added = w.impl.Add(key, value)
w.m.Unlock()
return
}
// Put key value to cache
func (w *wrapper) Put(key, value interface{}) {
w.m.Lock()
w.impl.Put(key, value)
w.m.Unlock()
return
}
// Get return cache value, if not exists then return ErrNotExists
func (w *wrapper) Get(key interface{}) (value interface{}, exists bool) {
w.m.Lock()
value, exists = w.impl.Get(key)
w.m.Unlock()
return
}
// BatchPut pairs to cache
func (w *wrapper) BatchPut(pair ...interface{}) {
w.m.Lock()
count := len(pair)
for i := 0; i < count; i += 2 {
if i+1 < count {
w.impl.Put(pair[i], pair[i+1])
} else {
w.impl.Put(pair[i], nil)
break
}
}
w.m.Unlock()
return
}
// BatchGet return cache values
func (w *wrapper) BatchGet(key ...interface{}) (vals []Value) {
w.m.Lock()
vals = make([]Value, len(key))
for i, k := range key {
vals[i].Value, vals[i].Exists = w.impl.Get(k)
}
w.m.Unlock()
return
}
// Delete key from cache
func (w *wrapper) Delete(key ...interface{}) (changed int) {
w.m.Lock()
changed = w.impl.Delete(key...)
w.m.Unlock()
return
}
// Len returns the number of cached data
func (w *wrapper) Len() (count int) {
w.m.Lock()
count = w.impl.Len()
w.m.Unlock()
return
}
// Clear all cached data
func (w *wrapper) Clear() {
w.m.Lock()
w.impl.Clear()
w.m.Unlock()
}
func (w *wrapper) clearExpired(ch <-chan time.Time) {
for {
select {
case <-w.closed:
return
case <-ch:
w.m.Lock()
w.impl.ClearExpired()
w.m.Unlock()
}
}
}
func (w *wrapper) stop() {
w.m.Lock()
close(w.closed)
if w.ticker != nil {
w.ticker.Stop()
}
w.impl.Clear()
w.m.Unlock()
}