-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_cache_redis.go
171 lines (147 loc) · 5.16 KB
/
client_cache_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
package injectors
import (
"time"
"github.com/go-redis/redis/v8"
"github.com/go-redsync/redsync/v4"
redsyncredis "github.com/go-redsync/redsync/v4/redis"
"github.com/go-redsync/redsync/v4/redis/goredis/v8"
)
type RedisClusterClientOptions struct {
Addrs []string `mapstructure:"addrs"`
RedisClientCommonOptions
// To route commands by latency or randomly, enable one of the following.
Route string `mapstructure:"route"`
}
type RedisClientCommonOptions struct {
Password string `mapstructure:"password"`
Username string `mapstructure:"username"`
MaxRetries int `mapstructure:"maxRetries"`
MinRetryBackoff time.Duration `mapstructure:"minRetryBackoff"`
MaxRetryBackoff time.Duration `mapstructure:"maxRetryBackoff"`
DialTimeout time.Duration `mapstructure:"dialTimeout"`
ReadTimeout time.Duration `mapstructure:"readTimeout"`
WriteTimeout time.Duration `mapstructure:"writeTimeout"`
// connect pool
PoolSize int `mapstructure:"poolSize"`
MinIdleConns int `mapstructure:"minIdleConns"`
MaxConnAge time.Duration `mapstructure:"maxConnAge"`
PoolTimeout time.Duration `mapstructure:"poolTimeout"`
IdleTimeout time.Duration `mapstructure:"idleTimeout"`
IdleCheckFrequency time.Duration `mapstructure:"idleCheckFrequency"`
}
func DefaultRedisClusterClientOptions() *RedisClusterClientOptions {
return &RedisClusterClientOptions{
//Addrs: []string{":26379"},
Addrs: []string{":26379", ":26380", ":26381", ":26382", ":26383", ":26384"},
RedisClientCommonOptions: RedisClientCommonOptions{
Password: "",
Username: "",
MaxRetries: 3,
MinRetryBackoff: 3 * time.Second,
MaxRetryBackoff: 5 * time.Second,
DialTimeout: 5 * time.Second,
ReadTimeout: 3 * time.Second,
WriteTimeout: 3 * time.Second,
// connect pool
PoolSize: 100,
MinIdleConns: 10,
MaxConnAge: 60 * time.Second,
PoolTimeout: 5 * time.Second,
IdleTimeout: 30 * time.Second,
IdleCheckFrequency: 3 * time.Second,
},
Route: "randomly",
}
}
// InitRedisClusterDefaultClient init default redis cluster instance
func InitRedisClusterDefaultClient(opts *RedisClusterClientOptions) redis.UniversalClient {
clusterOpts := &redis.ClusterOptions{
Addrs: opts.Addrs,
Password: opts.Password,
Username: opts.Username,
MaxRetries: opts.MaxRetries,
MinRetryBackoff: opts.MinRetryBackoff,
MaxRetryBackoff: opts.MaxRetryBackoff,
DialTimeout: opts.DialTimeout,
ReadTimeout: opts.ReadTimeout,
WriteTimeout: opts.WriteTimeout,
// connect pool
PoolSize: opts.PoolSize,
MinIdleConns: opts.MinIdleConns,
MaxConnAge: opts.MaxConnAge,
PoolTimeout: opts.PoolTimeout,
IdleTimeout: opts.IdleTimeout,
IdleCheckFrequency: opts.IdleCheckFrequency,
// To route commands by latency or randomly, enable one of the following.
//RouteByLatency: true,
//RouteRandomly: true,
}
switch opts.Route {
case "randomly":
clusterOpts.RouteRandomly = true
case "latency":
clusterOpts.RouteByLatency = true
default:
clusterOpts.RouteRandomly = true
}
return redis.NewClusterClient(clusterOpts)
}
type RedisClientOptions struct {
Addr string `mapstructure:"addr"`
Db int `mapstructure:"db"`
RedisClientCommonOptions
}
func DefaultRedisClientOptions() *RedisClientOptions {
return &RedisClientOptions{
Addr: "localhost:6379",
RedisClientCommonOptions: RedisClientCommonOptions{
Password: "",
Username: "",
MaxRetries: 3,
MinRetryBackoff: 3 * time.Second,
MaxRetryBackoff: 5 * time.Second,
DialTimeout: 5 * time.Second,
ReadTimeout: 3 * time.Second,
WriteTimeout: 3 * time.Second,
// connect pool
PoolSize: 100,
MinIdleConns: 10,
MaxConnAge: 60 * time.Second,
PoolTimeout: 5 * time.Second,
IdleTimeout: 30 * time.Second,
IdleCheckFrequency: 3 * time.Second,
},
}
}
// InitRedisDefaultClient init default redis instance
func InitRedisDefaultClient(opts *RedisClientOptions, limiter redis.Limiter) redis.UniversalClient {
return redis.NewClient(&redis.Options{
Addr: opts.Addr,
Username: opts.Username,
Password: opts.Password,
DB: opts.Db,
MaxRetries: opts.MaxRetries,
MinRetryBackoff: opts.MinRetryBackoff,
MaxRetryBackoff: opts.MaxRetryBackoff,
DialTimeout: opts.DialTimeout,
ReadTimeout: opts.ReadTimeout,
WriteTimeout: opts.WriteTimeout,
PoolSize: opts.PoolSize,
MinIdleConns: opts.MinIdleConns,
MaxConnAge: opts.MaxConnAge,
PoolTimeout: opts.PoolTimeout,
IdleTimeout: opts.IdleTimeout,
IdleCheckFrequency: opts.IdleCheckFrequency,
//TLSConfig: &tls.Config{},
Limiter: limiter,
})
}
// InitRedsync Create an instance of redisync to be used to obtain a mutual exclusion for distlock
// ref: https://redis.io/topics/distlock/
func InitRedsync(clients ...redis.UniversalClient) *redsync.Redsync {
var pools []redsyncredis.Pool
for _, client := range clients {
pools = append(pools, goredis.NewPool(client))
}
return redsync.New(pools...)
}