Skip to content

Commit e609fbe

Browse files
Documentation: Add detailed docs for several set operations
SADD[ARRAY] SSINTER[STORE] SUNION[STORE] SDIFF[STORE] [skip ci]
1 parent 78de25a commit e609fbe

6 files changed

Lines changed: 238 additions & 4 deletions

redis.stub.php

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,18 +949,177 @@ public function role(): mixed;
949949
*/
950950
public function rpoplpush(string $srckey, string $dstkey): Redis|string|false;
951951

952+
/**
953+
* Add one or more values to a Redis SET key.
954+
*
955+
* @see https://redis.io/commands/sadd
956+
957+
* @param string $key The key name
958+
* @param mixed $member A value to add to the set.
959+
* @param mixed $other_members One or more additional values to add
960+
*
961+
* @return Redis|int|false The number of values added to the set.
962+
*
963+
* <code>
964+
* <?php
965+
* $redis = new Redis(['host' => 'localhost']);
966+
*
967+
* $redis->del('myset');
968+
*
969+
* var_dump($redis->sadd('myset', 'foo', 'bar', 'baz'));
970+
* var_dump($redis->sadd('myset', 'foo', 'new'));
971+
*
972+
* // --- OUTPUT ---
973+
* // int(3)
974+
* // int(1)
975+
* ?>
976+
* </code>
977+
*/
952978
public function sAdd(string $key, mixed $value, mixed ...$other_values): Redis|int|false;
953979

980+
/**
981+
* Add one ore more values to a Redis SET key. This is an alternative to Redis::sadd() but
982+
* instead of being variadic, takes a single array of values.
983+
*
984+
* @see https://redis.io/commands/sadd
985+
* @see Redis::sadd()
986+
*
987+
* @param string $key The set to add values to.
988+
* @param array $values One or more members to add to the set.
989+
* @return Redis|int|false The number of members added to the set.
990+
*
991+
* </code>
992+
* <?php
993+
* $redis = new Redis(['host' => 'localhost']);
994+
*
995+
* $redis->del('myset');
996+
*
997+
* var_dump($redis->sAddArray('myset', ['foo', 'bar', 'baz']));
998+
* var_dump($redis->sAddArray('myset', ['foo', 'new']));
999+
*
1000+
* // --- OUTPUT ---
1001+
* // int(3)
1002+
* // int(1)
1003+
* ?>
1004+
* </code>
1005+
*/
9541006
public function sAddArray(string $key, array $values): int;
9551007

1008+
/**
1009+
* Given one or more Redis SETS, this command returns all of the members from the first
1010+
* set that are not in any subsequent set.
1011+
*
1012+
* @see https://redis.io/commands/sdiff
1013+
*
1014+
* @param string $key The first set
1015+
* @param string $other_keys One or more additional sets
1016+
*
1017+
* @return Redis|array|false Returns the elements from keys 2..N that don't exist in the
1018+
* first sorted set, or false on failure.
1019+
*
1020+
* <code>
1021+
* <?php
1022+
* $redis = new Redis(['host' => 'localhost']);
1023+
*
1024+
* $redis->pipeline()
1025+
* ->del('set1', 'set2', 'set3')
1026+
* ->sadd('set1', 'apple', 'banana', 'carrot', 'date')
1027+
* ->sadd('set2', 'carrot')
1028+
* ->sadd('set3', 'apple', 'carrot', 'eggplant')
1029+
* ->exec();
1030+
*
1031+
* // NOTE: 'banana' and 'date' are in set1 but none of the subsequent sets.
1032+
* var_dump($redis->sdiff('set1', 'set2', 'set3'));
1033+
*
1034+
* // --- OUTPUT ---
1035+
* array(2) {
1036+
* [0]=>
1037+
* string(6) "banana"
1038+
* [1]=>
1039+
* string(4) "date"
1040+
* }
1041+
* ?>
1042+
*/
9561043
public function sDiff(string $key, string ...$other_keys): Redis|array|false;
9571044

1045+
/**
1046+
* This method performs the same operation as SDIFF except it stores the resulting diff
1047+
* values in a specified destination key.
1048+
*
1049+
* @see https://redis.io/commands/sdiffstore
1050+
* @see Redis::sdiff()
1051+
*
1052+
* @param string $dst The key where to store the result
1053+
* @param string $key The first key to perform the DIFF on
1054+
* @param string $other_keys One or more additional keys.
1055+
*
1056+
* @return Redis|int|false The number of values stored in the destination set or false on failure.
1057+
*/
9581058
public function sDiffStore(string $dst, string $key, string ...$other_keys): Redis|int|false;
9591059

