@@ -13,6 +13,7 @@ import (
13
13
"context"
14
14
"fmt"
15
15
"strings"
16
+ "sync"
16
17
"time"
17
18
18
19
goredislib "github.com/go-redis/redis/v8"
@@ -31,17 +32,18 @@ var ctx = context.Background()
31
32
32
33
// RedisClient - Redis Client structure.
33
34
type RedisClient struct {
34
- * goredislib.Client
35
- * redsync.Redsync
36
- Name string
37
- Mutex map [string ]* redsync.Mutex
38
- logger * log.Logger
35
+ Client goredislib.UniversalClient
36
+ Redsync * redsync.Redsync
37
+ Name string
38
+ Mutex map [string ]* redsync.Mutex
39
+ logger * log.Logger
39
40
}
40
41
41
42
// Connect - Connects to DB.
42
43
func Connect (connName string , config config.Cache , logger * log.Logger ) * RedisClient {
43
- client := goredislib .NewClient (& goredislib.Options {
44
- Addr : config .Host + ":" + config .Port ,
44
+
45
+ client := goredislib .NewUniversalClient (& goredislib.UniversalOptions {
46
+ Addrs : config .Hosts ,
45
47
Password : config .Password ,
46
48
DB : config .DB ,
47
49
})
@@ -105,12 +107,26 @@ func (rdb *RedisClient) unlock(ctx context.Context, key string) error {
105
107
106
108
// PurgeAll - Purges all the existing keys on a DB.
107
109
func (rdb * RedisClient ) PurgeAll () (bool , error ) {
110
+ // multiple redis instances
111
+ if rdb .Client .ClusterSlots (ctx ).Err () == nil {
112
+ clusterClient := rdb .Client .(* goredislib.ClusterClient )
113
+ err := clusterClient .ForEachShard (ctx , func (ctx context.Context , client * goredislib.Client ) error {
114
+ return rdb .purgeAllKeys (client )
115
+ })
116
+ return err == nil , err
117
+ }
118
+
119
+ // single redis instance
120
+ err := rdb .purgeAllKeys (rdb .Client )
121
+ return err == nil , err
122
+ }
123
+
124
+ func (rdb * RedisClient ) purgeAllKeys (client goredislib.UniversalClient ) error {
108
125
_ , err := circuitbreaker .CB (rdb .Name , rdb .logger ).Execute (func () (interface {}, error ) {
109
- err := rdb . Client .FlushDB (ctx ).Err ()
126
+ err := client .FlushDB (ctx ).Err ()
110
127
return nil , err
111
128
})
112
-
113
- return err == nil , err
129
+ return err
114
130
}
115
131
116
132
// Ping - Tests the connection.
@@ -167,47 +183,15 @@ func (rdb *RedisClient) Del(ctx context.Context, key string) error {
167
183
return err
168
184
}
169
185
170
- // DelWildcard - Removes the matching keys based on a pattern.
171
- func (rdb * RedisClient ) DelWildcard (ctx context.Context , key string ) (int , error ) {
172
- k , err := circuitbreaker .CB (rdb .Name , rdb .logger ).Execute (func () (interface {}, error ) {
173
- keys , err := rdb .Client .Keys (ctx , key ).Result ()
174
- return keys , err
175
- })
176
-
177
- if err != nil {
178
- return 0 , nil
179
- }
180
-
181
- return rdb .deleteKeys (ctx , key , k .([]string ))
182
- }
183
-
184
- // DelWildcard - Removes the matching keys based on a pattern.
185
- func (rdb * RedisClient ) deleteKeys (ctx context.Context , keyID string , keys []string ) (int , error ) {
186
- l := len (keys )
187
-
188
- if l == 0 {
189
- return 0 , nil
190
- }
191
-
192
- _ , errDel := circuitbreaker .CB (rdb .Name , rdb .logger ).Execute (rdb .doDeleteKeys (ctx , keyID , keys ))
193
-
194
- return l , errDel
186
+ type Counter struct {
187
+ mu sync.Mutex
188
+ counter int
195
189
}
196
190
197
- func (rdb * RedisClient ) doDeleteKeys (ctx context.Context , keyID string , keys []string ) func () (interface {}, error ) {
198
- return func () (interface {}, error ) {
199
- if errLock := rdb .lock (ctx , keyID ); errLock != nil {
200
- return nil , errLock
201
- }
202
-
203
- err := rdb .Client .Del (ctx , keys ... ).Err ()
204
-
205
- if errUnlock := rdb .unlock (ctx , keyID ); errUnlock != nil {
206
- return nil , errUnlock
207
- }
208
-
209
- return nil , err
210
- }
191
+ func (c * Counter ) increment (num int ) {
192
+ c .mu .Lock ()
193
+ defer c .mu .Unlock ()
194
+ c .counter = c .counter + num
211
195
}
212
196
213
197
// List - Returns the values in a list.
0 commit comments