-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.go
192 lines (159 loc) · 4.46 KB
/
map.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
package kash
import (
"time"
"sync"
)
/*
Cache Notes
- http://stackoverflow.com/questions/10153724/google-guavas-cacheloader-loadall-vs-reload-semantics
- http://stackoverflow.com/questions/31983275/how-to-change-external-variables-value-inside-a-goroutine-closure
- http://google.github.io/guava/releases/snapshot/api/docs/com/google/common/cache/CacheBuilder.html
- http://google.github.io/guava/releases/snapshot/api/docs/com/google/common/cache/LoadingCache.html
- https://github.com/google/guava/wiki/CachesExplained
*/
type KeyValue struct {
Key interface{}
Value interface{}
}
type MapCache struct {
asyncReloadFunc func(key interface{}, prev interface{})
data map[interface{}]*element
loadFunc func(key interface{}) interface{}
sizePruneLock sync.Mutex
reloadFunc func(key interface{}, prev interface{}) interface{}
ReloadChan chan KeyValue
ttlAccessed time.Duration
ttlRefresh time.Duration
ttlWrite time.Duration
weigherFunc func(key interface{}, value interface{}) int64
weightCurr int64
weightMax int64
}
func NewMapCache() *MapCache {
nilLoadFunc := func(key interface{}) interface{} {
return nil
}
c := &MapCache{
data: make(map[interface{}]*element),
loadFunc: nilLoadFunc,
sizePruneLock: sync.Mutex{},
ttlWrite: MaxDuration,
ttlAccessed: MaxDuration,
ttlRefresh: MaxDuration,
ReloadChan: make(chan KeyValue),
weightCurr: 0,
}
c.launchReloadListener()
c.SetReload(func(key interface{}, prev interface{}) interface{} {
return c.loadFunc(key)
})
c.SetWeigher(func(key interface{}, prev interface{}) int64 {
return 1
})
return c
}
func (c *MapCache) SetLoad(loadFunc func(key interface{}) interface{}) {
c.loadFunc = loadFunc
}
func (c *MapCache) SetReload(reloadFunc func(key interface{}, prev interface{}) interface{}) {
c.reloadFunc = reloadFunc
}
func (c *MapCache) SetAsyncReload(reloadAsyncFunc func(key interface{}, prev interface{})) {
c.asyncReloadFunc = reloadAsyncFunc
}
func (c *MapCache) SetWeigher(weigherFunc func(key interface{}, value interface{}) int64) {
c.weigherFunc = weigherFunc
}
func (c *MapCache) SetMaxWeight(weight int64) {
c.weightMax = weight
}
func (c *MapCache) Get(key interface{}) (interface{}, bool) {
return c.get(key, time.Now().UTC())
}
func (c *MapCache) GetIfPresent(key interface{}) (interface{}, bool) {
return c.getIfPresent(key, time.Now().UTC())
}
func (c *MapCache) Invalidate(key interface{}) {
delete(c.data, key)
}
func (c *MapCache) Put(key interface{}, value interface{}) {
if value != nil {
c.data[key] = newElementWithWeight(value, c.weigherFunc(key, value))
}
}
func (c *MapCache) PutAll(values map[interface{}]interface{}) {
for k, v := range values {
c.Put(k, v)
}
}
func (c *MapCache) Refresh(key interface{}) {
if elem, exists := c.data[key]; exists {
if c.asyncReloadFunc != nil {
c.asyncReloadFunc(key, elem.Value)
} else {
c.Put(key, c.reloadFunc(key, elem.Value))
}
} else if value := c.loadFunc(key); value != nil {
c.Put(key, value)
}
}
func (c *MapCache) RefreshAfterWrite(ttl time.Duration) {
c.ttlRefresh = ttl
}
func (c *MapCache) ExpireAfterAccess(ttl time.Duration) {
c.ttlAccessed = ttl
}
func (c *MapCache) ExpireAfterWrite(ttl time.Duration) {
c.ttlWrite = ttl
}
func (c *MapCache) Len() int {
return len(c.data)
}
func (c *MapCache) get(key interface{}, now time.Time) (interface{}, bool) {
if elem, exists := c.data[key]; exists {
if elem.AccessStale(now, c.ttlAccessed) {
c.Invalidate(key)
return nil, false
}
if elem.WriteStale(now, c.ttlWrite) {
c.Invalidate(key)
return nil, false
}
if elem.WriteStale(now, c.ttlRefresh) {
c.Refresh(key)
defer c.updateAccessTime(key, now)
return c.data[key].Value, true
}
defer c.updateAccessTime(key, now)
return elem.Value, true
} else if value := c.loadFunc(key); value != nil {
c.Put(key, value)
return c.data[key].Value, true
} else {
return nil, false
}
}
func (c *MapCache) getIfPresent(key interface{}, now time.Time) (interface{}, bool) {
if elem, exists := c.data[key]; exists {
if !elem.WriteStale(now, c.ttlRefresh) {
return elem.Value, true
}
}
return nil, false
}
func (c *MapCache) updateAccessTime(key interface{}, now time.Time) {
if elem, exists := c.data[key]; exists {
elem.AccessedAt = now
c.data[key] = elem
}
}
func (c *MapCache) launchReloadListener() {
go func() {
for {
select {
case kv := <-c.ReloadChan:
c.Put(kv.Key, kv.Value)
}
}
}()
}