-
Notifications
You must be signed in to change notification settings - Fork 159
/
helper.go
279 lines (245 loc) · 8.53 KB
/
helper.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package rueidis
import (
"context"
"errors"
"time"
intl "github.com/redis/rueidis/internal/cmds"
)
// MGetCache is a helper that consults the client-side caches with multiple keys by grouping keys within same slot into multiple GETs
func MGetCache(client Client, ctx context.Context, ttl time.Duration, keys []string) (ret map[string]RedisMessage, err error) {
if len(keys) == 0 {
return make(map[string]RedisMessage), nil
}
if isCacheDisabled(client) {
return MGet(client, ctx, keys)
}
cmds := mgetcachecmdsp.Get(len(keys), len(keys))
defer mgetcachecmdsp.Put(cmds)
for i := range cmds.s {
cmds.s[i] = CT(client.B().Get().Key(keys[i]).Cache(), ttl)
}
return doMultiCache(client, ctx, cmds.s, keys)
}
func isCacheDisabled(client Client) bool {
switch c := client.(type) {
case *singleClient:
return c.DisableCache
case *sentinelClient:
return c.mOpt != nil && c.mOpt.DisableCache
case *clusterClient:
return c.opt != nil && c.opt.DisableCache
}
return false
}
// MGet is a helper that consults the redis directly with multiple keys by grouping keys within same slot into MGET or multiple GETs
func MGet(client Client, ctx context.Context, keys []string) (ret map[string]RedisMessage, err error) {
if len(keys) == 0 {
return make(map[string]RedisMessage), nil
}
switch client.(type) {
case *singleClient, *sentinelClient:
return clientMGet(client, ctx, client.B().Mget().Key(keys...).Build(), keys)
}
cmds := mgetcmdsp.Get(len(keys), len(keys))
defer mgetcmdsp.Put(cmds)
for i := range cmds.s {
cmds.s[i] = client.B().Get().Key(keys[i]).Build()
}
return doMultiGet(client, ctx, cmds.s, keys)
}
// MSet is a helper that consults the redis directly with multiple keys by grouping keys within same slot into MSETs or multiple SETs
func MSet(client Client, ctx context.Context, kvs map[string]string) map[string]error {
if len(kvs) == 0 {
return make(map[string]error)
}
switch client.(type) {
case *singleClient, *sentinelClient:
return clientMSet(client, ctx, "MSET", kvs, make(map[string]error, len(kvs)))
}
cmds := mgetcmdsp.Get(0, len(kvs))
defer mgetcmdsp.Put(cmds)
for k, v := range kvs {
cmds.s = append(cmds.s, client.B().Set().Key(k).Value(v).Build().Pin())
}
return doMultiSet(client, ctx, cmds.s)
}
// MDel is a helper that consults the redis directly with multiple keys by grouping keys within same slot into DELs
func MDel(client Client, ctx context.Context, keys []string) map[string]error {
if len(keys) == 0 {
return make(map[string]error)
}
switch client.(type) {
case *singleClient, *sentinelClient:
return clientMDel(client, ctx, keys)
}
cmds := mgetcmdsp.Get(len(keys), len(keys))
defer mgetcmdsp.Put(cmds)
for i, k := range keys {
cmds.s[i] = client.B().Del().Key(k).Build().Pin()
}
return doMultiSet(client, ctx, cmds.s)
}
// MSetNX is a helper that consults the redis directly with multiple keys by grouping keys within same slot into MSETNXs or multiple SETNXs
func MSetNX(client Client, ctx context.Context, kvs map[string]string) map[string]error {
if len(kvs) == 0 {
return make(map[string]error)
}
switch client.(type) {
case *singleClient, *sentinelClient:
return clientMSet(client, ctx, "MSETNX", kvs, make(map[string]error, len(kvs)))
}
cmds := mgetcmdsp.Get(0, len(kvs))
defer mgetcmdsp.Put(cmds)
for k, v := range kvs {
cmds.s = append(cmds.s, client.B().Set().Key(k).Value(v).Nx().Build().Pin())
}
return doMultiSet(client, ctx, cmds.s)
}
// JsonMGetCache is a helper that consults the client-side caches with multiple keys by grouping keys within same slot into multiple JSON.GETs
func JsonMGetCache(client Client, ctx context.Context, ttl time.Duration, keys []string, path string) (ret map[string]RedisMessage, err error) {
if len(keys) == 0 {
return make(map[string]RedisMessage), nil
}
cmds := mgetcachecmdsp.Get(len(keys), len(keys))
defer mgetcachecmdsp.Put(cmds)
for i := range cmds.s {
cmds.s[i] = CT(client.B().JsonGet().Key(keys[i]).Path(path).Cache(), ttl)
}
return doMultiCache(client, ctx, cmds.s, keys)
}
// JsonMGet is a helper that consults redis directly with multiple keys by grouping keys within same slot into JSON.MGETs or multiple JSON.GETs
func JsonMGet(client Client, ctx context.Context, keys []string, path string) (ret map[string]RedisMessage, err error) {
if len(keys) == 0 {
return make(map[string]RedisMessage), nil
}
switch client.(type) {
case *singleClient, *sentinelClient:
return clientMGet(client, ctx, client.B().JsonMget().Key(keys...).Path(path).Build(), keys)
}
cmds := mgetcmdsp.Get(len(keys), len(keys))
defer mgetcmdsp.Put(cmds)
for i := range cmds.s {
cmds.s[i] = client.B().JsonGet().Key(keys[i]).Path(path).Build()
}
return doMultiGet(client, ctx, cmds.s, keys)
}
// JsonMSet is a helper that consults redis directly with multiple keys by grouping keys within same slot into JSON.MSETs or multiple JOSN.SETs
func JsonMSet(client Client, ctx context.Context, kvs map[string]string, path string) map[string]error {
if len(kvs) == 0 {
return make(map[string]error)
}
switch client.(type) {
case *singleClient, *sentinelClient:
return clientJSONMSet(client, ctx, kvs, path, make(map[string]error, len(kvs)))
}
cmds := mgetcmdsp.Get(0, len(kvs))
defer mgetcmdsp.Put(cmds)
for k, v := range kvs {
cmds.s = append(cmds.s, client.B().JsonSet().Key(k).Path(path).Value(v).Build().Pin())
}
return doMultiSet(client, ctx, cmds.s)
}
// DecodeSliceOfJSON is a helper that struct-scans each RedisMessage into dest, which must be a slice of pointer.
func DecodeSliceOfJSON[T any](result RedisResult, dest *[]T) error {
values, err := result.ToArray()
if err != nil {
return err
}
ts := make([]T, len(values))
for i, v := range values {
var t T
if err = v.DecodeJSON(&t); err != nil {
if IsRedisNil(err) {
continue
}
return err
}
ts[i] = t
}
*dest = ts
return nil
}
func clientMGet(client Client, ctx context.Context, cmd Completed, keys []string) (ret map[string]RedisMessage, err error) {
arr, err := client.Do(ctx, cmd).ToArray()
if err != nil {
return nil, err
}
return arrayToKV(make(map[string]RedisMessage, len(keys)), arr, keys), nil
}
func clientMSet(client Client, ctx context.Context, mset string, kvs map[string]string, ret map[string]error) map[string]error {
cmd := client.B().Arbitrary(mset)
for k, v := range kvs {
cmd = cmd.Args(k, v)
}
ok, err := client.Do(ctx, cmd.Build()).AsBool()
if err == nil && !ok {
err = ErrMSetNXNotSet
}
for k := range kvs {
ret[k] = err
}
return ret
}
func clientJSONMSet(client Client, ctx context.Context, kvs map[string]string, path string, ret map[string]error) map[string]error {
cmd := intl.JsonMsetTripletValue(client.B().JsonMset())
for k, v := range kvs {
cmd = cmd.Key(k).Path(path).Value(v)
}
err := client.Do(ctx, cmd.Build()).Error()
for k := range kvs {
ret[k] = err
}
return ret
}
func clientMDel(client Client, ctx context.Context, keys []string) map[string]error {
err := client.Do(ctx, client.B().Del().Key(keys...).Build()).Error()
ret := make(map[string]error, len(keys))
for _, k := range keys {
ret[k] = err
}
return ret
}
func doMultiCache(cc Client, ctx context.Context, cmds []CacheableTTL, keys []string) (ret map[string]RedisMessage, err error) {
ret = make(map[string]RedisMessage, len(keys))
resps := cc.DoMultiCache(ctx, cmds...)
defer resultsp.Put(&redisresults{s: resps})
for i, resp := range resps {
if err := resp.NonRedisError(); err != nil {
return nil, err
}
ret[keys[i]] = resp.val
}
return ret, nil
}
func doMultiGet(cc Client, ctx context.Context, cmds []Completed, keys []string) (ret map[string]RedisMessage, err error) {
ret = make(map[string]RedisMessage, len(keys))
resps := cc.DoMulti(ctx, cmds...)
defer resultsp.Put(&redisresults{s: resps})
for i, resp := range resps {
if err := resp.NonRedisError(); err != nil {
return nil, err
}
ret[keys[i]] = resp.val
}
return ret, nil
}
func doMultiSet(cc Client, ctx context.Context, cmds []Completed) (ret map[string]error) {
ret = make(map[string]error, len(cmds))
resps := cc.DoMulti(ctx, cmds...)
for i, resp := range resps {
if ret[cmds[i].Commands()[1]] = resp.Error(); resp.NonRedisError() == nil {
intl.PutCompletedForce(cmds[i])
}
}
resultsp.Put(&redisresults{s: resps})
return ret
}
func arrayToKV(m map[string]RedisMessage, arr []RedisMessage, keys []string) map[string]RedisMessage {
for i, resp := range arr {
m[keys[i]] = resp
}
return m
}
// ErrMSetNXNotSet is used in the MSetNX helper when the underlying MSETNX response is 0.
// Ref: https://redis.io/commands/msetnx/
var ErrMSetNXNotSet = errors.New("MSETNX: no key was set")