当前位置:编程学习 > php >>

php-redis中文帮助手册_set相关_sAdd_sRem_sRemove_sMove_s...

set数据类型相关操作

      在Redis中,我们可以将Set类型看作为没有排序的字符集合,和List类型一样,我们也可以在该类型的数据值上执行添加、删除或判断某一元素是否存在等操作。需要说明的是,这些操作的时间复杂度为O(1),即常量时间内完成次操作。Set可包含的最大元素数量是4294967295。

      和List类型不同的是,Set集合中不允许出现重复的元素,这一点和C++标准库中的set容器是完全相同的。换句话说,如果多次添加相同元素,Set中将仅保留该元素的一份拷贝。和List类型相比,Set类型在功能上还存在着一个非常重要的特性,即在服务器端完成多个Sets之间的聚合计算操作,如unions、intersections和differences。由于这些操作均在服务端完成,因此效率极高,而且也节省了大量的网络IO开销。

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

添加一个VALUE到SET容器中,如果这个VALUE已经存在于SET中,那么返回FLASE。

Parameters
key value

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

如果VALUE不存在于SET中,那么ADDED成功,返回TRUE,负责返回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.

移除指定的VALUE从SET容器中

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.

移动一个指定的MEMBER从源SET到指定的另一个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.

如果操作成功返回TRUE,如果源SET或者目标SET不存在,或者MEMBER不存在于源SET,那么返回FLASE。

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.

检查VALUE是否是SET容器中的成员。

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.

返回SET容器的成员数

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'

补充:Web开发 , php ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,