**
**
**redis 五大类型之zset有序集合
基本命令:
zadd 给有序集合添加一个成员和分支
zrem 删除一个有序集合的成员
zcard 集合数量
zcount 返回分支介于min和max中间的成员数量
zincrby 给集合成员加上value值
zrank 返回成员在集合中的位置
zscore 返回成员的分值
zrange 返回集合的元素,0 -1 为所有成员
zrangesbycore返回集合中给定分值的成员
服务端redis.php扩展
/*
* zadd 给有序集合添加一个成员和分支
*/
public function zAdd($name,$val,$key)
{
return $this->handler->zadd($name,$val,$key);
}
/*
* zrem 删除一个有序集合的成员
*/
public function zRem($name,$key)
{
$num = 0;
if(is_array($key)){
foreach ($key as $row){
$num+= $this->handler->zrem($name,$row);
}
return $num;
}else{
return $this->handler->zrem($name,$key);
}
}
/*
* zcard 集合数量
*/
public function zCart($name)
{
return $this->handler->zcard($name);
}
/*
* zincrby 给集合成员加上value值
*/
public function zIncRby($name,$val,$key)
{
return $this->zincrby($name,$val,$key);
}
/*
* zcount 返回分支介于min和max中间的成员数量
*/
public function zCount($name,$min=0,$max=100000000)
{
return $this->handler->zount($name,$min,$max);
}
/*
* zrank 返回成员在集合中的位置
*/
public function zRank($name,$key)
{
return $this->handler->zrank($name,$key);
}
/*
* zscore 返回成员的分值
*/
public function zScore($name,$key)
{
return $this->handler->zscore($name,$key);
}
/*
* zrange 返回集合的元素,0 -1 为所有成员
*/
public function zRange($name,$start = 0 , $over = -1 ,$withscores)
{
if($withscores){
return $this->handler->zrange($name,$start,$over,$withscores);
}else{
return $this->handler->zrange($name,$start,$over);
}
}
/*
* zrangescore 返回集合中给定分值的成员
*/
public function zRangeByScore($name,$start = 0 , $over = 100000000 ,$withscores)
{
if($withscores){
return $this->handler->zrangebyscore($name,$start,$over,$withscores);
}else{
return $this->handler->zrangebyscore($name,$start,$over);
}
}
1.zadd ,zrem ,zcount ,zrange
客户端请求
public function redis()
{
$redis= new Redis();
$redis->zAdd('demo','10','v1');
$redis->zAdd('demo','20','v2');
$redis->zAdd('demo','30','v3');
dump($redis->zRange('demo'));
echo $redis->zCount('demo');
$redis->zRem('demo','v3');
dump($redis->zRange('demo'));
}
结果:
array(3) {
[0] => string(2) "v1"
[1] => string(2) "v2"
[2] => string(2) "v3"
}
3
array(2) {
[0] => string(2) "v1"
[1] => string(2) "v2"
}
2.zincrby ,zscore
客户端请求
public function redis()
{
$redis= new Redis();
echo $redis->zScore('demo','v1');
$redis->zIncRby('demo','10','v1');
echo $redis->zScore('demo','v1');
}
结果
10
20
3.zcard ,zrank
客户端请求
public function redis()
{
$redis= new Redis();
echo $redis->zCart('demo');
echo $redis->zrank ('demo','v1');
}
结果
2
1
4.zrangescore
客户端请求
public function redis()
{
$redis= new Redis();
dump($redis->zRangeByScore('demo','10','20'));
}
结果
v1