forked from xuyu/goredis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsets.go
206 lines (189 loc) · 6.59 KB
/
sets.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
package goredis
import (
"strconv"
)
// SAdd add the specified members to the set stored at key.
// Specified members that are already a member of this set are ignored.
// If key does not exist, a new set is created before adding the specified members.
// An error is returned when the value stored at key is not a set.
//
// Integer reply: the number of elements that were added to the set,
// not including all the elements already present into the set.
func (r *Redis) SAdd(key string, members ...string) (int64, error) {
args := packArgs("SADD", key, members)
rp, err := r.ExecuteCommand(args...)
if err != nil {
return 0, err
}
return rp.IntegerValue()
}
// SCard returns the set cardinality (number of elements) of the set stored at key.
func (r *Redis) SCard(key string) (int64, error) {
rp, err := r.ExecuteCommand("SCARD", key)
if err != nil {
return 0, err
}
return rp.IntegerValue()
}
// SDiff returns the members of the set resulting from the difference
// between the first set and all the successive sets.
// Keys that do not exist are considered to be empty sets.
// Multi-bulk reply: list with members of the resulting set.
func (r *Redis) SDiff(keys ...string) ([]string, error) {
args := packArgs("SDIFF", keys)
rp, err := r.ExecuteCommand(args...)
if err != nil {
return nil, err
}
return rp.ListValue()
}
// SDiffStore is equal to SDIFF, but instead of returning the resulting set,
// it is stored in destination.
// If destination already exists, it is overwritten.
// Integer reply: the number of elements in the resulting set.
func (r *Redis) SDiffStore(destination string, keys ...string) (int64, error) {
args := packArgs("SDIFFSTORE", destination, keys)
rp, err := r.ExecuteCommand(args...)
if err != nil {
return 0, err
}
return rp.IntegerValue()
}
// SInter returns the members of the set resulting from the intersection of all the given sets.
// Multi-bulk reply: list with members of the resulting set.
func (r *Redis) SInter(keys ...string) ([]string, error) {
args := packArgs("SINTER", keys)
rp, err := r.ExecuteCommand(args...)
if err != nil {
return nil, err
}
return rp.ListValue()
}
// SInterStore is equal to SINTER, but instead of returning the resulting set,
// it is stored in destination.
// If destination already exists, it is overwritten.
// Integer reply: the number of elements in the resulting set.
func (r *Redis) SInterStore(destination string, keys ...string) (int64, error) {
args := packArgs("SINTERSTORE", destination, keys)
rp, err := r.ExecuteCommand(args...)
if err != nil {
return 0, err
}
return rp.IntegerValue()
}
// SIsMember returns if member is a member of the set stored at key.
func (r *Redis) SIsMember(key, member string) (bool, error) {
rp, err := r.ExecuteCommand("SISMEMBER", key, member)
if err != nil {
return false, err
}
return rp.BoolValue()
}
// SMembers returns all the members of the set value stored at key.
func (r *Redis) SMembers(key string) ([]string, error) {
rp, err := r.ExecuteCommand("SMEMBERS", key)
if err != nil {
return nil, err
}
return rp.ListValue()
}
// SMove moves member from the set at source to the set at destination.
// This operation is atomic.
// In every given moment the element will appear to be a member of source or destination for other clients.
func (r *Redis) SMove(source, destination, member string) (bool, error) {
rp, err := r.ExecuteCommand("SMOVE", source, destination, member)
if err != nil {
return false, err
}
return rp.BoolValue()
}
// SPop removes and returns a random element from the set value stored at key.
// Bulk reply: the removed element, or nil when key does not exist.
func (r *Redis) SPop(key string) ([]byte, error) {
rp, err := r.ExecuteCommand("SPOP", key)
if err != nil {
return nil, err
}
return rp.BytesValue()
}
// SRandMember returns a random element from the set value stored at key.
// Bulk reply: the command returns a Bulk Reply with the randomly selected element,
// or nil when key does not exist.
func (r *Redis) SRandMember(key string) ([]byte, error) {
rp, err := r.ExecuteCommand("SRANDMEMBER", key)
if err != nil {
return nil, err
}
return rp.BytesValue()
}
// SRandMemberCount returns an array of count distinct elements if count is positive.
// If called with a negative count the behavior changes and the command
// is allowed to return the same element multiple times.
// In this case the numer of returned elements is the absolute value of the specified count.
// returns an array of elements, or an empty array when key does not exist.
func (r *Redis) SRandMemberCount(key string, count int) ([]string, error) {
rp, err := r.ExecuteCommand("SRANDMEMBER", key, count)
if err != nil {
return nil, err
}
return rp.ListValue()
}
// SRem remove the specified members from the set stored at key.
// Specified members that are not a member of this set are ignored.
// If key does not exist, it is treated as an empty set and this command returns 0.
// An error is returned when the value stored at key is not a set.
// Integer reply: the number of members that were removed from the set,
// not including non existing members.
func (r *Redis) SRem(key string, members ...string) (int64, error) {
args := packArgs("SREM", key, members)
rp, err := r.ExecuteCommand(args...)
if err != nil {
return 0, err
}
return rp.IntegerValue()
}
// SUnion returns the members of the set resulting from the union of all the given sets.
// Multi-bulk reply: list with members of the resulting set.
func (r *Redis) SUnion(keys ...string) ([]string, error) {
args := packArgs("SUNION", keys)
rp, err := r.ExecuteCommand(args...)
if err != nil {
return nil, err
}
return rp.ListValue()
}
// SUnionStore is equal to SUnion.
// If destination already exists, it is overwritten.
// Integer reply: the number of elements in the resulting set.
func (r *Redis) SUnionStore(destination string, keys ...string) (int64, error) {
args := packArgs("SUNIONSTORE", destination, keys)
rp, err := r.ExecuteCommand(args...)
if err != nil {
return 0, err
}
return rp.IntegerValue()
}
// SScan key cursor [MATCH pattern] [COUNT count]
func (r *Redis) SScan(key string, cursor uint64, pattern string, count int) (uint64, []string, error) {
args := packArgs("SSCAN", key, cursor)
if pattern != "" {
args = append(args, "MATCH", pattern)
}
if count > 0 {
args = append(args, "COUNT", count)
}
rp, err := r.ExecuteCommand(args...)
if err != nil {
return 0, nil, err
}
first, err := rp.Multi[0].StringValue()
if err != nil {
return 0, nil, err
}
next, err := strconv.ParseUint(first, 10, 64)
if err != nil {
return 0, nil, err
}
list, err := rp.Multi[1].ListValue()
return next, list, err
}