Skip to content

Commit f7cb3c7

Browse files
committed
Added documentation for explaining the methods responsibility
1 parent f711eb0 commit f7cb3c7

File tree

1 file changed

+116
-2
lines changed

1 file changed

+116
-2
lines changed

set_commands.go

Lines changed: 116 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"github.com/redis/go-redis/v9/internal/hashtag"
77
)
88

9+
// SetCmdable is an interface for Redis set commands.
10+
// Sets are unordered collections of unique strings.
911
type SetCmdable interface {
1012
SAdd(ctx context.Context, key string, members ...interface{}) *IntCmd
1113
SCard(ctx context.Context, key string) *IntCmd
@@ -29,8 +31,13 @@ type SetCmdable interface {
2931
SUnionStore(ctx context.Context, destination string, keys ...string) *IntCmd
3032
}
3133

32-
//------------------------------------------------------------------------------
33-
34+
// SAdd Redis `SADD key member [member ...]` command.
35+
// Adds the specified members to the set stored at key.
36+
// Specified members that are already members of this set are ignored.
37+
// If key does not exist, a new set is created before adding the specified members.
38+
//
39+
// Returns the number of elements that were added to the set, not including all
40+
// the elements already present in the set.
3441
func (c cmdable) SAdd(ctx context.Context, key string, members ...interface{}) *IntCmd {
3542
args := make([]interface{}, 2, 2+len(members))
3643
args[0] = "sadd"
@@ -41,12 +48,21 @@ func (c cmdable) SAdd(ctx context.Context, key string, members ...interface{}) *
4148
return cmd
4249
}
4350

51+
// SCard Redis `SCARD key` command.
52+
// Returns the set cardinality (number of elements) of the set stored at key.
53+
// Returns 0 if key does not exist.
4454
func (c cmdable) SCard(ctx context.Context, key string) *IntCmd {
4555
cmd := NewIntCmd(ctx, "scard", key)
4656
_ = c(ctx, cmd)
4757
return cmd
4858
}
4959

60+
// SDiff Redis `SDIFF key [key ...]` command.
61+
// Returns the members of the set resulting from the difference between the first set
62+
// and all the successive sets.
63+
// Keys that do not exist are considered to be empty sets.
64+
//
65+
// Returns a slice of members of the resulting set.
5066
func (c cmdable) SDiff(ctx context.Context, keys ...string) *StringSliceCmd {
5167
args := make([]interface{}, 1+len(keys))
5268
args[0] = "sdiff"
@@ -58,6 +74,12 @@ func (c cmdable) SDiff(ctx context.Context, keys ...string) *StringSliceCmd {
5874
return cmd
5975
}
6076

77+
// SDiffStore Redis `SDIFFSTORE destination key [key ...]` command.
78+
// Stores the members of the set resulting from the difference between the first set
79+
// and all the successive sets into destination.
80+
// If destination already exists, it is overwritten.
81+
//
82+
// Returns the number of elements in the resulting set.
6183
func (c cmdable) SDiffStore(ctx context.Context, destination string, keys ...string) *IntCmd {
6284
args := make([]interface{}, 2+len(keys))
6385
args[0] = "sdiffstore"
@@ -70,6 +92,12 @@ func (c cmdable) SDiffStore(ctx context.Context, destination string, keys ...str
7092
return cmd
7193
}
7294

95+
// SInter Redis `SINTER key [key ...]` command.
96+
// Returns the members of the set resulting from the intersection of all the given sets.
97+
// Keys that do not exist are considered to be empty sets.
98+
// With one of the keys being an empty set, the resulting set is also empty.
99+
//
100+
// Returns a slice of members of the resulting set.
73101
func (c cmdable) SInter(ctx context.Context, keys ...string) *StringSliceCmd {
74102
args := make([]interface{}, 1+len(keys))
75103
args[0] = "sinter"
@@ -81,6 +109,15 @@ func (c cmdable) SInter(ctx context.Context, keys ...string) *StringSliceCmd {
81109
return cmd
82110
}
83111

112+
// SInterCard Redis `SINTERCARD numkeys key [key ...] [LIMIT limit]` command.
113+
// Returns the cardinality of the set resulting from the intersection of all the given sets.
114+
// Keys that do not exist are considered to be empty sets.
115+
// With one of the keys being an empty set, the resulting set is also empty.
116+
//
117+
// The limit parameter sets an upper bound on the number of results returned.
118+
// If limit is 0, no limit is applied.
119+
//
120+
// Returns the number of elements in the resulting set.
84121
func (c cmdable) SInterCard(ctx context.Context, limit int64, keys ...string) *IntCmd {
85122
numKeys := len(keys)
86123
args := make([]interface{}, 4+numKeys)
@@ -96,6 +133,12 @@ func (c cmdable) SInterCard(ctx context.Context, limit int64, keys ...string) *I
96133
return cmd
97134
}
98135

136+
// SInterStore Redis `SINTERSTORE destination key [key ...]` command.
137+
// Stores the members of the set resulting from the intersection of all the given sets
138+
// into destination.
139+
// If destination already exists, it is overwritten.
140+
//
141+
// Returns the number of elements in the resulting set.
99142
func (c cmdable) SInterStore(ctx context.Context, destination string, keys ...string) *IntCmd {
100143
args := make([]interface{}, 2+len(keys))
101144
args[0] = "sinterstore"
@@ -108,13 +151,22 @@ func (c cmdable) SInterStore(ctx context.Context, destination string, keys ...st
108151
return cmd
109152
}
110153

154+
// SIsMember Redis `SISMEMBER key member` command.
155+
// Returns if member is a member of the set stored at key.
156+
// Returns true if the element is a member of the set, false if it is not a member
157+
// or if key does not exist.
111158
func (c cmdable) SIsMember(ctx context.Context, key string, member interface{}) *BoolCmd {
112159
cmd := NewBoolCmd(ctx, "sismember", key, member)
113160
_ = c(ctx, cmd)
114161
return cmd
115162
}
116163

117164
// SMIsMember Redis `SMISMEMBER key member [member ...]` command.
165+
// Returns whether each member is a member of the set stored at key.
166+
// For each member, returns true if the element is a member of the set, false if it is not
167+
// a member or if key does not exist.
168+
//
169+
// Returns a slice of booleans, one for each member, indicating membership.
118170
func (c cmdable) SMIsMember(ctx context.Context, key string, members ...interface{}) *BoolSliceCmd {
119171
args := make([]interface{}, 2, 2+len(members))
120172
args[0] = "smismember"
@@ -126,53 +178,93 @@ func (c cmdable) SMIsMember(ctx context.Context, key string, members ...interfac
126178
}
127179

128180
// SMembers Redis `SMEMBERS key` command output as a slice.
181+
// Returns all the members of the set value stored at key.
182+
// Returns an empty slice if key does not exist.
183+
//
184+
// Returns a slice of all members of the set.
129185
func (c cmdable) SMembers(ctx context.Context, key string) *StringSliceCmd {
130186
cmd := NewStringSliceCmd(ctx, "smembers", key)
131187
_ = c(ctx, cmd)
132188
return cmd
133189
}
134190

135191
// SMembersMap Redis `SMEMBERS key` command output as a map.
192+
// Returns all the members of the set value stored at key as a map.
193+
// Returns an empty map if key does not exist.
194+
//
195+
// Returns a map where keys are the set members and values are empty structs.
136196
func (c cmdable) SMembersMap(ctx context.Context, key string) *StringStructMapCmd {
137197
cmd := NewStringStructMapCmd(ctx, "smembers", key)
138198
_ = c(ctx, cmd)
139199
return cmd
140200
}
141201

202+
// SMove Redis `SMOVE source destination member` command.
203+
// Moves member from the set at source to the set at destination.
204+
// This operation is atomic. In every given moment the element will appear to be a member
205+
// of source or destination for other clients.
206+
//
207+
// Returns true if the element is moved, false if the element is not a member of source
208+
// and no operation was performed.
142209
func (c cmdable) SMove(ctx context.Context, source, destination string, member interface{}) *BoolCmd {
143210
cmd := NewBoolCmd(ctx, "smove", source, destination, member)
144211
_ = c(ctx, cmd)
145212
return cmd
146213
}
147214

148215
// SPop Redis `SPOP key` command.
216+
// Removes and returns one or more random members from the set value stored at key.
217+
// This version returns a single random member.
218+
//
219+
// Returns the removed member, or nil if key does not exist.
149220
func (c cmdable) SPop(ctx context.Context, key string) *StringCmd {
150221
cmd := NewStringCmd(ctx, "spop", key)
151222
_ = c(ctx, cmd)
152223
return cmd
153224
}
154225

155226
// SPopN Redis `SPOP key count` command.
227+
// Removes and returns one or more random members from the set value stored at key.
228+
// This version returns up to count random members.
229+
//
230+
// Returns a slice of removed members. If key does not exist, returns an empty slice.
156231
func (c cmdable) SPopN(ctx context.Context, key string, count int64) *StringSliceCmd {
157232
cmd := NewStringSliceCmd(ctx, "spop", key, count)
158233
_ = c(ctx, cmd)
159234
return cmd
160235
}
161236

162237
// SRandMember Redis `SRANDMEMBER key` command.
238+
// Returns a random member from the set value stored at key.
239+
// This version returns a single random member without removing it.
240+
//
241+
// Returns the random member, or nil if key does not exist or the set is empty.
163242
func (c cmdable) SRandMember(ctx context.Context, key string) *StringCmd {
164243
cmd := NewStringCmd(ctx, "srandmember", key)
165244
_ = c(ctx, cmd)
166245
return cmd
167246
}
168247

169248
// SRandMemberN Redis `SRANDMEMBER key count` command.
249+
// Returns an array of random members from the set value stored at key.
250+
// This version returns up to count random members without removing them.
251+
// When called with a positive count, returns distinct elements.
252+
// When called with a negative count, allows for repeated elements.
253+
//
254+
// Returns a slice of random members. If key does not exist, returns an empty slice.
170255
func (c cmdable) SRandMemberN(ctx context.Context, key string, count int64) *StringSliceCmd {
171256
cmd := NewStringSliceCmd(ctx, "srandmember", key, count)
172257
_ = c(ctx, cmd)
173258
return cmd
174259
}
175260

261+
// SRem Redis `SREM key member [member ...]` command.
262+
// Removes the specified members from the set stored at key.
263+
// Specified members that are not a member of this set are ignored.
264+
// If key does not exist, it is treated as an empty set and this command returns 0.
265+
//
266+
// Returns the number of members that were removed from the set, not including
267+
// non-existing members.
176268
func (c cmdable) SRem(ctx context.Context, key string, members ...interface{}) *IntCmd {
177269
args := make([]interface{}, 2, 2+len(members))
178270
args[0] = "srem"
@@ -183,6 +275,11 @@ func (c cmdable) SRem(ctx context.Context, key string, members ...interface{}) *
183275
return cmd
184276
}
185277

278+
// SUnion Redis `SUNION key [key ...]` command.
279+
// Returns the members of the set resulting from the union of all the given sets.
280+
// Keys that do not exist are considered to be empty sets.
281+
//
282+
// Returns a slice of members of the resulting set.
186283
func (c cmdable) SUnion(ctx context.Context, keys ...string) *StringSliceCmd {
187284
args := make([]interface{}, 1+len(keys))
188285
args[0] = "sunion"
@@ -194,6 +291,12 @@ func (c cmdable) SUnion(ctx context.Context, keys ...string) *StringSliceCmd {
194291
return cmd
195292
}
196293

294+
// SUnionStore Redis `SUNIONSTORE destination key [key ...]` command.
295+
// Stores the members of the set resulting from the union of all the given sets
296+
// into destination.
297+
// If destination already exists, it is overwritten.
298+
//
299+
// Returns the number of elements in the resulting set.
197300
func (c cmdable) SUnionStore(ctx context.Context, destination string, keys ...string) *IntCmd {
198301
args := make([]interface{}, 2+len(keys))
199302
args[0] = "sunionstore"
@@ -206,6 +309,17 @@ func (c cmdable) SUnionStore(ctx context.Context, destination string, keys ...st
206309
return cmd
207310
}
208311

312+
// SScan Redis `SSCAN key cursor [MATCH pattern] [COUNT count]` command.
313+
// Incrementally iterates the set elements stored at key.
314+
// This is a cursor-based iterator that allows scanning large sets efficiently.
315+
//
316+
// Parameters:
317+
// - cursor: The cursor value for the iteration (use 0 to start a new scan)
318+
// - match: Optional pattern to match elements (empty string means no pattern)
319+
// - count: Optional hint about how many elements to return per iteration
320+
//
321+
// Returns a ScanCmd that can be used to iterate through all members of the set.
322+
// Use the returned cursor from each iteration to continue scanning.
209323
func (c cmdable) SScan(ctx context.Context, key string, cursor uint64, match string, count int64) *ScanCmd {
210324
args := []interface{}{"sscan", key, cursor}
211325
if match != "" {

0 commit comments

Comments
 (0)