-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis.go
224 lines (190 loc) · 7.18 KB
/
redis.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
// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm)
// Source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
package redis // import "aahframe.work/cache/provider/redis"
import (
"bytes"
"encoding/gob"
"fmt"
"runtime"
"strings"
"sync"
"time"
"aahframe.work/cache"
"aahframe.work/config"
"aahframe.work/log"
"github.com/go-redis/redis"
)
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Provider and its exported methods
//______________________________________________________________________________
// Provider struct represents the Redis cache provider.
type Provider struct {
name string
logger log.Loggerer
cfg *cache.Config
appCfg *config.Config
client *redis.Client
clientOpts *redis.Options
}
var _ cache.Provider = (*Provider)(nil)
// Init method initializes the Redis cache provider.
func (p *Provider) Init(providerName string, appCfg *config.Config, logger log.Loggerer) error {
p.name = providerName
p.appCfg = appCfg
p.logger = logger
cfgPrefix := "cache." + p.name + "."
if strings.ToLower(p.appCfg.StringDefault(cfgPrefix+"provider", "")) != "redis" {
return fmt.Errorf("aah/cache: not a vaild provider name, expected 'redis'")
}
p.clientOpts = &redis.Options{
Network: p.appCfg.StringDefault(cfgPrefix+"network", "tcp"),
Addr: p.appCfg.StringDefault(cfgPrefix+"address", ":6379"),
Password: p.appCfg.StringDefault(cfgPrefix+"password", ""),
DB: p.appCfg.IntDefault(cfgPrefix+"db", 0),
PoolSize: p.appCfg.IntDefault(cfgPrefix+"pool_size", 10*runtime.NumCPU()),
DialTimeout: parseDuration(p.appCfg.StringDefault(cfgPrefix+"timeout.connect", "5s"), "5s"),
ReadTimeout: parseDuration(p.appCfg.StringDefault(cfgPrefix+"timeout.read", "3s"), "3s"),
WriteTimeout: parseDuration(p.appCfg.StringDefault(cfgPrefix+"timeout.write", "3s"), "3s"),
PoolTimeout: parseDuration(p.appCfg.StringDefault(cfgPrefix+"timeout.pool", "3s"), "3s"),
IdleTimeout: parseDuration(p.appCfg.StringDefault(cfgPrefix+"timeout.idle", "5m"), "5m"),
IdleCheckFrequency: parseDuration(p.appCfg.StringDefault(cfgPrefix+"idle_check_interval", "1m"), "1m"),
MinRetryBackoff: parseDuration(p.appCfg.StringDefault(cfgPrefix+"retry_backoff.min", "8ms"), "8ms"),
MaxRetryBackoff: parseDuration(p.appCfg.StringDefault(cfgPrefix+"retry_backoff.max", "512ms"), "512ms"),
}
p.client = redis.NewClient(p.clientOpts)
if _, err := p.client.Ping().Result(); err != nil {
return fmt.Errorf("aah/cache/%s: %s", p.name, err)
}
gob.Register(entry{})
p.logger.Infof("aah/cache/provider: %s connected successfully with %s", p.name, p.clientOpts.Addr)
return nil
}
// Create method creates new Redis cache with given options.
func (p *Provider) Create(cfg *cache.Config) (cache.Cache, error) {
p.cfg = cfg
r := &redisCache{
keyPrefix: p.cfg.Name + "-",
p: p,
}
return r, nil
}
// Client method returns underlying redis client. So that aah user could perform
// cache provider specific features.
func (p *Provider) Client() *redis.Client {
return p.client
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// redisCache struct implements `cache.Cache` interface.
//______________________________________________________________________________
type redisCache struct {
keyPrefix string
p *Provider
}
var _ cache.Cache = (*redisCache)(nil)
// Name method returns the cache store name.
func (r *redisCache) Name() string {
return r.p.cfg.Name
}
// Get method returns the cached entry for given key if it exists otherwise nil.
// Method uses `gob.Decoder` to unmarshal cache value from bytes.
func (r *redisCache) Get(k string) interface{} {
k = r.keyPrefix + k
v, err := r.p.client.Get(k).Bytes()
if err != nil {
if notacacheMiss(err) != nil {
r.p.logger.Errorf("aah/cache/%s: key(%s) %v", r.Name(), k[len(r.keyPrefix):], err)
}
return nil
}
var e entry
err = gob.NewDecoder(bytes.NewBuffer(v)).Decode(&e)
if err != nil {
r.p.logger.Errorf("aah/cache/%s: %v", r.Name(), err)
return nil
}
if r.p.cfg.EvictionMode == cache.EvictionModeSlide {
if err = r.p.client.Expire(k, e.D).Err(); err != nil {
r.p.logger.Errorf("aah/cache/%s: key(%s) %v", r.Name(), k[len(r.keyPrefix):], err)
}
}
return e.V
}
// GetOrPut method returns the cached entry for the given key if it exists otherwise
// it puts the new entry into cache store and returns the value.
func (r *redisCache) GetOrPut(k string, v interface{}, d time.Duration) (interface{}, error) {
ev := r.Get(k)
if ev == nil {
if err := r.Put(k, v, d); err != nil {
return nil, err
}
return v, nil
}
return ev, nil
}
// Put method adds the cache entry with specified expiration. Returns error
// if cache entry exists. Method uses `gob.Encoder` to marshal cache value into bytes.
func (r *redisCache) Put(k string, v interface{}, d time.Duration) error {
e := entry{D: d, V: v}
buf := acquireBuffer()
enc := gob.NewEncoder(buf)
if err := enc.Encode(e); err != nil {
return fmt.Errorf("aah/cache/%s: %v", r.Name(), err)
}
cmd := r.p.client.Set(r.keyPrefix+k, buf.Bytes(), d)
releaseBuffer(buf)
return cmd.Err()
}
// Delete method deletes the cache entry from cache store.
func (r *redisCache) Delete(k string) error {
if err := r.p.client.Del(r.keyPrefix + k).Err(); notacacheMiss(err) != nil {
return fmt.Errorf("aah/cache/%s: key(%s) %v", r.Name(), k, err)
}
return nil
}
// Exists method checks given key exists in cache store and its not expried.
func (r *redisCache) Exists(k string) bool {
result, err := r.p.client.Exists(r.keyPrefix + k).Result()
if err != nil {
r.p.logger.Errorf("aah/cache/%s: key(%s) %v", r.Name(), k, err)
return false
}
return result == 1
}
// Flush methods flushes(deletes) all the cache entries from cache.
func (r *redisCache) Flush() error {
if err := r.p.client.FlushDB().Err(); err != nil {
return fmt.Errorf("aah/cache/%s: %v", r.Name(), err)
}
return nil
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Helper methods
//______________________________________________________________________________
type entry struct {
D time.Duration
V interface{}
}
var bufPool = sync.Pool{New: func() interface{} { return new(bytes.Buffer) }}
func acquireBuffer() *bytes.Buffer {
return bufPool.Get().(*bytes.Buffer)
}
func releaseBuffer(b *bytes.Buffer) {
if b != nil {
b.Reset()
bufPool.Put(b)
}
}
func parseDuration(v, f string) time.Duration {
if d, err := time.ParseDuration(v); err == nil {
return d
}
d, _ := time.ParseDuration(f)
return d
}
func notacacheMiss(err error) error {
if err != nil && err.Error() == "redis: nil" {
return nil
}
return err
}