/* * *sets集合* */
$sdd2 = $redis->sAdd("sets", "user1", "user2", "user3"); //集合数据添加(不重复)
$sadd3 = $redis->sAdd("sets1", "user1", "user3");
$scar = $redis->sCard("sets"); //集合长度替代老版本sSize
$sdiff = $redis->sDiff("sets", "sets1"); //计算差集 返回array user2
$sdiffstore = $redis->sDiffStore("diff", "sets", "sets1"); //计算差集保存到store集合中返回差集数量 可多个比较
$sinter = $redis->sInter("sets", "sets1"); //返回交集 返回array
$sinterstore = $redis->sInterStore("inter", "sets", "sets1"); //返回交集到一个集合中
$smembers = $redis->sMembers("inter"); //返回集合中所有内容
$sismember = $redis->sIsMember("sets", "user1"); //判断某个值是否存在集合中返回bool
$smove = $redis->sMove("sets", "sets1", "user2"); //将user2移动到sets1中
$spop = $redis->sPop("sets"); //随机弹出一个值并返回
$randmember = $redis->sRandMember("sets1"); //随机获取一个值(不删除)
$srem = $redis->sRem("sets1", "user1"); //集合中删除一个键值
$sets1 = $redis->sMembers("sets1"); //返回集合中所有内容
$sunion = $redis->sUnion("sets1", "sets"); //获取集合中的并集(参数可数组)
$sunionstore = $redis->sUnionStore("unionstore", "sets1", "sets"); //获取多个集合并集并写入到一个新集合中unionstore
$redis->setOption(\Redis::OPT_SCAN, \Redis::SCAN_RETRY); //不设置有可能返回空数组while会判断false停止执行设置后空数组不返回而继续执行
$it = NULL;
while ($arr = $redis->sScan("sets", $it, "user*")) { //sscan扫描结果集遍历
foreach ($arr as $k => $v) {
// echo $k . "=" . $v . "\n";
}
}
/* * *Sorted sets 有序集合* */
$zadd = $redis->zAdd("zset", 5, "user5"); //向已排序的集合添加一个或更新一个新值
$zadd = $redis->zAdd("zset", 3, "user3");
$zadd = $redis->zAdd("zset", 8, "user8");
$zadd = $redis->zAdd("zset", 1, "user1");
$zadd = $redis->zAdd("zset1", 5, "user1");
$zadd = $redis->zAdd("zset1", 3, "user5");
$zadd = $redis->zAdd("zset", 4, "user3"); //更新值
//$bzpopmax=$redis->bzPopMax("zset",1);//1秒后弹出最大的值
//$bzpopmin=$redis->bzPopMin("zset",1);//1秒后弹出最小的值
// $zcard=$redis->zCard("zset");//集合数量 zsize替代 返回int
$zcount = $redis->zCount("zset", 1, 5); //返回1-5分的数量
// $zincrby=$redis->zIncrBy("zset",30,"user1");//给user1增加30
$zinterstore = $redis->zInterStore("zinterstore", ["zset", "zset1"], [2, 3], "max"); //取两个集合交集并且按权重排序和排序规则
//$zpopmin=$redis->zPopMin("zset",1);//弹窗最低分
//$zpopmax=$redis->zPopMax("zset",1);//弹窗最高分
//$zrangebyscore=$redis->zRangeByScore("zset",3,5,['withscores' => TRUE, 'limit' => [0, 2]]);//取3-5分之间的数据关联key-val 重0取2个
$zrank = $redis->zRank("zset", "user3"); //获取某个用户排名第几位 zrank正序数 zrevrank倒数
// $zrem=$redis->zRem("zset","user1","user5");//删除集合中数据
// $remrangebyrank=$redis->zRemRangeByRank("zset",0,2);//删除zset中0-2的key索引删除
$zremrangebyscore = $redis->zRemRangeByScore("zset", 0, 4); //按分数删除 区间数据
$zunionstore = $redis->zUnionStore("bzset", ["zset", "zset1"], [1, 1]); //并集排序1*5 1*1 后面是集合乘后排序
$zrange = $redis->zRange("bzset", 0, -1, ['withscores' => TRUE]); //获取集合范围值 withscores 关联数组user5=>5 否则返回 0=>user5
$zrevrange = $redis->zRevRange("zset", 0, -1, ['withscores' => TRUE]); //获取集合范围值以相反的顺序
$zscore = $redis->zScore("zset", "user5"); //返回指定key分数 5
$it = NULL;
$redis->setOption(\Redis::OPT_SCAN, \Redis::SCAN_RETRY);
while ($arr = $redis->zScan("zset", $it, "user*")) { //遍历有序集合
foreach ($arr as $k => $v) {
// echo "$k = $v \n";
}
}