-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis.go
135 lines (121 loc) · 3.81 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
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/joy12825/gf.
// Package redis provides gredis.Adapter implements using go-redis.
package redis
import (
"context"
"time"
"github.com/joy12825/gf/container/gvar"
"github.com/joy12825/gf/database/gredis"
"github.com/joy12825/gf/errors/gerror"
"github.com/joy12825/gf/text/gstr"
)
// Redis is an implement of Adapter using go-redis.
type Redis struct {
client redis.UniversalClient
config *gredis.Config
}
const (
defaultPoolMaxIdle = 10
defaultPoolMaxActive = 100
defaultPoolIdleTimeout = 10 * time.Second
defaultPoolWaitTimeout = 10 * time.Second
defaultPoolMaxLifeTime = 30 * time.Second
defaultMaxRetries = -1
)
func init() {
gredis.RegisterAdapterFunc(func(config *gredis.Config) gredis.Adapter {
return New(config)
})
}
// New creates and returns a redis adapter using go-redis.
func New(config *gredis.Config) *Redis {
fillWithDefaultConfiguration(config)
opts := &redis.UniversalOptions{
Addrs: gstr.SplitAndTrim(config.Address, ","),
Password: config.Pass,
DB: config.Db,
MaxRetries: defaultMaxRetries,
PoolSize: config.MaxActive,
MinIdleConns: config.MinIdle,
MaxConnAge: config.MaxConnLifetime,
IdleTimeout: config.IdleTimeout,
PoolTimeout: config.WaitTimeout,
DialTimeout: config.DialTimeout,
ReadTimeout: config.ReadTimeout,
WriteTimeout: config.WriteTimeout,
MasterName: config.MasterName,
TLSConfig: config.TLSConfig,
}
var client redis.UniversalClient
if opts.MasterName != "" {
redisSentinel := opts.Failover()
redisSentinel.SlaveOnly = config.SlaveOnly
client = redis.NewFailoverClient(redisSentinel)
} else if len(opts.Addrs) > 1 {
client = redis.NewClusterClient(opts.Cluster())
} else {
client = redis.NewClient(opts.Simple())
}
return &Redis{
client: client,
config: config,
}
}
// Do send a command to the server and returns the received reply.
// It uses json.Marshal for struct/slice/map type values before committing them to redis.
func (r *Redis) Do(ctx context.Context, command string, args ...interface{}) (*gvar.Var, error) {
conn, err := r.Conn(ctx)
if err != nil {
return nil, err
}
defer func() {
_ = conn.Close(ctx)
}()
return conn.Do(ctx, command, args...)
}
// Close closes the redis connection pool, which will release all connections reserved by this pool.
// It is commonly not necessary to call Close manually.
func (r *Redis) Close(ctx context.Context) (err error) {
if err = r.client.Close(); err != nil {
err = gerror.Wrap(err, `Redis Client Close failed`)
}
return
}
// Conn retrieves and returns a connection object for continuous operations.
// Note that you should call Close function manually if you do not use this connection any further.
func (r *Redis) Conn(ctx context.Context) (gredis.Conn, error) {
return &Conn{
redis: r,
}, nil
}
func fillWithDefaultConfiguration(config *gredis.Config) {
// The MaxIdle is the most important attribute of the connection pool.
// Only if this attribute is set, the created connections from client
// can not exceed the limit of the server.
if config.MaxIdle == 0 {
config.MaxIdle = defaultPoolMaxIdle
}
// This value SHOULD NOT exceed the connection limit of redis server.
if config.MaxActive == 0 {
config.MaxActive = defaultPoolMaxActive
}
if config.IdleTimeout == 0 {
config.IdleTimeout = defaultPoolIdleTimeout
}
if config.WaitTimeout == 0 {
config.WaitTimeout = defaultPoolWaitTimeout
}
if config.MaxConnLifetime == 0 {
config.MaxConnLifetime = defaultPoolMaxLifeTime
}
if config.WriteTimeout == 0 {
config.WriteTimeout = -1
}
if config.ReadTimeout == 0 {
config.ReadTimeout = -1
}
}