Set data type related operations
In Redis, we can see the set type as an unordered character set, and as with the list type, we can also perform operations such as adding, deleting, or judging whether an element exists on the data value of that type. It should be stated that the time complexity of these operations is O (1), which is the completion of the operation within a 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 collection, which is identical to the set container in the C + + standard library. In other words, if you add the same element more than once, only one copy of the element will be preserved in set. The set type has a functionally important feature in comparison to the list type, which is to perform aggregate compute operations between multiple sets on the server side, such as unions, intersections, and differences. Because these operations are done on the server side, they are highly efficient and also save a lot of network IO overhead.
Sadd
Description
Adds a value to the set value stored at key. If the value is already in the set, FALSE is returned.
Add a value to the set container, and if the value already exists in the set, return to Flase.
Parameters
Key value
Return value
BOOL TRUE if value didn ' t exist and was added successfully, FALSE if the value is already present.
If value does not exist in set, then added succeeds, returns True, and is responsible for returning false.
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.
Removes the specified value from the Set container
Parameters
Key member
Return value
BOOL TRUE If the member is 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.
Moves a specified member from the source set to another specified 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 returns true successfully, returns flase if the source set or target set does not exist, or if the member does not exist in the source set.
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 key.
Checks if value is a member in the Set container.
Parameters
Key value
Return value
BOOL TRUE If value is a member of the set at key 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 members of a set container
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 the element in 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 the sets held at the specified keys. If just a single key was specified, then this command produces the "this set". If One of the keys is missing, FALSE is returned.
Returns the result of the intersection of the specified sets collection. If only a set collection is specified, the set collection is returned. If there is a parameter error in the parameter, then Flase is returned.
Parameters
Key1, Key2, Keyn:keys identifying the different sets on which we'll apply the intersection.
A parameter list that represents a different set collection.
Return value
Array, contain the result of the intersection between those keys. If the intersection beteen the different sets is empty, the return value would be empty array.
Returns an array in which the result of the array is the intersection of all set sets. If the set set involved 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.
Performs an intersection operation and stores the result in a new set container.
Parameters
Key:dstkey, the key to store the diff into.
Key to store the result set container key
Keys:key1, Key2 ... keyN. Key1. KeyN is intersected as in SInter.
Keys for 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.
Executes a set operation between n set containers and returns the result.
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 is 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
Performing a set operation is just like sunion (), but the result is stored in the first parameter.
Parameters
Key:dstkey, the key to store the diff into.
Set set of stored results key
Keys:key1, Key2, ..., keyn:any number of keys corresponding to sets in Redis.
Keys for the collection
Return value
Integer:the cardinality of the resulting set, or FALSE in case of a missing key.
Returns an integer value: The number of results of the set.
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 is 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.
Performs a differential set operation between n different set containers and returns the result. The result of this operation is the difference set of the first set relative to the other set set that participates in the 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 would all the others.
Returns an array that returns the difference set (first set-(N sets)) from the second set set relative to the other set
Returns the array: the complement of the first set 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 is 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
With the Sdiff function function all the time, only the result is a new set set, stored to Dstkey.
Parameters
Key:dstkey, the key to store the diff into.
Key: Set set of stored results key
Keys:key1, Key2, ..., keyn:any number of keys corresponding to sets in Redis
Set set of participating operations
Return value
Integer:the cardinality of the resulting set, or FALSE in case of a missing key.
Returns an 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 is 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 the elements in the set collection.
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) "a"
[2]=>
String (1) "B"
}
The order is a random and corresponds to Redis ' own internal representation of the set structure.
The order of the result set is random, which also conforms to the definition of the set data structure by Redis itself. No duplicates, no sequence of collections.
Author: Four Yun Kylin
http://www.bkjia.com/PHPjc/478094.html www.bkjia.com true http://www.bkjia.com/PHPjc/478094.html techarticle set data type related operations in Redis, we can see the set type as an unordered character set, and as with the list type, we can also perform additions on the data values of that type 、...