-
Notifications
You must be signed in to change notification settings - Fork 2
/
goredis_client.go
178 lines (135 loc) · 4.58 KB
/
goredis_client.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
package client
import (
"context"
"fmt"
"strconv"
"time"
"github.com/go-redis/redis/v8"
)
type GoRedisClient struct {
redis *redis.Client
sentinel *redis.SentinelClient
}
func NewFromConnectionString(connectionString string) (*GoRedisClient, error) {
var err error
c := &GoRedisClient{}
opt, err := redis.ParseURL(connectionString)
if err != nil {
return nil, err
}
// don't keep idle connections open
opt.MinIdleConns = 0
c.redis = redis.NewClient(opt)
c.sentinel = redis.NewSentinelClient(opt)
return c, nil
}
func MustNewFromConnectionString(connectionString string) *GoRedisClient {
c, err := NewFromConnectionString(connectionString)
if err != nil {
panic(err)
}
return c
}
func NewFromOptions(opt *redis.Options) *GoRedisClient {
return &GoRedisClient{
redis: redis.NewClient(opt),
sentinel: redis.NewSentinelClient(opt),
}
}
func (c *GoRedisClient) CloseRedis() error {
return c.redis.Close()
}
func (c *GoRedisClient) CloseSentinel() error {
return c.sentinel.Close()
}
func (c *GoRedisClient) Close() error {
var firstErr error
if err := c.CloseRedis(); err != nil {
firstErr = err
}
if err := c.CloseSentinel(); err != nil && firstErr == nil {
firstErr = err
}
return firstErr
}
func (c *GoRedisClient) SentinelMaster(ctx context.Context, shard string) (*SentinelMasterCmdResult, error) {
result := &SentinelMasterCmdResult{}
err := c.sentinel.Master(ctx, shard).Scan(result)
return result, err
}
func (c *GoRedisClient) SentinelGetMasterAddrByName(ctx context.Context, shard string) ([]string, error) {
values, err := c.sentinel.GetMasterAddrByName(ctx, shard).Result()
return values, err
}
func (c *GoRedisClient) SentinelMasters(ctx context.Context) ([]interface{}, error) {
values, err := c.sentinel.Masters(ctx).Result()
return values, err
}
func (c *GoRedisClient) SentinelSlaves(ctx context.Context, shard string) ([]interface{}, error) {
values, err := c.sentinel.Slaves(ctx, shard).Result()
return values, err
}
func (c *GoRedisClient) SentinelMonitor(ctx context.Context, name, host string, port string, quorum int) error {
_, err := c.sentinel.Monitor(ctx, name, host, port, strconv.Itoa(quorum)).Result()
return err
}
func (c *GoRedisClient) SentinelSet(ctx context.Context, shard, parameter, value string) error {
_, err := c.sentinel.Set(ctx, shard, parameter, value).Result()
return err
}
func (c *GoRedisClient) SentinelPSubscribe(ctx context.Context, events ...string) (<-chan *redis.Message, func() error) {
pubsub := c.sentinel.PSubscribe(ctx, events...)
return pubsub.Channel(), pubsub.Close
}
func (c *GoRedisClient) SentinelInfoCache(ctx context.Context) (interface{}, error) {
val, err := c.redis.Do(ctx, "sentinel", "info-cache").Result()
return val, err
}
func (c *GoRedisClient) SentinelPing(ctx context.Context) error {
_, err := c.sentinel.Ping(ctx).Result()
return err
}
func (c *GoRedisClient) SentinelDo(ctx context.Context, args ...interface{}) (interface{}, error) {
val, err := c.redis.Do(ctx, args...).Result()
return val, err
}
func (c *GoRedisClient) RedisRole(ctx context.Context) (interface{}, error) {
val, err := c.redis.Do(ctx, "role").Result()
return val, err
}
func (c *GoRedisClient) RedisConfigGet(ctx context.Context, parameter string) ([]interface{}, error) {
val, err := c.redis.ConfigGet(ctx, parameter).Result()
return val, err
}
func (c *GoRedisClient) RedisConfigSet(ctx context.Context, parameter, value string) error {
_, err := c.redis.ConfigSet(ctx, parameter, value).Result()
return err
}
func (c *GoRedisClient) RedisSlaveOf(ctx context.Context, host, port string) error {
_, err := c.redis.SlaveOf(ctx, host, port).Result()
return err
}
// WARNING: this command blocks for the duration
func (c *GoRedisClient) RedisDebugSleep(ctx context.Context, duration time.Duration) error {
_, err := c.redis.Do(ctx, "debug", "sleep", fmt.Sprintf("%.1f", duration.Seconds())).Result()
return err
}
func (c *GoRedisClient) RedisDo(ctx context.Context, args ...interface{}) (interface{}, error) {
val, err := c.redis.Do(ctx, args...).Result()
return val, err
}
func (c *GoRedisClient) RedisBGSave(ctx context.Context) error {
_, err := c.redis.BgSave(ctx).Result()
return err
}
func (c *GoRedisClient) RedisLastSave(ctx context.Context) (int64, error) {
return c.redis.LastSave(ctx).Result()
}
func (c *GoRedisClient) RedisSet(ctx context.Context, key string, value interface{}) error {
_, err := c.redis.Set(ctx, key, value, 0).Result()
return err
}
func (c *GoRedisClient) RedisInfo(ctx context.Context, section string) (string, error) {
val, err := c.redis.Info(ctx, section).Result()
return val, err
}