-
Notifications
You must be signed in to change notification settings - Fork 1
/
redisBackend.go
364 lines (292 loc) · 8.35 KB
/
redisBackend.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package httpcache
import (
"bytes"
"encoding/gob"
"errors"
"fmt"
"regexp"
"runtime"
"time"
"flamingo.me/flamingo/v3/core/healthcheck/domain/healthcheck"
"github.com/gomodule/redigo/redis"
"flamingo.me/flamingo/v3/framework/flamingo"
)
type (
// RedisBackend implements the cache backend interface with a redis solution
RedisBackend struct {
cacheMetrics Metrics
pool *redis.Pool
logger flamingo.Logger
}
// RedisBackendFactory creates fully configured instances of Redis
RedisBackendFactory struct {
logger flamingo.Logger
frontendName string
pool *redis.Pool
config *RedisBackendConfig
}
// RedisBackendConfig holds the configuration values
RedisBackendConfig struct {
MaxIdle int
IdleTimeOutSeconds int
Host string
Port string
}
)
const (
tagPrefix = "tag:"
valuePrefix = "value:"
)
var (
_ Backend = new(RedisBackend)
_ healthcheck.Status = new(RedisBackend)
redisKeyRegex = regexp.MustCompile(`[^a-zA-Z0-9]`)
ErrInvalidRedisConfig = errors.New("invalid redis config")
)
func init() {
gob.Register(Entry{})
}
func finalizer(b *RedisBackend) {
b.close()
}
// Inject Redis dependencies
func (f *RedisBackendFactory) Inject(logger flamingo.Logger) *RedisBackendFactory {
f.logger = logger
return f
}
// Build a new redis backend
func (f *RedisBackendFactory) Build() (Backend, error) {
if f.config != nil {
if f.config.IdleTimeOutSeconds <= 0 {
return nil, fmt.Errorf("IdleTimeOut must be >0: %w", ErrInvalidRedisConfig)
}
if f.config.Host == "" || f.config.Port == "" {
return nil, fmt.Errorf("host and port must set: %w", ErrInvalidRedisConfig)
}
f.pool = &redis.Pool{
MaxIdle: f.config.MaxIdle,
IdleTimeout: time.Second * time.Duration(f.config.IdleTimeOutSeconds),
TestOnBorrow: func(c redis.Conn, t time.Time) error {
_, err := c.Do("PING")
return fmt.Errorf("redis PING failed: %w", err)
},
Dial: func() (redis.Conn, error) {
return f.redisConnector(
"tcp",
fmt.Sprintf("%v:%v", f.config.Host, f.config.Port),
"",
0,
)
},
}
}
redisBackend := &RedisBackend{
pool: f.pool,
logger: f.logger.WithField(flamingo.LogKeyCategory, "Redis"),
cacheMetrics: NewCacheMetrics("redis", f.frontendName),
}
runtime.SetFinalizer(redisBackend, finalizer) // close all connections on destruction
return redisBackend, nil
}
// SetFrontendName for redis cache metrics
func (f *RedisBackendFactory) SetFrontendName(frontendName string) *RedisBackendFactory {
f.frontendName = frontendName
return f
}
// SetConfig for redis
func (f *RedisBackendFactory) SetConfig(config RedisBackendConfig) *RedisBackendFactory {
f.config = &config
return f
}
// SetPool directly - use instead of SetConfig if desired
func (f *RedisBackendFactory) SetPool(pool *redis.Pool) *RedisBackendFactory {
f.pool = pool
return f
}
func (f *RedisBackendFactory) redisConnector(network, address, password string, database int) (redis.Conn, error) {
conn, err := redis.Dial(network, address)
if err != nil {
return nil, fmt.Errorf("redis dial error: %w", err)
}
if password != "" {
if _, err := conn.Do("AUTH", password); err != nil {
_ = conn.Close()
return nil, fmt.Errorf("redis auth error: %w", err)
}
}
if database != 0 {
if _, err := conn.Do("SELECT", database); err != nil {
_ = conn.Close()
return nil, fmt.Errorf("redis select db error: %w", err)
}
}
return conn, nil
}
// Close ensures all redis connections are closed
func (b *RedisBackend) close() {
_ = b.pool.Close()
}
// createPrefixedKey creates a redis-compatible key
func (b *RedisBackend) createPrefixedKey(key string, prefix string) string {
key = redisKeyRegex.ReplaceAllString(key, "-")
return fmt.Sprintf("%v%v", prefix, key)
}
// Get a cache key
func (b *RedisBackend) Get(key string) (entry Entry, found bool) {
conn := b.pool.Get()
defer func(conn redis.Conn) {
_ = conn.Close()
}(conn)
reply, err := conn.Do("GET", b.createPrefixedKey(key, valuePrefix))
if err != nil {
b.cacheMetrics.countError(fmt.Sprintf("%v", err))
b.logger.Error(fmt.Sprintf("Error getting key '%v': %v", key, err))
return Entry{}, false
}
if reply == nil {
b.cacheMetrics.countMiss()
return Entry{}, false
}
value, err := redis.Bytes(reply, err)
if err != nil {
b.cacheMetrics.countError("ByteConvertFailed")
b.logger.Error(fmt.Sprintf("Error convert value to bytes of key '%v': %v", key, err))
return Entry{}, false
}
redisEntry, err := b.decodeEntry(value)
if err != nil {
b.cacheMetrics.countError("DecodeFailed")
b.logger.Error(fmt.Sprintf("Error decoding content of key '%v': %v", key, err))
return Entry{}, false
}
b.cacheMetrics.countHit()
return redisEntry, true
}
// Set a cache key
func (b *RedisBackend) Set(key string, entry Entry) error {
conn := b.pool.Get()
defer func(conn redis.Conn) {
_ = conn.Close()
}(conn)
buffer, err := b.encodeEntry(entry)
if err != nil {
b.cacheMetrics.countError("EncodeFailed")
b.logger.Error(fmt.Sprintf("Error encoding entry for key: %q", key))
return err
}
err = conn.Send(
"SET",
b.createPrefixedKey(key, valuePrefix),
buffer,
"PX",
time.Until(entry.Meta.GraceTime).Round(time.Millisecond).Milliseconds(),
)
if err != nil {
b.cacheMetrics.countError("SetFailed")
b.logger.Error(fmt.Sprintf("Error setting key %q with timeout %v and buffer %v", key, entry.Meta.GraceTime, buffer))
return fmt.Errorf("redis SET PX failed: %w", err)
}
for _, tag := range entry.Meta.Tags {
err = conn.Send(
"SADD",
b.createPrefixedKey(tag, tagPrefix),
b.createPrefixedKey(key, valuePrefix),
)
if err != nil {
b.cacheMetrics.countError("SetTagFailed")
b.logger.Error(fmt.Sprintf("Error setting tag: %q on key %q", tag, key))
return fmt.Errorf("redis SADD failed: %w", err)
}
}
err = conn.Flush()
if err != nil {
return fmt.Errorf("redis flush failed: %w", err)
}
return nil
}
// Purge a cache key
func (b *RedisBackend) Purge(key string) error {
conn := b.pool.Get()
defer func(conn redis.Conn) {
_ = conn.Close()
}(conn)
_, err := conn.Do("DEL", b.createPrefixedKey(key, valuePrefix))
if err != nil {
return fmt.Errorf("redis DEL failed: %w", err)
}
return nil
}
// PurgeTags purges all keys+tags by tag(s)
func (b *RedisBackend) PurgeTags(tags []string) error {
conn := b.pool.Get()
defer func(conn redis.Conn) {
_ = conn.Close()
}(conn)
for _, tag := range tags {
reply, err := conn.Do("SMEMBERS", b.createPrefixedKey(tag, tagPrefix))
members, err := redis.Strings(reply, err)
if err != nil {
b.logger.Error(fmt.Sprintf("Failed SMEMBERS for tag '%v': %v", tag, err))
}
for _, member := range members {
_, err = conn.Do("DEL", member)
if err != nil {
b.logger.Error(fmt.Sprintf("Failed DEL for key '%v': %v", member, err))
return fmt.Errorf("redis DEL failed for key %q: %w", member, err)
}
}
_, err = conn.Do("DEL", fmt.Sprintf("%v", tag))
if err != nil {
b.logger.Error(fmt.Sprintf("Failed DEL for key '%v': %v", tag, err))
return fmt.Errorf("redis DEL failed for key %q: %w", tag, err)
}
}
err := conn.Flush()
if err != nil {
return fmt.Errorf("redis flush failed: %w", err)
}
return nil
}
// Flush the whole cache
func (b *RedisBackend) Flush() error {
conn := b.pool.Get()
defer func(conn redis.Conn) {
_ = conn.Close()
}(conn)
err := conn.Send("FLUSHALL")
if err != nil {
b.logger.Error(fmt.Sprintf("Failed purge all keys %v", err))
return fmt.Errorf("redis FLUSHALL failed: %w", err)
}
err = conn.Flush()
if err != nil {
return fmt.Errorf("redis flush failed: %w", err)
}
return nil
}
func (b *RedisBackend) encodeEntry(entry Entry) (*bytes.Buffer, error) {
buffer := new(bytes.Buffer)
err := gob.NewEncoder(buffer).Encode(entry)
if err != nil {
return nil, fmt.Errorf("gob encode failed: %w", err)
}
return buffer, nil
}
func (b *RedisBackend) decodeEntry(content []byte) (Entry, error) {
buffer := bytes.NewBuffer(content)
decoder := gob.NewDecoder(buffer)
entry := new(Entry)
err := decoder.Decode(entry)
if err != nil {
return *entry, fmt.Errorf("gob decode failed: %w", err)
}
return *entry, nil
}
// Status checks the health of the used redis instance
func (b *RedisBackend) Status() (bool, string) {
_, err := b.pool.Get().Do("PING")
if err != nil {
return false, fmt.Sprintf("redis PING failed: %q", err.Error())
}
return true, ""
}