1060+
/**
1061+
* Given one or more Redis SET keys, this command will return all of the elements that are
1062+
* in every one.
1063+
*
1064+
* @see https://redis.io/commands/sinter
1065+
*
1066+
* @param string $key The first SET key to intersect.
1067+
* @param string $other_keys One or more Redis SET keys.
1068+
*
1069+
* <code>
1070+
* <?php
1071+
*
1072+
* $redis = new Redis(['host' => 'localhost']);
1073+
*
1074+
* $redis->pipeline()
1075+
* ->del('alice_likes', 'bob_likes', 'bill_likes')
1076+
* ->sadd('alice_likes', 'asparagus', 'broccoli', 'carrot', 'potato')
1077+
* ->sadd('bob_likes', 'asparagus', 'carrot', 'potato')
1078+
* ->sadd('bill_likes', 'broccoli', 'potato')
1079+
* ->exec();
1080+
*
1081+
* // NOTE: 'potato' is the only value in all three sets
1082+
* var_dump($redis->sinter('alice_likes', 'bob_likes', 'bill_likes'));
1083+
*
1084+
* // --- OUTPUT ---
1085+
* // array(1) {
1086+
* // [0]=>
1087+
* // string(6) "potato"
1088+
* // }
1089+
* ?>
1090+
* </code>
1091+
*/
9601092
public function sInter(array|string $key, string ...$other_keys): Redis|array|false;
9611093

9621094
public function sintercard(array $keys, int $limit = -1): Redis|int|false;
9631095

1096+
/**
1097+
* Perform the intersection of one or more Redis SETs, storing the result in a destination
1098+
* key, rather than returning them.
1099+
*
1100+
* @see https://redis.io/commands/sinterstore
1101+
* @see Redis::sinter()
1102+
*
1103+
* @param array|string $key_or_keys Either a string key, or an array of keys (with at least two
1104+
* elements, consisting of the destination key name and one
1105+
* or more source keys names.
1106+
* @param string $other_keys If the first argument was a string, subsequent arguments should
1107+
* be source key names.
1108+
*
1109+
* @return Redis|int|false The number of values stored in the destination key or false on failure.
1110+
*
1111+
* <code>
1112+
* <?php
1113+
* $redis = new Redis(['host' => 'localhost']);
1114+
*
1115+
* // OPTION 1: A single array
1116+
* $redis->sInterStore(['dst', 'src1', 'src2', 'src3']);
1117+
*
1118+
* // OPTION 2: Variadic
1119+
* $redis->sInterStore('dst', 'src1', 'src'2', 'src3');
1120+
* ?>
1121+
* </code>
1122+
*/
9641123
public function sInterStore(array|string $key, string ...$other_keys): Redis|int|false;
9651124

9661125
public function sMembers(string $key): Redis|array|false;
@@ -973,8 +1132,59 @@ public function sPop(string $key, int $count = 0): Redis|string|array|false;
9731132

9741133
public function sRandMember(string $key, int $count = 0): Redis|string|array|false;
9751134

1135+
/**
1136+
* Returns the union of one or more Redis SET keys.
1137+
*
1138+
* @see https://redis.io/commands/sunion
1139+
*
1140+
* @param string $key The first SET to do a union with
1141+
* @param string $other_keys One or more subsequent keys
1142+
*
1143+
* @return Redis|array|false The union of the one or more input sets or false on failure.
1144+
*
1145+
* <code>
1146+
* <?php
1147+
* $redis = new Redis(['host' => 'localhost']);
1148+
*
1149+
* $redis->pipeline()
1150+
* ->del('set1', 'set2', 'set3')
1151+
* ->sadd('set1', 'apple', 'banana', 'carrot')
1152+
* ->sadd('set2', 'apple', 'carrot', 'fish')
1153+
* ->sadd('set3', 'carrot', 'fig', 'eggplant');
1154+
*
1155+
* var_dump($redis->sunion('set1', 'set2', 'set3'));
1156+
*
1157+
* // --- OPUTPUT ---
1158+
* // array(5) {
1159+
* // [0]=>
1160+
* // string(6) "banana"
1161+
* // [1]=>
1162+
* // string(5) "apple"
1163+
* // [2]=>
1164+
* // string(4) "fish"
1165+
* // [3]=>
1166+
* // string(6) "carrot"
1167+
* // [4]=>
1168+
* // string(8) "eggplant"
1169+
* // }
1170+
* ?>
1171+
* </code>
1172+
*/
9761173
public function sUnion(string $key, string ...$other_keys): Redis|array|false;
9771174

