Home >Backend Development >PHP Tutorial >php-redis Chinese help manual_set related_sAdd_sRem_sRemove_sMove_s..._PHP tutorial

php-redis Chinese help manual_set related_sAdd_sRem_sRemove_sMove_s..._PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-13 17:52:471169browse

set data type related operations

In Redis, we can regard the Set type as an unsorted character collection. Like the List type, we can also perform operations such as adding, deleting, or determining whether an element exists on the data values ​​of this type. It should be noted that the time complexity of these operations is O(1), that is, the operations are completed in 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 exactly the same as the set container in the C++ standard library. In other words, if you add the same element multiple times, only one copy of the element will be kept in the Set. Compared with the List type, the Set type also has a very important functional feature, that is, the aggregation calculation operations between multiple Sets, such as unions, intersections, and differences, are completed on the server side. Since these operations are completed on the server side, they are extremely efficient and save a lot of network IO 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, return 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 is successful and returns TRUE, 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.

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 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 is successful, TRUE is returned. If the source SET or target SET does not exist, or MEMBER does not exist in the source SET, then 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' => sIsMember, sContains
Description
Checks if value is a member of the set stored at the key key.

Check whether 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 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 the 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.

随机返回一个元素,并且在SET容器中移除该元素。

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.

取得指定SET容器中的一个随机元素,但不会在SET容器中移除它。

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.

返回指定SETS集合的交集结果。如果只是指定了一个SET集合,那么返回该SET集合。如果在参数中有参数错误,那么则返回FLASE。

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

参数列表,代表不同的SET集合。

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.

返回数组,数组中的结果为所有SET集合的交集。如果所涉及到的SET集合没有交集结果,那么将返回一个空数组。

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.

执行一个交集操作,并把结果存储到一个新的SET容器中。

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

key 储存结果的SET容器KEY

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

求交集的KEYS

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.

Perform a union operation between N SET containers and return 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 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

Performs a union operation just like sUnion(), but the result is stored in the first parameter.

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

SET collection KEY to store the results

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

Find the KEYS of the union

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

Returns an integer value: 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.

Perform a difference operation between N different SET containers and return the result. The result of this operation is the difference set of the first SET relative to other SET sets participating 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 will all the others.

Returns an array, which returns the difference between the first SET set and other sets (first set - (N sets))

Return array: 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 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 same function as the sDiff function, except that the result is a new SET set, stored in dstkey.

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

Key: SET collection KEY to store the results

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

SET collection participating in the operation

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

Return 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 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 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 random and corresponds to redis' own internal representation of the set structure.

The order of the result set is random, which is also in line with Redis's own definition of the SET data structure. Unrepeated, unordered collection.

Author: Siyun Qilin

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478094.htmlTechArticleset data type related operations In Redis, we can regard the Set type as an unsorted character collection, and List The type is the same, we can also add,...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn