Php-redis help manual _ set related _ sAdd_sRem_sRemove_sMove_s... _ PHP Tutorial

Source: Internet
Author: User
Php-redis help manual _ set related _ sAdd_sRem_sRemove_sMove_s .... Set data type-related operations in Redis, we can view the Set type as a character set without sorting, the same as the List type, you can also add or set data types on the data values of this type.

In Redis, we can view the Set type as a character Set combination without sorting, which is the same as the List type, you can also add, delete, or determine whether an element exists on the data value of this type. Note that the time complexity of these operations is O (1), that is, the operation is completed within the constant time. The maximum number of elements that a Set can contain is 4294967295.

Unlike the List type, duplicate elements are not allowed in the Set set, which is exactly the same as the Set container in the C ++ standard library. In other words, if the same element is added multiple times, only one copy of the element is retained in Set. Compared with the List type, the Set type also has a very important feature in terms of functionality, that is, completing aggregation computing operations between multiple Sets on the server side, such as unions, intersections, and differences. Because these operations are completed on the server, the efficiency is extremely high, and it also saves a lot of network I/O overhead.

SAdd
Description
Adds a value to the set value stored at key. If this value is already in the set, FALSE is returned.

Add a VALUE to the SET container. if the VALUE already exists in the SET container, FLASE is returned.

Parameters
Key value

Return value
Bool true if value didn't exist and was added successfully, FALSE if the value is already present.

If the VALUE does not exist in the SET, the VALUE is successfully ADDED, TRUE is returned, and FALSE is returned.

Example
$ Redis-> sAdd ('key1', 'member1');/* TRUE, 'key1' =>{ 'member1 '}*/
$ Redis-> sAdd ('key1', 'member2');/* TRUE, 'key1' => {'member1', 'member2 '}*/
$ Redis-> sAdd ('key1', 'member2');/* FALSE, 'key1' => {'member1', 'member2 '}*/
SRem, sRemove
Description
Removes the specified member from the set value stored at key.

Remove the specified VALUE from the SET container

Parameters
Key member

Return value
Bool true if the member was present in the set, FALSE if it didn't.

Example
$ Redis-> sAdd ('key1', 'member1 ');
$ Redis-> sAdd ('key1', 'member2 ');
$ Redis-> sAdd ('key1', 'member3');/* 'key1' => {'member1', 'member2', 'member3 '}*/
$ Redis-> sRem ('key1', 'member2');/* 'key1' => {'member1', 'member3 '}*/
SMove
Description
Moves the specified member from the set at srcKey to the set at dstKey.

Move a specified MEMBER from the source SET to the specified another SET.

Parameters
SrcKey dstKey member

Return value
BOOL If the operation is successful, return TRUE. If the srcKey and/or dstKey didn't exist, and/or the member didn't exist in srcKey, FALSE is returned.

If the operation succeeds, TRUE is returned. if the source SET or target SET does not exist, or the MEMBER does not exist in the source SET, FLASE is returned.

Example
$ Redis-> sAdd ('key1', 'member11 ');
$ Redis-> sAdd ('key1', 'member12 ');
$ Redis-> sAdd ('key1', 'member13');/* 'key1' => {'member11', 'member12', 'member13 '}*/
$ Redis-> sAdd ('key2', 'member21 ');
$ Redis-> sAdd ('key2', 'member22');/* 'key2' => {'member21', 'member22 '}*/
$ Redis-> sMove ('key1', 'key2', 'member13');/* 'key1' =>{ 'member11', 'member12 '}*/
/* 'Key2' => {'member21', 'member22', 'member13 '}*/
SIsMember, sContains
Description
Checks if value is a member of the set stored at the key.

Check whether the VALUE is a member of the SET container.

Parameters
Key value

Return value
Bool true if value is a member of the set at key, FALSE otherwise.

Example
$ Redis-> sAdd ('key1', 'member1 ');
$ Redis-> sAdd ('key1', 'member2 ');
$ Redis-> sAdd ('key1', 'member3');/* 'key1' => {'member1', 'member2', 'member3 '}*/

$ Redis-> sIsMember ('key1', 'member1');/* TRUE */
$ Redis-> sIsMember ('key1', 'memberx');/* FALSE */
SCard, sSize
Description
Returns the cardinality of the set identified by key.

Returns the number of SET container members.

Parameters
Key

Return value
LONG the cardinality of the set identified by key, 0 if the set doesn't exist.

Example
$ Redis-> sAdd ('key1', 'member1 ');
$ Redis-> sAdd ('key1', 'member2 ');
$ Redis-> sAdd ('key1', 'member3');/* 'key1' => {'member1', 'member2', 'member3 '}*/
$ Redis-> sCard ('key1');/* 3 */
$ Redis-> sCard ('keyx');/* 0 */
SPop
Description
Removes and returns a random element from the set value at Key.