1175+
/**
1176+
* Perform a union of one or more Redis SET keys and store the result in a new set
1177+
*
1178+
* @see https://redis.io/commands/sunionstore
1179+
* @see Redis::sunion()
1180+
*
1181+
* @param string $dst The destination key
1182+
* @param string $key The first source key
1183+
* @param string $other_keys One or more additional source keys
1184+
*
1185+
* @return Redis|int|false The number of elements stored in the destination SET or
1186+
* false on failure.
1187+
*/
9781188
public function sUnionStore(string $dst, string $key, string ...$other_keys): Redis|int|false;
9791189

9801190
public function save(): bool;

redis_arginfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 63dd54d205675ceed36354c9b880e6afc7a0604e */
2+
* Stub hash: 8e423eab8d5b732655e7fcaab4d0800aadc38747 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis___construct, 0, 0, 0)
55
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, options, IS_ARRAY, 0, "null")

redis_cluster.stub.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,14 @@ public function rpush(string $key, mixed ...$elements): RedisCluster|int|false;
367367

368368
public function rpushx(string $key, string $value): RedisCluster|bool|int;
369369

370+
/**
371+
* @see Redis::sadd()
372+
*/
370373
public function sadd(string $key, mixed $value, mixed ...$other_values): RedisCluster|int|false;
371374

375+
/**
376+
* @see Redis::saddarray()
377+
*/
372378
public function saddarray(string $key, array $values): RedisCluster|bool|int;
373379

374380
public function save(string|array $key_or_address): RedisCluster|bool;
@@ -379,8 +385,14 @@ public function scard(string $key): RedisCluster|int|false;
379385

380386
public function script(string|array $key_or_address, mixed ...$args): mixed;
381387

388+
/**
389+
* @see Redis::sdiff()
390+
*/
382391
public function sdiff(string $key, string ...$other_keys): RedisCluster|array|false;
383392

393+
/**
394+
* @see Redis::sdiffstore()
395+
*/
384396
public function sdiffstore(string $dst, string $key, string ...$other_keys): RedisCluster|int|false;
385397

386398
public function set(string $key, mixed $value, mixed $options = NULL): RedisCluster|string|bool;
@@ -395,10 +407,16 @@ public function setoption(int $option, mixed $value): bool;
395407

396408
public function setrange(string $key, int $offset, string $value): RedisCluster|int|false;
397409

410+
/**
411+
* @see Redis::sinter()
412+
*/
398413
public function sinter(array|string $key, string ...$other_keys): RedisCluster|array|false;
399414

400415
public function sintercard(array $keys, int $limit = -1): RedisCluster|int|false;
401416

417+
/**
418+
* @see Redis::sinterstore()
419+
*/
402420
public function sinterstore(array|string $key, string ...$other_keys): RedisCluster|int|false;
403421

404422
public function sismember(string $key, mixed $value): RedisCluster|bool;
@@ -431,8 +449,14 @@ public function strlen(string $key): RedisCluster|int|false;
431449

432450
public function subscribe(array $channels, callable $cb): void;
433451

452+
/**
453+
* @see Redis::sunion()
454+
*/
434455
public function sunion(string $key, string ...$other_keys): RedisCluster|bool|array;
435456

457+
/**
458+
* @see Redis::sunionstore()
459+
*/
436460
public function sunionstore(string $dst, string $key, string ...$other_keys): RedisCluster|int|false;
437461

438462
public function time(string|array $key_or_address): RedisCluster|bool|array;

redis_cluster_arginfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 84ef1f62ed4ba37f79eab0897519bba0946b0f26 */
2+
* Stub hash: 836411b2d661943a61fd5a2aadac30997a506cde */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_RedisCluster___construct, 0, 0, 1)
55
ZEND_ARG_TYPE_INFO(0, name, IS_STRING, 1)

redis_cluster_legacy_arginfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 84ef1f62ed4ba37f79eab0897519bba0946b0f26 */
2+
* Stub hash: 836411b2d661943a61fd5a2aadac30997a506cde */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_RedisCluster___construct, 0, 0, 1)
55
ZEND_ARG_INFO(0, name)

redis_legacy_arginfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 63dd54d205675ceed36354c9b880e6afc7a0604e */
2+
* Stub hash: 8e423eab8d5b732655e7fcaab4d0800aadc38747 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis___construct, 0, 0, 0)
55
ZEND_ARG_INFO(0, options)

0 commit comments

Comments
 (0)