-
Notifications
You must be signed in to change notification settings - Fork 1
/
cache.go
218 lines (184 loc) · 4.54 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
package lwcache
import (
"sync"
"time"
)
const (
NoExpire = time.Duration(0)
)
const (
refresherOn = true
refresherOff = false
)
type refreshNotifier struct {
c *cache
key interface{}
}
type Cache interface {
Get(key interface{}) (value interface{}, ok bool)
Set(key interface{}, item interface{}, expire time.Duration)
SetExpire(key interface{}, expire time.Duration)
SetRefresher(fn func(c Cache, key interface{}, currentValue interface{}) (newValue interface{}, err error))
StartRefresher(key interface{}, refreshInterval time.Duration)
StopRefresher(key interface{})
}
type cache struct {
namespace string
items map[interface{}]cacheItem
mutex sync.RWMutex
refresher func(c Cache, key interface{}, currentValue interface{}) (newValue interface{}, err error)
// for refresher
refresherStatus map[interface{}]bool
refresherMutex sync.RWMutex
}
var _ Cache = (*cache)(nil)
type cacheItem struct {
expiration time.Time
value interface{}
deleter *time.Timer
}
func New(name string) Cache {
c := &cache{
namespace: name,
items: make(map[interface{}]cacheItem),
mutex: sync.RWMutex{},
refresherStatus: make(map[interface{}]bool),
refresherMutex: sync.RWMutex{},
}
return c
}
// TODO: implement MSet (multi set) for performance
func (c *cache) Set(key, value interface{}, expire time.Duration) {
var (
expiration time.Time
deleter *time.Timer
)
if expire != NoExpire {
expiration = time.Now().Add(expire)
deleter = time.AfterFunc(expire, func() {
c.mutexDelete(key)
})
}
item := cacheItem{
value: value,
expiration: expiration,
deleter: deleter,
}
c.mutexSet(key, item)
}
func (c *cache) SetExpire(key interface{}, expire time.Duration) {
if item, ok := c.mutexGet(key); ok {
if expire == NoExpire {
item.expiration = time.Time{}
if item.deleter != nil {
item.deleter.Stop()
item.deleter = nil
}
} else {
item.expiration = time.Now().Add(expire)
if item.deleter != nil {
item.deleter.Reset(expire)
} else {
item.deleter = time.AfterFunc(expire, func() {
c.mutexDelete(key)
})
}
}
c.mutexSet(key, item)
}
}
func (c *cache) SetRefresher(fn func(c Cache, key interface{}, currentValue interface{}) (newValue interface{}, err error)) {
c.mutex.Lock()
defer c.mutex.Unlock()
c.refresher = fn
}
func (c *cache) StartRefresher(key interface{}, refreshInterval time.Duration) {
c.refresherMutex.RLock()
running := c.refresherStatus[key]
c.refresherMutex.RUnlock()
if !running {
c.refresherMutex.Lock()
c.refresherStatus[key] = refresherOn
c.refresherMutex.Unlock()
time.AfterFunc(refreshInterval, c.startBackGroundRefresh(key, refreshInterval))
}
}
func (c *cache) StopRefresher(key interface{}) {
c.refresherMutex.Lock()
c.refresherStatus[key] = false
c.refresherMutex.Unlock()
}
func (c *cache) startBackGroundRefresh(key interface{}, interval time.Duration) func() {
return func() {
c.refresherMutex.RLock()
running := c.refresherStatus[key]
c.refresherMutex.RUnlock()
if !running {
return
}
c.refresh(key)
time.AfterFunc(interval, c.startBackGroundRefresh(key, interval))
}
}
/*
func (c *cache) OnExpire(key string, fn func(item interface{}) error) error {
return fn(c.item)
}
func (c *cache) OnRefresh(key string, fn func(item interface{}) error) error {
return fn(c.item)
}
*/
// TODO: implement MGet (multi get) for performance
func (c *cache) Get(key interface{}) (interface{}, bool) {
item, ok := c.mutexGet(key)
// no expire on expiration is zero value
if !ok {
return nil, false
}
return item.value, ok
}
func (c *cache) mutexGet(key interface{}) (cacheItem, bool) {
c.mutex.RLock()
item, ok := c.items[key]
c.mutex.RUnlock()
return item, ok
}
func (c *cache) mutexDelete(key interface{}) {
c.mutex.Lock()
defer c.mutex.Unlock()
// for avoid goroutine leak
if c.items[key].deleter != nil {
c.items[key].deleter.Stop()
}
delete(c.items, key)
}
func (c *cache) mutexSet(key interface{}, item cacheItem) {
c.mutex.Lock()
defer c.mutex.Unlock()
c.items[key] = item
}
func (c *cache) mutexSetValue(key, value interface{}) {
c.mutex.Lock()
defer c.mutex.Unlock()
item := c.items[key]
item.value = value
c.items[key] = item
}
func (c *cache) refresh(key interface{}) {
// avoid panic
// see: https://github.com/azihsoyn/lwcache/issues/5
if c.refresher == nil {
return
}
item, ok := c.mutexGet(key)
if !ok {
// item deleted. not refresh
return
}
val, err := c.refresher(c, key, item.value)
if err != nil {
// TODO?: notifyError
return
}
c.mutexSetValue(key, val)
}