Returns an element randomly and removes it from the SET container.

Parameters
Key

Return value
String "popped" value
Bool FALSE if set identified by key is empty or doesn't exist.

Example
$ Redis-> sAdd ('key1', 'member1 ');
$ Redis-> sAdd ('key1', 'member2 ');
$ Redis-> sAdd ('key1', 'member3');/* 'key1' => {'member3', 'member1', 'member2 '}*/
$ Redis-> sPop ('key1');/* 'member1', 'key1' => {'member3', 'member2 '}*/
$ Redis-> sPop ('key1');/* 'member3', 'key1' =>{ 'member2 '}*/
SRandMember
Description
Returns a random element from the set value at Key, without removing it.

Gets a random element in the specified SET container, but does not remove it from the SET container.

Parameters
Key

Return value
String value from the set
Bool FALSE if set identified by key is empty or doesn't exist.

Example
$ Redis-> sAdd ('key1', 'member1 ');
$ Redis-> sAdd ('key1', 'member2 ');
$ Redis-> sAdd ('key1', 'member3');/* 'key1' => {'member3', 'member1', 'member2 '}*/
$ Redis-> sRandMember ('key1');/* 'member1', 'key1' => {'member3', 'member1', 'member2 '}*/
$ Redis-> sRandMember ('key1');/* 'member3', 'key1' => {'member3', 'member1', 'member2 '}*/
SInter
Description
Returns the members of a set resulting from the intersection of all the sets held at the specified keys. if just a single key is specified, then this command produces the members of this set. if one of the keys is missing, FALSE is returned.

Returns the intersection of the specified SETS. If only a SET is specified, the SET is returned. If any Parameter error occurs, FLASE is returned.

Parameters
Key1, key2, keyN: keys identifying the different sets on which we will apply the intersection.

Parameter list, indicating different SET sets.

Return value
Array, contain the result of the intersection between those keys. If the intersection beteen the different sets is empty, the return value will be empty array.

Returns an array. the result in the array is the intersection of all SET sets. If the involved SET does not have an intersection result, an empty array is returned.

Examples
$ Redis-> sAdd ('key1', 'val1 ');
$ Redis-> sAdd ('key1', 'val2 ');
$ Redis-> sAdd ('key1', 'val3 ');
$ Redis-> sAdd ('key1', 'val4 ');

$ Redis-> sAdd ('key2', 'val3 ');
$ Redis-> sAdd ('key2', 'val4 ');

$ Redis-> sAdd ('key3', 'val3 ');
$ Redis-> sAdd ('key3', 'val4 ');

Var_dump ($ redis-> sInter ('key1', 'key2', 'key3 '));
Output:

Array (2 ){
[0] =>
String (4) "val4"
[1] =>
String (4) "val3"
}
SInterStore
Description
Performs a sInter command and stores the result in a new set.

Execute an intersection operation and store the result in a new SET container.

Parameters
Key: dstkey, the key to store the diff.

The SET container key of the KEY storage result.

Keys: key1, key2. .. keyN. key1.. keyN are intersected as in sInter.

Calculates the KEYS of the intersection.

Return value
INTEGER: The cardinality of the resulting set, or FALSE in case of a missing key.

Example
$ Redis-> sAdd ('key1', 'val1 ');
$ Redis-> sAdd ('key1', 'val2 ');
$ Redis-> sAdd ('key1', 'val3 ');
$ Redis-> sAdd ('key1', 'val4 ');

$ Redis-> sAdd ('key2', 'val3 ');
$ Redis-> sAdd ('key2', 'val4 ');

$ Redis-> sAdd ('key3', 'val3 ');
$ Redis-> sAdd ('key3', 'val4 ');

Var_dump ($ redis-> sInterStore ('output', 'key1', 'key2', 'key3 '));
Var_dump ($ redis-> sMembers ('output '));
Output:

Int (2)

Array (2 ){
[0] =>
String (4) "val4"
[1] =>
String (4) "val3"
}
SUnion
Description
Performs the union between N sets and returns it.

Execute a union operation between n set containers and return results.

Parameters
Keys: key1, key2,..., keyN: Any number of keys corresponding to sets in redis.

Return value
Array of strings: The union of all these sets.

Returns an array.

Example
$ Redis-> delete ('s0', 'S1', 'S2 ');

$ Redis-> sAdd ('s0', '1 ');
$ Redis-> sAdd ('s0', '2 ');
$ Redis-> sAdd ('S1', '3 ');
$ Redis-> sAdd ('S1', '1 ');
$ Redis-> sAdd ('s2 ', '3 ');
$ Redis-> sAdd ('s2 ', '4 ');

Var_dump ($ redis-> sUnion ('s0', 'S1', 'S2 '));
Return value: all elements that are either in s0 or in s1 or in s2.

