-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.go
281 lines (234 loc) · 4.99 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
package main
import (
//"fmt"
"log"
"time"
)
type readOp struct {
app string
key string
val string
found bool
done chan bool
}
type writeOp struct {
app string
key string
val string
ttl int64
ct int64
done chan bool
}
type removeOp struct {
app string
key string
found bool
done chan bool
}
type statsOp struct {
stats *cacheStats
done chan bool
}
type storageUnit struct {
CreatedAt int64
Value string
TTL int64
}
type cacheStats struct {
Node string `json:"node"`
Nodes []sibling `json:"nodes"`
Applications []string `json:"applications"`
MemBytes int64 `json:"mem_bytes"`
ElapsedNs int64 `json:"elapsed_ns"`
}
type appCache map[string]map[string]storageUnit
type cacheOperator struct {
storage appCache
Reads chan *readOp
Writes chan *writeOp
RemoveKey chan *removeOp
RemoveApp chan *removeOp
RemoveAll chan *removeOp
CacheStats chan *statsOp
Imports chan *writeOp
GcTimer *time.Ticker
}
/*func init() {
CACHE = NewCache()
}
*/
func newCache() *cacheOperator {
c := &cacheOperator{storage: appCache{},
Reads: make(chan *readOp),
Writes: make(chan *writeOp),
RemoveKey: make(chan *removeOp),
RemoveApp: make(chan *removeOp),
RemoveAll: make(chan *removeOp),
CacheStats: make(chan *statsOp),
Imports: make(chan *writeOp),
GcTimer: time.NewTicker(time.Duration(cacheGCFreq) * time.Second),
}
go func() {
for {
select {
case read := <-c.Reads:
{
log.Println("cache::readop", read)
read.val, read.found = c.get(read.app, read.key)
read.done <- true
log.Println("cache::readop", read, "done")
}
case write := <-c.Writes:
{
c.set(write.app, write.key, write.val, write.ttl)
//write.done <- true
}
case removek := <-c.RemoveKey:
{
removek.found = c.removeKey(removek.app, removek.key)
removek.done <- true
}
case removea := <-c.RemoveApp:
{
removea.found = c.removeApp(removea.app)
removea.done <- true
}
case removeall := <-c.RemoveAll:
{
c.removeAll()
removeall.done <- true
}
case stats := <-c.CacheStats:
{
stats.stats = c.stats()
stats.done <- true
}
case importf := <-c.Imports:
{
c.importForeign(importf)
//importf.done <- true
}
case <-c.GcTimer.C:
c.gc()
}
}
}()
return c
}
func (c *cacheOperator) set(appname, key, value string, ttl int64) {
su := storageUnit{
CreatedAt: time.Now().Unix(),
Value: value,
TTL: ttl}
_, ok := c.storage[appname]
if !ok {
c.storage[appname] = map[string]storageUnit{}
}
c.storage[appname][key] = su
}
func (c *cacheOperator) get(appname, key string) (string, bool) {
cache, ok := c.storage[appname]
if !ok {
return "", ok
}
su, ok := cache[key]
if !ok {
return "", ok
}
if time.Now().Unix()-su.CreatedAt >= su.TTL {
delete(c.storage[appname], key)
return "", false
}
return su.Value, ok
}
func (c *cacheOperator) removeKey(appname, key string) bool {
cache, ok := c.storage[appname]
if !ok {
return false
}
_, ok = cache[key]
if !ok {
return false
}
delete(c.storage[appname], key)
return true
}
func (c *cacheOperator) removeApp(appname string) bool {
_, ok := c.storage[appname]
if !ok {
return false
}
delete(c.storage, appname)
return true
}
func (c *cacheOperator) removeAll() {
//for app, _ := range c.storage {
for app := range c.storage {
delete(c.storage, app)
}
}
func (c *cacheOperator) gc() {
log.Println("cache::Starting GC")
for app, cache := range c.storage {
for k, su := range cache {
if time.Now().Unix()-su.CreatedAt > su.TTL {
delete(c.storage[app], k)
log.Printf("cache::GC %s->%s has been killed", app, k)
}
}
if len(c.storage[app]) == 0 {
delete(c.storage, app)
}
}
log.Println("cache::GC finished")
}
func (c *cacheOperator) stats() *cacheStats {
cs := &cacheStats{
Node: ME,
Nodes: siblingsMgr.getSiblings(),
MemBytes: 0,
}
for app, cache := range c.storage {
cs.Applications = append(cs.Applications, app)
for k, su := range cache {
cs.MemBytes += int64(len(k)) + int64(len(su.Value))
}
}
return cs
}
type exportUnit struct {
AppName string `json:"appname"`
CreatedAt int64 `json:"created_at"`
Key string `json:"key"`
Value string `json:"val"`
TTL int64 `json:"ttl"`
}
func (c *cacheOperator) contentExporter(ch chan *exportUnit) {
for app, cache := range c.storage {
for k, su := range cache {
eu := &exportUnit{
AppName: app,
CreatedAt: su.CreatedAt,
Key: k,
Value: su.Value,
TTL: su.TTL,
}
delete(c.storage[app], k)
ch <- eu
log.Printf("cache::export %+v ready to export\n", eu)
}
}
close(ch)
}
func (c *cacheOperator) importForeign(wop *writeOp) {
su := storageUnit{
CreatedAt: wop.ct,
Value: wop.val,
TTL: wop.ttl,
}
_, ok := c.storage[wop.app]
if !ok {
c.storage[wop.app] = map[string]storageUnit{}
}
c.storage[wop.app][wop.key] = su
}