1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Webdcg\Redis\Traits; |
4
|
|
|
|
5
|
|
|
trait Sets |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Adds a value to the set value stored at key. If this value is already |
9
|
|
|
* in the set, FALSE is returned. |
10
|
|
|
* See: https://redis.io/commands/sadd. |
11
|
|
|
* |
12
|
|
|
* @param string $key |
13
|
|
|
* @param splat $members |
14
|
|
|
* |
15
|
|
|
* @return int The number of elements added to the set. |
16
|
|
|
*/ |
17
|
|
|
public function sAdd(string $key, ...$members): int |
18
|
|
|
{ |
19
|
|
|
if (is_array($members[0])) { |
20
|
|
|
return $this->redis->sAdd($key, ...$members[0]); |
|
|
|
|
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
return $this->redis->sAdd($key, ...$members); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Returns the cardinality of the set identified by key. |
28
|
|
|
* See: https://redis.io/commands/scard. |
29
|
|
|
* |
30
|
|
|
* @param string $key |
31
|
|
|
* |
32
|
|
|
* @return int the cardinality of the set identified by key, |
33
|
|
|
* 0 if the set doesn't exist. |
34
|
|
|
*/ |
35
|
|
|
public function sCard(string $key): int |
36
|
|
|
{ |
37
|
|
|
return $this->redis->sCard($key); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Returns the cardinality of the set identified by key. |
42
|
|
|
* See: https://redis.io/commands/scard. |
43
|
|
|
* Note: sSize is an alias for sCard and will be removed in future |
44
|
|
|
* versions of phpredis. |
45
|
|
|
* |
46
|
|
|
* @param string $key |
47
|
|
|
* |
48
|
|
|
* @return int the cardinality of the set identified by key, |
49
|
|
|
* 0 if the set doesn't exist. |
50
|
|
|
*/ |
51
|
|
|
public function sSize(string $key): int |
52
|
|
|
{ |
53
|
|
|
return $this->redis->sCard($key); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Performs the difference between N sets and returns it. |
58
|
|
|
* |
59
|
|
|
* @param splat $keys |
60
|
|
|
* |
61
|
|
|
* @return array |
62
|
|
|
*/ |
63
|
|
|
public function sDiff(...$keys): array |
64
|
|
|
{ |
65
|
|
|
return $this->redis->sDiff(...$keys); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Performs the same action as sDiff, but stores the result in the first key. |
70
|
|
|
* |
71
|
|
|
* @param string $destinationKey The key to store the diff into. |
72
|
|
|
* @param splat $keys key1, key2, ... , keyN: Any number of keys |
73
|
|
|
* corresponding to sets in redis. |
74
|
|
|
* |
75
|
|
|
* @return The cardinality of the resulting set, or FALSE in case of a |
76
|
|
|
* missing key. |
77
|
|
|
*/ |
78
|
|
|
public function sDiffStore(string $destinationKey, ...$keys): int |
79
|
|
|
{ |
80
|
|
|
return $this->redis->sDiffStore($destinationKey, ...$keys); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Returns the members of a set resulting from the intersection of all the |
85
|
|
|
* sets held at the specified keys. |
86
|
|
|
* If just a single key is specified, then this command produces the members |
87
|
|
|
* of this set. If one of the keys is missing, FALSE is returned. |
88
|
|
|
* See: https://redis.io/commands/sinter. |
89
|
|
|
* |
90
|
|
|
* @param splat $keys key1, key2, keyN: keys identifying the different |
91
|
|
|
* sets on which we will apply the intersection. |
92
|
|
|
* |
93
|
|
|
* @return array Contain the result of the intersection between |
94
|
|
|
* those keys. If the intersection between the |
95
|
|
|
* different sets is empty, the return value will be |
96
|
|
|
* empty array. |
97
|
|
|
*/ |
98
|
|
|
public function sInter(...$keys): array |
99
|
|
|
{ |
100
|
|
|
return $this->redis->sInter(...$keys); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Performs a sInter command and stores the result in a new set. |
105
|
|
|
* See: https://redis.io/commands/sinterstore. |
106
|
|
|
* |
107
|
|
|
* @param string $destinationKey The key to store the diff into. |
108
|
|
|
* @param plat $keys key1, key2... keyN. key1..keyN are |
109
|
|
|
* intersected as in sInter. |
110
|
|
|
* |
111
|
|
|
* @return int The cardinality of the resulting set, |
112
|
|
|
* or FALSE in case of a missing key. |
113
|
|
|
*/ |
114
|
|
|
public function sInterStore(string $destinationKey, ...$keys): int |
115
|
|
|
{ |
116
|
|
|
return $this->redis->sInterStore($destinationKey, ...$keys); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Checks if member is a member of the set stored at the key key. |
121
|
|
|
* See: https://redis.io/commands/sismember. |
122
|
|
|
* |
123
|
|
|
* @param string $key |
124
|
|
|
* @param mixed $member |
125
|
|
|
* |
126
|
|
|
* @return bool TRUE if value is a member of the set at key key, |
127
|
|
|
* FALSE otherwise. |
128
|
|
|
*/ |
129
|
|
|
public function sIsMember(string $key, $member): bool |
130
|
|
|
{ |
131
|
|
|
return $this->redis->sIsMember($key, $member); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Checks if member is a member of the set stored at the key key. |
136
|
|
|
* Note: sContains is an alias for sIsMember and will be removed in future |
137
|
|
|
* versions of phpredis. |
138
|
|
|
* See: https://redis.io/commands/sismember. |
139
|
|
|
* |
140
|
|
|
* @param string $key |
141
|
|
|
* @param mixed $member |
142
|
|
|
* |
143
|
|
|
* @return bool TRUE if value is a member of the set at key key, |
144
|
|
|
* FALSE otherwise. |
145
|
|
|
*/ |
146
|
|
|
public function sContains(string $key, $member): bool |
147
|
|
|
{ |
148
|
|
|
return $this->redis->sIsMember($key, $member); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Returns the contents of a set. |
153
|
|
|
* The order is random and corresponds to redis' own internal representation |
154
|
|
|
* of the set structure. |
155
|
|
|
* See: https://redis.io/commands/smembers. |
156
|
|
|
* |
157
|
|
|
* @param string $key |
158
|
|
|
* |
159
|
|
|
* @return array An array of elements, the contents of the set. |
160
|
|
|
*/ |
161
|
|
|
public function sMembers(string $key): array |
162
|
|
|
{ |
163
|
|
|
return $this->redis->sMembers($key); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Returns the contents of a set. |
168
|
|
|
* The order is random and corresponds to redis' own internal representation |
169
|
|
|
* of the set structure. |
170
|
|
|
* Note: sGetMembers is an alias for sMembers and will be removed in future |
171
|
|
|
* versions of phpredis. |
172
|
|
|
* See: https://redis.io/commands/smembers. |
173
|
|
|
* |
174
|
|
|
* @param string $key |
175
|
|
|
* |
176
|
|
|
* @return array An array of elements, the contents of the set. |
177
|
|
|
*/ |
178
|
|
|
public function sGetMembers(string $key): array |
179
|
|
|
{ |
180
|
|
|
return $this->redis->sMembers($key); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Moves the specified member from the set at sourceKey to the set at |
185
|
|
|
* destinationKey. |
186
|
|
|
* See: https://redis.io/commands/smove. |
187
|
|
|
* |
188
|
|
|
* @param string $sourceKey |
189
|
|
|
* @param string $destinationKey |
190
|
|
|
* @param mixed $member |
191
|
|
|
* |
192
|
|
|
* @return bool If the operation is successful, return TRUE. |
193
|
|
|
* If the sourceKey and/or destinationKey didn't |
194
|
|
|
* exist, and/or the member didn't exist in sourceKey, |
195
|
|
|
* FALSE is returned. |
196
|
|
|
*/ |
197
|
|
|
public function sMove(string $sourceKey, string $destinationKey, $member): bool |
198
|
|
|
{ |
199
|
|
|
return $this->redis->sMove($sourceKey, $destinationKey, $member); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Removes and returns a random element from the set value at Key. |
204
|
|
|
* See: https://redis.io/commands/spop. |
205
|
|
|
* |
206
|
|
|
* @param string $key |
207
|
|
|
* @param int|integer $count Number of elemets to be returned |
208
|
|
|
* |
209
|
|
|
* @return mixed|string|array String "popped" value. |
210
|
|
|
* Array Member(s) returned or an empty array |
211
|
|
|
* if the set doesn't exist |
212
|
|
|
* FALSE on error if the key is not a set. |
213
|
|
|
*/ |
214
|
|
|
public function sPop(string $key, int $count = 1) |
215
|
|
|
{ |
216
|
|
|
if ($count > 1) { |
217
|
|
|
return $this->redis->sPop($key, $count); |
218
|
|
|
} |
219
|
|
|
return $this->redis->sPop($key); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Returns a random element from the set value at Key, without removing it. |
224
|
|
|
* See: https://redis.io/commands/srandmember. |
225
|
|
|
* |
226
|
|
|
* @param string $key |
227
|
|
|
* @param int|integer $count |
228
|
|
|
* |
229
|
|
|
* @return mixed|string|array If no count is provided, a random String |
230
|
|
|
* value from the set will be returned. If a |
231
|
|
|
* count is provided, an array of values from |
232
|
|
|
* the set will be returned. Read about the |
233
|
|
|
* different ways to use the count here: |
234
|
|
|
* SRANDMEMBER Bool FALSE if set identified by |
235
|
|
|
* key is empty or doesn't exist. |
236
|
|
|
*/ |
237
|
|
|
public function sRandMember(string $key, int $count = 1) |
238
|
|
|
{ |
239
|
|
|
if ($count > 1) { |
240
|
|
|
return $this->redis->sRandMember($key, $count); |
241
|
|
|
} |
242
|
|
|
return $this->redis->sRandMember($key); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Removes the specified member from the set value stored at key. |
247
|
|
|
* See: https://redis.io/commands/srem. |
248
|
|
|
* |
249
|
|
|
* @param string $key |
250
|
|
|
* @param splat $members |
251
|
|
|
* |
252
|
|
|
* @return int The number of elements removed from the set. |
253
|
|
|
*/ |
254
|
|
|
public function sRem(string $key, ...$members): int |
255
|
|
|
{ |
256
|
|
|
return $this->redis->sRem($key, ...$members); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Removes the specified member from the set value stored at key. |
261
|
|
|
* Note: sRemove is an alias for sRem and will be removed in future |
262
|
|
|
* versions of phpredis. |
263
|
|
|
* See: https://redis.io/commands/srem. |
264
|
|
|
* |
265
|
|
|
* @param string $key |
266
|
|
|
* @param splat $members |
267
|
|
|
* |
268
|
|
|
* @return int The number of elements removed from the set. |
269
|
|
|
*/ |
270
|
|
|
public function sRemove(string $key, ...$members): int |
271
|
|
|
{ |
272
|
|
|
return $this->redis->sRem($key, ...$members); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Performs the union between N sets and returns it. |
277
|
|
|
* See: https://redis.io/commands/sunion. |
278
|
|
|
* |
279
|
|
|
* @param splat $keys |
280
|
|
|
* |
281
|
|
|
* @return array key1, key2, ... , keyN: Any number of keys |
282
|
|
|
* corresponding to sets in redis. |
283
|
|
|
*/ |
284
|
|
|
public function sUnion(...$keys): array |
285
|
|
|
{ |
286
|
|
|
return $this->redis->sUnion(...$keys); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Performs the same action as sUnion, but stores the result in the first |
291
|
|
|
* key. |
292
|
|
|
* See: https://redis.io/commands/sunionstore. |
293
|
|
|
* |
294
|
|
|
* @param string $destinationKey |
295
|
|
|
* @param splat $keys |
296
|
|
|
* |
297
|
|
|
* @return int The cardinality of the resulting set, |
298
|
|
|
* or FALSE in case of a missing key. |
299
|
|
|
*/ |
300
|
|
|
public function sUnionStore(string $destinationKey, ...$keys): int |
301
|
|
|
{ |
302
|
|
|
if (is_array($keys[0])) { |
303
|
|
|
return $this->redis->sUnionStore($destinationKey, ...$keys[0]); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
return $this->redis->sUnionStore($destinationKey, ...$keys); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Scan a set for members. |
311
|
|
|
* See: https://redis.io/commands/sscan. |
312
|
|
|
* |
313
|
|
|
* @param string $key The set to search. |
314
|
|
|
* @param int $iterator LONG (reference) to the iterator as we go. |
315
|
|
|
* @param string $pattern String, optional pattern to match against. |
316
|
|
|
* @param int|integer $count How many members to return at a time |
317
|
|
|
* (Redis might return a different amount). |
318
|
|
|
* |
319
|
|
|
* @return array PHPRedis will return an array of keys or |
320
|
|
|
* FALSE when we're done iterating. |
321
|
|
|
*/ |
322
|
|
|
public function sScan(string $key, ?int &$iterator, string $pattern, int $count = 10) |
323
|
|
|
{ |
324
|
|
|
return $this->redis->sScan($key, $iterator, $pattern, $count); |
325
|
|
|
} |
326
|
|
|
} |
327
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: