-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcache.go
276 lines (258 loc) · 7.18 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
package mnemosyne
import (
"context"
"encoding/json"
"errors"
"fmt"
"math/rand"
"sync"
"time"
"github.com/allegro/bigcache"
"github.com/go-redis/redis"
"github.com/sirupsen/logrus"
)
type cache struct {
layerName string
baseRedisClient *redis.Client
slaveRedisClients []*redis.Client
inMemCache *bigcache.BigCache
syncmap *sync.Map
amnesiaChance int
compressionEnabled bool
cacheTTL time.Duration
ctx context.Context
watcher ITimer
}
func newCacheRedis(layerName string, addr string, db int, TTL time.Duration, redisIdleTimeout time.Duration, amnesiaChance int, compressionEnabled bool, watcher ITimer) *cache {
redisOptions := &redis.Options{
Addr: addr,
DB: db,
}
if redisIdleTimeout >= time.Second {
redisOptions.IdleTimeout = redisIdleTimeout
}
redisClient := redis.NewClient(redisOptions)
err := redisClient.Ping().Err()
if err != nil {
logrus.WithError(err).Error("error while connecting to Redis")
}
return &cache{
layerName: layerName,
baseRedisClient: redisClient,
amnesiaChance: amnesiaChance,
compressionEnabled: compressionEnabled,
cacheTTL: TTL,
ctx: context.Background(),
watcher: watcher,
}
}
func newCacheClusterRedis(layerName string, masterAddr string, slaveAddrs []string, db int, TTL time.Duration, redisIdleTimeout time.Duration, amnesiaChance int, compressionEnabled bool, watcher ITimer) *cache {
slaveClients := make([]*redis.Client, len(slaveAddrs))
for i, addr := range slaveAddrs {
redisOptions := &redis.Options{
Addr: addr,
DB: db,
}
if redisIdleTimeout >= time.Second {
redisOptions.IdleTimeout = redisIdleTimeout
}
slaveClients[i] = redis.NewClient(redisOptions)
}
redisOptions := &redis.Options{
Addr: masterAddr,
DB: db,
}
redisClient := redis.NewClient(redisOptions)
if err := redisClient.Ping().Err(); err != nil {
logrus.WithError(err).Error("error while connecting to Redis Master")
}
return &cache{
layerName: layerName,
baseRedisClient: redisClient,
slaveRedisClients: slaveClients,
amnesiaChance: amnesiaChance,
compressionEnabled: compressionEnabled,
cacheTTL: TTL,
ctx: context.Background(),
watcher: watcher,
}
}
func newCacheInMem(layerName string, maxMem int, TTL time.Duration, amnesiaChance int, compressionEnabled bool) *cache {
opts := bigcache.Config{
Shards: 1024,
LifeWindow: TTL,
MaxEntriesInWindow: 1100 * 10 * 60,
MaxEntrySize: 500,
Verbose: false,
HardMaxCacheSize: maxMem,
CleanWindow: 1 * time.Minute,
}
cacheInstance, err := bigcache.NewBigCache(opts)
if err != nil {
logrus.Errorf("InMemCache Error: %v", err)
}
return &cache{
layerName: layerName,
inMemCache: cacheInstance,
amnesiaChance: amnesiaChance,
compressionEnabled: compressionEnabled,
cacheTTL: TTL,
ctx: context.Background(),
}
}
func newCacheTiny(layerName string, amnesiaChance int, compressionEnabled bool) *cache {
data := sync.Map{}
return &cache{
layerName: layerName,
syncmap: &data,
amnesiaChance: amnesiaChance,
compressionEnabled: compressionEnabled,
cacheTTL: time.Hour * 9999,
ctx: context.Background(),
}
}
func (cr *cache) withContext(ctx context.Context) *cache {
return &cache{
layerName: cr.layerName,
baseRedisClient: cr.baseRedisClient,
slaveRedisClients: cr.slaveRedisClients,
inMemCache: cr.inMemCache,
syncmap: cr.syncmap,
amnesiaChance: cr.amnesiaChance,
compressionEnabled: cr.compressionEnabled,
cacheTTL: cr.cacheTTL,
ctx: ctx,
watcher: cr.watcher,
}
}
func (cr *cache) get(key string) (*cachableRet, error) {
if cr.amnesiaChance > rand.Intn(100) {
return nil, errors.New("Had Amnesia")
}
var rawBytes []byte
var err error
if cr.syncmap != nil {
val, ok := cr.syncmap.Load(key)
if !ok {
err = errors.New("Failed to load from syncmap")
} else {
rawBytes, ok = val.([]byte)
if !ok {
err = errors.New("Failed to load from syncmap")
}
}
} else if cr.inMemCache != nil {
rawBytes, err = cr.inMemCache.Get(key)
} else {
var strValue string
client := cr.pickClient().WithContext(cr.ctx)
startMarker := cr.watcher.Start()
strValue, err = client.Get(key).Result()
if err == nil {
cr.watcher.Done(startMarker, cr.layerName, "get", "ok")
} else if err == redis.Nil {
cr.watcher.Done(startMarker, cr.layerName, "get", "miss")
} else {
cr.watcher.Done(startMarker, cr.layerName, "get", "error")
}
rawBytes = []byte(strValue)
}
if err != nil {
return nil, err
}
var finalBytes []byte
if cr.compressionEnabled {
finalBytes = DecompressZlib(rawBytes)
} else {
finalBytes = rawBytes
}
var finalObject cachableRet
unmarshalErr := json.Unmarshal(finalBytes, &finalObject)
if unmarshalErr != nil {
return nil, fmt.Errorf("failed to unmarshall cached value : %v", unmarshalErr)
}
return &finalObject, nil
}
func (cr *cache) set(key string, value interface{}) (setError error) {
if cr.amnesiaChance == 100 {
return errors.New("Had Amnesia")
}
defer func() {
if r := recover(); r != nil {
//json.Marshal panics under heavy-load which is not repeated with the same values
setError = fmt.Errorf("panic in cache-set: %v", r)
}
}()
rawData, err := json.Marshal(value)
if err != nil {
return err
}
var finalData []byte
if cr.compressionEnabled {
finalData = CompressZlib(rawData)
} else {
finalData = rawData
}
if cr.syncmap != nil {
cr.syncmap.Store(key, finalData)
return nil
} else if cr.inMemCache != nil {
return cr.inMemCache.Set(key, finalData)
}
client := cr.baseRedisClient.WithContext(cr.ctx)
startMarker := cr.watcher.Start()
setError = client.Set(key, finalData, cr.cacheTTL).Err()
if setError != nil {
cr.watcher.Done(startMarker, cr.layerName, "set", "error")
} else {
cr.watcher.Done(startMarker, cr.layerName, "set", "ok")
}
return
}
func (cr *cache) delete(ctx context.Context, key string) error {
if cr.amnesiaChance == 100 {
return errors.New("Had Amnesia")
}
if cr.syncmap != nil {
cr.syncmap.Delete(key)
} else if cr.inMemCache != nil {
return cr.inMemCache.Delete(key)
}
client := cr.baseRedisClient.WithContext(ctx)
err := client.Del(key).Err()
return err
}
func (cr *cache) clear() error {
if cr.amnesiaChance == 100 {
return errors.New("Had Amnesia")
}
if cr.syncmap != nil {
cr.syncmap = &sync.Map{}
} else if cr.inMemCache != nil {
return cr.inMemCache.Reset()
}
client := cr.baseRedisClient
err := client.FlushDB().Err()
return err
}
func (cr *cache) getTTL(key string) time.Duration {
if cr.inMemCache != nil || cr.syncmap != nil {
return time.Second * 0
}
client := cr.pickClient().WithContext(cr.ctx)
res, err := client.TTL(key).Result()
if err != nil {
return time.Second * 0
}
return res
}
func (cr *cache) pickClient() *redis.Client {
if len(cr.slaveRedisClients) == 0 {
return cr.baseRedisClient
}
cl := rand.Intn(len(cr.slaveRedisClients) + 1)
if cl == 0 {
return cr.baseRedisClient
}
return cr.slaveRedisClients[cl-1]
}