Array (4 ){
[0] =>
String (1) "3"
[1] =>
String (1) "4"
[2] =>
String (1) "1"
[3] =>
String (1) "2"
}
SUnionStore
Description
Performs the same action as sUnion, but stores the result in the first key

Execute a union operation, which is the same as sUnion (), but the result is stored in the first parameter.

Parameters
Key: dstkey, the key to store the diff.

Set key for storing results

Keys: key1, key2,..., keyN: Any number of keys corresponding to sets in redis.

KEYS of the Union

Return value
INTEGER: The cardinality of the resulting set, or FALSE in case of a missing key.

Returns an integer: the number of union results.

Example
$ Redis-> delete ('s0', 'S1', 'S2 ');

$ Redis-> sAdd ('s0', '1 ');
$ Redis-> sAdd ('s0', '2 ');
$ Redis-> sAdd ('S1', '3 ');
$ Redis-> sAdd ('S1', '1 ');
$ Redis-> sAdd ('s2 ', '3 ');
$ Redis-> sAdd ('s2 ', '4 ');

Var_dump ($ redis-> sUnionStore ('dst ', 's0', 'S1', 'S2 '));
Var_dump ($ redis-> sMembers ('dst '));
Return value: the number of elements that are either in s0 or in s1 or in s2.

Int (4)
Array (4 ){
[0] =>
String (1) "3"
[1] =>
String (1) "4"
[2] =>
String (1) "1"
[3] =>
String (1) "2"
}
SDiff
Description
Performs the difference between N sets and returns it.

Execute the difference SET operation between N different SET containers and return results. The result of this operation is the difference SET between the first SET and other SET sets involved in calculation. (Result = SET0-(SET1 UNION SET2 UNION... set n ))

Parameters
Keys: key1, key2,..., keyN: Any number of keys corresponding to sets in redis.

Return value
Array of strings: The difference of the first set will all the others.

Returns an array that returns the first SET set (first SET-(N sets) that is relative to other sets ))

Returned array: complement of the first SET

Example
$ Redis-> delete ('s0', 'S1', 'S2 ');

$ Redis-> sAdd ('s0', '1 ');
$ Redis-> sAdd ('s0', '2 ');
$ Redis-> sAdd ('s0', '3 ');
$ Redis-> sAdd ('s0', '4 ');

$ Redis-> sAdd ('S1', '1 ');
$ Redis-> sAdd ('s2 ', '3 ');

Var_dump ($ redis-> sDiff ('s0', 'S1', 'S2 '));
Return value: all elements of s0 that are neither in s1 nor in s2.

Array (2 ){
[0] =>
String (1) "4"
[1] =>
String (1) "2"
}
SDiffStore
Description
Performs the same action as sDiff, but stores the result in the first key

The function is consistent with the sDiff function, but the result is a new SET and stored in dstkey.

Parameters
Key: dstkey, the key to store the diff.

Key: set key for storing results

Keys: key1, key2,..., keyN: Any number of keys corresponding to sets in redis

SET of involved operations

Return value
INTEGER: The cardinality of the resulting set, or FALSE in case of a missing key.

Return integer: the number of result sets.

Example
$ Redis-> delete ('s0', 'S1', 'S2 ');

$ Redis-> sAdd ('s0', '1 ');
$ Redis-> sAdd ('s0', '2 ');
$ Redis-> sAdd ('s0', '3 ');
$ Redis-> sAdd ('s0', '4 ');

$ Redis-> sAdd ('S1', '1 ');
$ Redis-> sAdd ('s2 ', '3 ');

Var_dump ($ redis-> sDiffStore ('dst ', 's0', 'S1', 'S2 '));
Var_dump ($ redis-> sMembers ('dst '));
Return value: the number of elements of s0 that are neither in s1 nor in s2.

Int (2)
Array (2 ){
[0] =>
String (1) "4"
[1] =>
String (1) "2"
}
SMembers, sGetMembers
Description
Returns the contents of a set.

Returns all elements in the SET.

Parameters
Key: key

Return value
An array of elements, the contents of the set.

Example
$ Redis-> delete ('s ');
$ Redis-> sAdd ('s ', 'A ');
$ Redis-> sAdd ('s ',' B ');
$ Redis-> sAdd ('s ', 'A ');
$ Redis-> sAdd ('s ', 'C ');
Var_dump ($ redis-> sMembers ('s '));
Output:

Array (3 ){
[0] =>
String (1) "c"
[1] =>
String (1) ""
[2] =>
String (1) "B"
}
The order is random and corresponds to redis 'own internal representation of the set structure.

The order of the result SET is random, which also conforms to Redis's definition of the SET data structure. It is a set that is not repeated and has no sequence.

Author: si Yun Qilin

In Redis, we can view the Set type as a character Set combination without sorting. like the List type, we can also add ,...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.