-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredisList.go
128 lines (98 loc) · 3.61 KB
/
redisList.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
package redis
import (
"context"
"strings"
"time"
"github.com/farseer-go/fs/parse"
"github.com/go-redis/redis/v8"
)
type redisList struct {
*redisManager
}
func (receiver *redisList) ListPushRight(key string, values ...any) (bool, error) {
traceDetail := receiver.traceManager.TraceRedis("ListPushRight", key, "")
result, err := receiver.GetClient().RPush(context.Background(), key, values).Result()
defer func() { traceDetail.End(err) }()
if err == nil {
traceDetail.SetRows(parse.ToInt(result))
}
return result > 0, err
}
func (receiver *redisList) ListPushLeft(key string, values ...any) (bool, error) {
traceDetail := receiver.traceManager.TraceRedis("ListPushLeft", key, "")
result, err := receiver.GetClient().LPush(context.Background(), key, values).Result()
defer func() { traceDetail.End(err) }()
if err == nil {
traceDetail.SetRows(parse.ToInt(result))
}
return result > 0, err
}
func (receiver *redisList) ListSet(key string, index int64, value any) (bool, error) {
traceDetail := receiver.traceManager.TraceRedis("ListSet", key, "")
result, err := receiver.GetClient().LSet(context.Background(), key, index, value).Result()
defer func() { traceDetail.End(err) }()
if err == nil {
traceDetail.SetRows(1)
}
return result == "OK", err
}
func (receiver *redisList) ListRemove(key string, count int64, value any) (bool, error) {
traceDetail := receiver.traceManager.TraceRedis("ListRemove", key, "")
result, err := receiver.GetClient().LRem(context.Background(), key, count, value).Result()
defer func() { traceDetail.End(err) }()
if err == nil {
traceDetail.SetRows(parse.ToInt(result))
}
return result > 0, err
}
func (receiver *redisList) ListCount(key string) (int64, error) {
traceDetail := receiver.traceManager.TraceRedis("ListCount", key, "")
result, err := receiver.GetClient().LLen(context.Background(), key).Result()
if err == redis.Nil {
err = nil
}
defer func() { traceDetail.End(err) }()
if err == nil {
traceDetail.SetRows(parse.ToInt(result))
}
return result, err
}
func (receiver *redisList) ListRange(key string, start int64, stop int64) ([]string, error) {
traceDetail := receiver.traceManager.TraceRedis("ListRange", key, "")
result, err := receiver.GetClient().LRange(context.Background(), key, start, stop).Result()
if err == redis.Nil {
err = nil
}
defer func() { traceDetail.End(err) }()
if err == nil {
traceDetail.SetRows(len(result))
}
return result, err
}
func (receiver *redisList) ListLeftPop(timeout time.Duration, keys ...string) ([]string, error) {
traceDetail := receiver.traceManager.TraceRedis("ListLeftPop", strings.Join(keys, ","), "")
result, err := receiver.GetClient().BLPop(context.Background(), timeout, keys...).Result()
defer func() { traceDetail.End(err) }()
if err == nil {
traceDetail.SetRows(len(result))
}
return result, err
}
func (receiver *redisList) ListRightPop(timeout time.Duration, keys ...string) ([]string, error) {
traceDetail := receiver.traceManager.TraceRedis("ListRightPop", strings.Join(keys, ","), "")
result, err := receiver.GetClient().BRPop(context.Background(), timeout, keys...).Result()
defer func() { traceDetail.End(err) }()
if err == nil {
traceDetail.SetRows(len(result))
}
return result, err
}
func (receiver *redisList) ListRightPopPush(source, destination string, timeout time.Duration) (string, error) {
traceDetail := receiver.traceManager.TraceRedis("ListRightPopPush", source, "")
result, err := receiver.GetClient().BRPopLPush(context.Background(), source, destination, timeout).Result()
defer func() { traceDetail.End(err) }()
if err == nil {
traceDetail.SetRows(1)
}
return result, err
}