-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclient_method.go
More file actions
75 lines (62 loc) · 1.9 KB
/
client_method.go
File metadata and controls
75 lines (62 loc) · 1.9 KB
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
package redis
import (
`context`
`github.com/go-redis/redis/v8`
)
func (c *Client) putValues(ctx context.Context, key string, pushType putValuesType, opts ...valuesOption) (affected int64, err error) {
_options := defaultValuesOptions()
for _, opt := range opts {
opt.applyValues(_options)
}
values := make([]interface{}, 0, len(_options.values))
for _, value := range _options.values {
var marshaled interface{}
if marshaled, err = c.marshal(value, _options.label, _options.serializer); nil != err {
return
}
values = append(values, marshaled)
}
client := c.getClient(_options.options)
switch pushType {
case putValuesTypeLPush:
affected, err = client.LPush(ctx, key, values...).Result()
case putValuesTypeRPush:
affected, err = client.RPush(ctx, key, values...).Result()
case putValuesTypeSAdd:
affected, err = client.SAdd(ctx, key, values...).Result()
}
return
}
func (c *Client) _range(ctx context.Context, key string, values interface{}, rangeType rangeType, opts ...rangeOption) (err error) {
_options := defaultRangeOptions()
for _, opt := range opts {
opt.applyRange(_options)
}
var results []string
client := c.getClient(_options.options)
switch rangeType {
case rangeTypeLRange:
results, err = client.LRange(ctx, key, _options.start, _options.stop).Result()
case rangeTypeZRange:
results, err = client.ZRange(ctx, key, _options.start, _options.stop).Result()
}
if nil == err {
err = c.unmarshalSlice(results, values, _options.label, _options.serializer)
}
return
}
func (c *Client) len(ctx context.Context, key string, lenType lenType, opts ...option) (int64, error) {
_options := defaultOptions()
for _, opt := range opts {
opt.apply(_options)
}
var redisCmd *redis.IntCmd
client := c.getClient(_options)
switch lenType {
case lenTypeLLen:
redisCmd = client.LLen(ctx, key)
case lenTypeZCard:
redisCmd = client.ZCard(ctx, key)
}
return redisCmd.Result()
}