1 package redis
2
3 import (
4 "context"
5
6 "github.com/redis/go-redis/v9/internal/hashtag"
7 )
8
9 type SetCmdable interface {
10 SAdd(ctx context.Context, key string, members ...interface{}) *IntCmd
11 SCard(ctx context.Context, key string) *IntCmd
12 SDiff(ctx context.Context, keys ...string) *StringSliceCmd
13 SDiffStore(ctx context.Context, destination string, keys ...string) *IntCmd
14 SInter(ctx context.Context, keys ...string) *StringSliceCmd
15 SInterCard(ctx context.Context, limit int64, keys ...string) *IntCmd
16 SInterStore(ctx context.Context, destination string, keys ...string) *IntCmd
17 SIsMember(ctx context.Context, key string, member interface{}) *BoolCmd
18 SMIsMember(ctx context.Context, key string, members ...interface{}) *BoolSliceCmd
19 SMembers(ctx context.Context, key string) *StringSliceCmd
20 SMembersMap(ctx context.Context, key string) *StringStructMapCmd
21 SMove(ctx context.Context, source, destination string, member interface{}) *BoolCmd
22 SPop(ctx context.Context, key string) *StringCmd
23 SPopN(ctx context.Context, key string, count int64) *StringSliceCmd
24 SRandMember(ctx context.Context, key string) *StringCmd
25 SRandMemberN(ctx context.Context, key string, count int64) *StringSliceCmd
26 SRem(ctx context.Context, key string, members ...interface{}) *IntCmd
27 SScan(ctx context.Context, key string, cursor uint64, match string, count int64) *ScanCmd
28 SUnion(ctx context.Context, keys ...string) *StringSliceCmd
29 SUnionStore(ctx context.Context, destination string, keys ...string) *IntCmd
30 }
31
32
33
34 func (c cmdable) SAdd(ctx context.Context, key string, members ...interface{}) *IntCmd {
35 args := make([]interface{}, 2, 2+len(members))
36 args[0] = "sadd"
37 args[1] = key
38 args = appendArgs(args, members)
39 cmd := NewIntCmd(ctx, args...)
40 _ = c(ctx, cmd)
41 return cmd
42 }
43
44 func (c cmdable) SCard(ctx context.Context, key string) *IntCmd {
45 cmd := NewIntCmd(ctx, "scard", key)
46 _ = c(ctx, cmd)
47 return cmd
48 }
49
50 func (c cmdable) SDiff(ctx context.Context, keys ...string) *StringSliceCmd {
51 args := make([]interface{}, 1+len(keys))
52 args[0] = "sdiff"
53 for i, key := range keys {
54 args[1+i] = key
55 }
56 cmd := NewStringSliceCmd(ctx, args...)
57 _ = c(ctx, cmd)
58 return cmd
59 }
60
61 func (c cmdable) SDiffStore(ctx context.Context, destination string, keys ...string) *IntCmd {
62 args := make([]interface{}, 2+len(keys))
63 args[0] = "sdiffstore"
64 args[1] = destination
65 for i, key := range keys {
66 args[2+i] = key
67 }
68 cmd := NewIntCmd(ctx, args...)
69 _ = c(ctx, cmd)
70 return cmd
71 }
72
73 func (c cmdable) SInter(ctx context.Context, keys ...string) *StringSliceCmd {
74 args := make([]interface{}, 1+len(keys))
75 args[0] = "sinter"
76 for i, key := range keys {
77 args[1+i] = key
78 }
79 cmd := NewStringSliceCmd(ctx, args...)
80 _ = c(ctx, cmd)
81 return cmd
82 }
83
84 func (c cmdable) SInterCard(ctx context.Context, limit int64, keys ...string) *IntCmd {
85 numKeys := len(keys)
86 args := make([]interface{}, 4+numKeys)
87 args[0] = "sintercard"
88 args[1] = numKeys
89 for i, key := range keys {
90 args[2+i] = key
91 }
92 args[2+numKeys] = "limit"
93 args[3+numKeys] = limit
94 cmd := NewIntCmd(ctx, args...)
95 _ = c(ctx, cmd)
96 return cmd
97 }
98
99 func (c cmdable) SInterStore(ctx context.Context, destination string, keys ...string) *IntCmd {
100 args := make([]interface{}, 2+len(keys))
101 args[0] = "sinterstore"
102 args[1] = destination
103 for i, key := range keys {
104 args[2+i] = key
105 }
106 cmd := NewIntCmd(ctx, args...)
107 _ = c(ctx, cmd)
108 return cmd
109 }
110
111 func (c cmdable) SIsMember(ctx context.Context, key string, member interface{}) *BoolCmd {
112 cmd := NewBoolCmd(ctx, "sismember", key, member)
113 _ = c(ctx, cmd)
114 return cmd
115 }
116
117
118 func (c cmdable) SMIsMember(ctx context.Context, key string, members ...interface{}) *BoolSliceCmd {
119 args := make([]interface{}, 2, 2+len(members))
120 args[0] = "smismember"
121 args[1] = key
122 args = appendArgs(args, members)
123 cmd := NewBoolSliceCmd(ctx, args...)
124 _ = c(ctx, cmd)
125 return cmd
126 }
127
128
129 func (c cmdable) SMembers(ctx context.Context, key string) *StringSliceCmd {
130 cmd := NewStringSliceCmd(ctx, "smembers", key)
131 _ = c(ctx, cmd)
132 return cmd
133 }
134
135
136 func (c cmdable) SMembersMap(ctx context.Context, key string) *StringStructMapCmd {
137 cmd := NewStringStructMapCmd(ctx, "smembers", key)
138 _ = c(ctx, cmd)
139 return cmd
140 }
141
142 func (c cmdable) SMove(ctx context.Context, source, destination string, member interface{}) *BoolCmd {
143 cmd := NewBoolCmd(ctx, "smove", source, destination, member)
144 _ = c(ctx, cmd)
145 return cmd
146 }
147
148
149 func (c cmdable) SPop(ctx context.Context, key string) *StringCmd {
150 cmd := NewStringCmd(ctx, "spop", key)
151 _ = c(ctx, cmd)
152 return cmd
153 }
154
155
156 func (c cmdable) SPopN(ctx context.Context, key string, count int64) *StringSliceCmd {
157 cmd := NewStringSliceCmd(ctx, "spop", key, count)
158 _ = c(ctx, cmd)
159 return cmd
160 }
161
162
163 func (c cmdable) SRandMember(ctx context.Context, key string) *StringCmd {
164 cmd := NewStringCmd(ctx, "srandmember", key)
165 _ = c(ctx, cmd)
166 return cmd
167 }
168
169
170 func (c cmdable) SRandMemberN(ctx context.Context, key string, count int64) *StringSliceCmd {
171 cmd := NewStringSliceCmd(ctx, "srandmember", key, count)
172 _ = c(ctx, cmd)
173 return cmd
174 }
175
176 func (c cmdable) SRem(ctx context.Context, key string, members ...interface{}) *IntCmd {
177 args := make([]interface{}, 2, 2+len(members))
178 args[0] = "srem"
179 args[1] = key
180 args = appendArgs(args, members)
181 cmd := NewIntCmd(ctx, args...)
182 _ = c(ctx, cmd)
183 return cmd
184 }
185
186 func (c cmdable) SUnion(ctx context.Context, keys ...string) *StringSliceCmd {
187 args := make([]interface{}, 1+len(keys))
188 args[0] = "sunion"
189 for i, key := range keys {
190 args[1+i] = key
191 }
192 cmd := NewStringSliceCmd(ctx, args...)
193 _ = c(ctx, cmd)
194 return cmd
195 }
196
197 func (c cmdable) SUnionStore(ctx context.Context, destination string, keys ...string) *IntCmd {
198 args := make([]interface{}, 2+len(keys))
199 args[0] = "sunionstore"
200 args[1] = destination
201 for i, key := range keys {
202 args[2+i] = key
203 }
204 cmd := NewIntCmd(ctx, args...)
205 _ = c(ctx, cmd)
206 return cmd
207 }
208
209 func (c cmdable) SScan(ctx context.Context, key string, cursor uint64, match string, count int64) *ScanCmd {
210 args := []interface{}{"sscan", key, cursor}
211 if match != "" {
212 args = append(args, "match", match)
213 }
214 if count > 0 {
215 args = append(args, "count", count)
216 }
217 cmd := NewScanCmd(ctx, c, args...)
218 if hashtag.Present(match) {
219 cmd.SetFirstKeyPos(4)
220 }
221 _ = c(ctx, cmd)
222 return cmd
223 }
224
View as plain text