set无序集合
Redis中的set是string类型的集合,集合成员是唯一的,所以里面不会有重复的元素。
Redis中集合是通过哈希列表来实现的,所以添加,删除,查找的复杂度都是O(1)。
连接redis
$redis = new \Redis;
$redis->connect('127.0.0.1',6379);
//查看服务是否运行
echo "Server is running: " . $redis->ping();
set集合命令
-
sadd($key,$value1,$value2,...$valueN);
向集合添加一个或多个成员,成功返回成功添加成员的个数,失败返回false$redis->sadd('user1','tag1','tag2','tag3','tag1','tag2','tag3');
-
sMembers($key);
获取集合中某个key的所有成员$result =$redis->sMembers('user1'); //返回 array ( 0 => 'tag2', 1 => 'tag1', 2 => 'tag3', )
-
sGetMembers($key);
同sMembers($key);
-
sCard($key);
获取集合中成员个数$result =$redis->sCard('user1');//3
-
sIsMeber($key,$value);
判断value是否在set中,返回true/false$result =$redis->sIsMember('user1','tag6');//fasle
-
sRem($key,$value1,$value2,...$valueN)
删除集合中一个或多个成员,返回成功删除成员的个数$result=$redis->sRem('user1','tag2','tag3');//2
-
sPop($key);
随机删除集合中的一个成员,并将他返回$redis->sadd('user1','tag2','tag3','tag4','tag5'); $result1 =$redis->sPop('user1');//tag1 $result =$redis->sMembers('user1');//array ( 0 => 'tag4', 1 => 'tag2', 2 => 'tag5', 3 => 'tag3', )
-
sRandMember($key,[count]);
随机返回集合中指定个数的成员,默认是1个$result =$redis->sRandMember('user1',2);//array ( 0 => 'tag5', 1 => 'tag4', )
-
sDiff($key1,$key2,...,$keyN)
返回集合1与集合2,集合N之间的差集所谓差集是存在与第一个key的集合且均不存在与任何其他集合的成员
//两个集合比较 $redis->sAdd('user1',1,2,3,4,5,6,7); $redis->sAdd('user2',1,6,7); $result = $redis->sDiff('user1','user2');//array ( 0 => '2', 1 => '3', 2 => '4', 3 => '5', )
//三个集合比较 $redis->del('user1','user2','user3','user4'); $redis->sAdd('user1',1,2,3,4,5,6,7); $redis->sAdd('user2',1,6,7); $redis->sAdd('user3',7,8,9); $result = $redis->sDiff('user1','user2','user3');//array ( 0 => '2', 1 => '3', 2 => '4', 3 => '5', )
-
sDiffStore($dstKey,$key1,$key2...$keyN)
将集合的差集存到指定集合中$result =$redis->sDiffStore('dst','user1','user2'); var_export($result);//4 代表4个元素 $result=$redis->sMembers('dst'); var_export($result);//( 0 => '2', 1 => '3', 2 => '4', 3 => '5', )
-
sUnion($key1,$key2,...$keyN);
返回N个集合的合集$result =$redis->sUnion('user1','user3');//array ( 0 => '1', 1 => '2', 2 => '3', 3 => '4', 4 => '5', 5 => '6', 6 => '7', 7 => '8', 8 => '9', )
-
sUnionStore($dstKey,$key1,$key2...$keyN);
将N个集合的合集存到指定集合中$result =$redis->sUnionStore('dst','user1','user2'); var_export($result);//7 代表7个元素 $result=$redis->sMembers('dst'); var_export($result);// ( 0 => '1', 1 => '2', 2 => '3', 3 => '4', 4 => '5', 5 => '6', 6 => '7', )
-
·
sInter($key1,$key2,...$keyN);
求N个集合的交集$result=$redis->sInter('user1','user2'); var_export($result);//array ( 0 => '1', 1 => '6', 2 => '7', )
-
sInterStore($dstKey,$key1,$key2...$keyN);
将N个集合的交集存到指定集合$result =$redis->sInterStore('dst','user1','user2');//3 var_export($result);//3 代表3个元素 $result=$redis->sMembers('dst'); var_export($result);// ( 0 => '1', 1 => '6', 2 => '7', )
-
sScan($key,$interator,$pattern='',$count=0)
迭代集合中的键元素,sScan继承自scan$redis->del('user1'); $redis->sAdd('user1','tag1','tag2','tag3','ugg','bm'); $iterator=null; while ($v = $redis->sScan('user1',$iterator,'tag*',2)){ var_export($v); var_export("iterator=$iterator<br>"); //array ( 0 => 'tag2', )'iterator=2 //'array ( 0 => 'tag1', )'iterator=3 //'array ( 0 => 'tag3', )'iterator=0 }
set适用场景
- 校园网求两个用户好友的差集,sDiff, 做可能认识的人
- sInter求共同好友
- sismember命令可以判断A是否是B的好友
- srandmember 随机展示数据
网友评论