SADD
将一个或多个member元素加入到集合key当中。(从左侧插入,最后插入的元素在0位置),集合中已经存在TK 则返回false,不存在添加成功 返回true
<?php var_dump($redis->sadd('set-key','A','B','C')); //int(3)
SMEMBERS
返回集合key中的所有成员。
<?php var_dump($redis->sMembers('set-key')); //array(3) { [0]=> string(1) "C" [1]=> string(1) "A" [2]=> string(1) "B" }
SREM
移除集合key中的一个或多个member元素,不存在的member元素会被忽略。
<?php $redis->srem('set-key','C'); //移除set-key中的C元素
SISMEMBER
判断member元素是否是集合key的成员。如果member元素是集合的成员,返回1。如果member元素不是集合的成员,或key不存在,返回0。
<?php var_dump($redis->sismember('set-key','A')); //bool(true) var_dump($redis->sismember('set-key','F')); //bool(false)
SCARD
返回集合key的基数(集合中元素的数量)。
<?php var_dump($redis->scard('set-key')); //int(3)
SMOVE
将指定元素从指定集合移动到目标集合。smove(要移除元素的集合,要移动到的目标集合,要移除的元素);
<?php $redis->sadd('s-key1','A','B','C'); $redis->sadd('s-key2','1','2','3'); var_dump($redis->smove('s-key1','s-key2','C')); //key1集合存在元素返回bool(true) var_dump($redis->smove('s-key1','s-key2','F')); //key1集合不存在元素返回 bool(false) var_dump($redis->smembers('s-key1')); //array(2) { [0]=> string(1) "A" [1]=> string(1) "B" } var_dump($redis->smembers('s-key2')); // array(4) { [0]=> string(1) "C" [1]=> string(1) "3" [2]=> string(1) "2" [3]=> string(1) "1" }
SPOP
随机返回容器中一个元素,并移除该元素
<?php $redis->sadd('s-key','A','B','C','D','E'); var_dump($redis->spop('s-key')); //string(1) "E"
var_dump($redis->spop('s-key2')); //该key不存在 ,返回bool(false)
SRANDMEMBER
随机返回容器中一个元素,不移除该元素
<?php $redis->srandmember('s-key');
SINTER
返回两个集合的交集 没有交集返回一个空数组,若参数只有一个集合,则返回集合对应的完整的数组
<?php $redis->sadd('s-key1','A','B','C','D','E'); $redis->sadd('s-key2','E','G','C','F','E'); var_dump($redis->sinter('s-key1','s-key2')); //array(2) { [0]=> string(1) "C" [1]=> string(1) "E" }
SINTERSTORE
将两个集合的交集放入另一个集合里。
<?php $redis->sadd('s-key1','A','B','C','D','E'); $redis->sadd('s-key2','E','G','C','F','E'); var_dump($redis->sInterStore('store','s-key1','s-key2')); //int(2) var_dump($redis->smembers('store')); // array(2) { [0]=> string(1) "C" [1]=> string(1) "E" }
SUNION
返回两个集合的并集,即使多个集合有相同元素 只保留一个
<?php $redis->sadd('s-key1','A','B','C','D','E'); $redis->sadd('s-key2','E','G','C','F','E'); var_dump($redis->sunion('s-key1','s-key2'));
//array(7) { [0]=> string(1) "C" [1]=> string(1) "A" [2]=> string(1) "G" [3]=> string(1) "F" [4]=> string(1) "B" [5]=> string(1) "D" [6]=> string(1) "E" }
SUNIONSTORE
将两个集合的并集放入另一个集合里。
<?php $redis->sadd('s-key1','A','B','C','D','E'); $redis->sadd('s-key2','E','G','C','F','E'); var_dump($redis->sUnionStore('store','s-key1','s-key2')); //int(7) var_dump($redis->smembers('store')); //array(7) { [0]=> string(1) "C" [1]=> string(1) "A" [2]=> string(1) "G" [3]=> string(1) "F" [4]=> string(1) "B" [5]=> string(1) "D" [6]=> string(1) "E" }
SDIFF
返回两个集合的差集,注意是存在于第一个参数集合里的元素
<?php $redis->sadd('s-key1','A','B','C','D','E'); $redis->sadd('s-key2','E','G','C','F','E'); var_dump($redis->sdiff('s-key1','s-key2')); //返回数组,该数组元素是存在于s-key1集合而不存在于集合s-key2 //array(3) { [0]=> string(1) "A" [1]=> string(1) "B" [2]=> string(1) "D" }
<?php $redis->sadd('s-key1','A','B','C','D','E'); $redis->sadd('s-key2','E','G','C','F','E'); $redis->sadd('s-key3','A','B','H','R','J'); var_dump($redis->sdiff('s-key1','s-key2','s-key3')); //返回数组,该数组元素是存在于s-key1集合而不存在于集合s-key2,s-key3 //array(1) { [0]=> string(1) "D" }
SDIFFSTORE
将两个集合的差集放入另一个集合里。
<?php $redis->sadd('s-key1','A','B','C','D','E'); $redis->sadd('s-key2','E','G','C','F','E'); var_dump($redis->sdiffstore('store','s-key1','s-key2')); //int(3) var_dump($redis->smembers('store')); //array(3) { [0]=> string(1) "A" [1]=> string(1) "B" [2]=> string(1) "D" }