有序集合 zset
zet 一般操作
// 1.增 : zadd key score member [ score member ... ]
// redis3.2为zadd命令添加了nx、xx、ch、incr四个选项
// 2.计算成员个数 zcard key
// 3.计算某个成员分数 zscore key member
// 4.计算成员排名 zrank key member zrevrank key member
// 5.删除成员 zrem key member [ member ... ]
// 6.增加成员分数 zincrby key increment member
// 7.返回指定排名范围的成员(加上withscores同时返回成员分数)
// 低到高:zrange key start end [withscores]
// 高到低:zrevrange key start end [withscores]
// 8.返回指定分数范围的成员(加上withscores同时返回成员分数, [limit offset count]可以限制输出的起始位置和个数)
// zrangebyscore key min max [withscores] [limit offset count]
// zrevrangebyscore key max min [withscores] [limit offset count]
// min和max还支持开区间(小括号)和闭区间(中括号),-inf、+inf 分别代表无限小和无限大
// 9.返回指定分数范围成员个数 zcount key min max
// 10.删除指定排名内的升序元素 zremrangebyrank key start end
// 11.删除指定分数范围的成员 zremrangebyscore key min max
$k31 = 'imei:zset:test1';
$k32 = 'imei:zset:test2';
$redis->del($k31);
$redis->del($k32);
$redis->zadd($k31, 1, 'a', '2', 'b', '3', 'c');
$redis->zadd($k32, 100, 'a', '12', 'b', '23', 'c', 33, 'd', 4, 'e', 4, 'f');
$res1 = $redis->zrange($k31, 0, 100);
print_r($res1);// Array ( [0] => a [1] => b [2] => c )
$res2 = $redis->zcard($k31);
var_dump($res2);// string(1) "3"
$res3 = $redis->zscore($k31, 'a');
var_dump($res3);// string(1) "1"
$res4 = $redis->zrank($k31, 'a');
var_dump($res4);// string(1) "0"
$res5 = $redis->zrem($k31, 'b');
var_dump($res5);//string(1) "1"
$redis->zincrby($k31, 100, 'b');
$res6 = $redis->zscore($k31, 'b');
var_dump($res6);// string(3) "100"
$res7 = $redis->zrange($k31, 0, 100);
print_r($res7);// Array ( [0] => a [1] => b [2] => c )
$res8 = $redis->zrangebyscore($k32, '-inf', "+inf");
print_r($res8);// Array ( [0] => a [1] => c [2] => b )
$res90 = $redis->zcount($k32, '4', 5);
var_dump($res90);// string(1) "2"
$res91 = $redis->zcount($k32, '(4', 5);
var_dump($res91);// string(1) "0"
$res10 = $redis->zremrangebyrank($k31, 0, 1);
var_dump($res10);// string(1) "2"
print_r($redis->zrangebyscore($k31, '-inf', "+inf"));// Array ( [0] => b )
$res11 = $redis->zremrangebyscore($k32, 10, 30);
var_dump($res11);// string(1) "2"
print_r($redis->zrangebyscore($k32, '-inf', "+inf"));// Array ( [0] => e [1] => f [2] => d [3] => a )
zset 集合间操作
$k31 = 'imei:zset:test1';
$k32 = 'imei:zset:test2';
$k33 = 'imei:zset:test3';
$k34 = 'imei:zset:test4';
$k35 = 'imei:zset:test5';
$redis->del($k31);
$redis->del($k32);
$redis->del($k33);
$redis->del($k35);
$redis->zadd($k31, 1, 'kris', 91, 'mike', 200, 'frank', 220, 'tim', 250, 'martin', 251, 'tom');
$redis->zadd($k32, 8, 'james', 77, 'mike', 625, 'martin', 888, 'tom');
//print_r($redis->zrange($k31, 0, -1));//
//print_r($redis->zrange($k32, 0, -1));//
// 1.交集
// zinterstore destination numkeys key[ key ... ] [weights weght [weight ... ]] [aggregate sum|min|max]
// 参数说明:
// destination : 交集计算结果保存到这个键
// numkeys : 需要做交集计算的键的个数
// key[ key ... ] : 需要做交集计算的键
// weights weght [weight ... ] : 每个键的权重,在做交集计算时,每个键中的每个member会将自己的分数乘以这个权重,默认权重是1
// aggregate sum|min|max : 计算成员交集后,分值可以按照sum,min,max做汇总,默认是sum
var_dump($redis->zinterstore($k33, 2, $k31, $k32));// 3
print_r($redis->zrange($k33, 0, -1, 'withscores'));// [ mike ,168 , martin , 875 , tom , 1139 ]
var_dump($redis->zinterstore($k34, 2, $k31, $k32, 'weights', 1, 0.5, 'aggregate', 'max'));// 3
print_r($redis->zrange($k34, 0, -1, 'withscores'));// [ mike ,91 , martin , 312.5 , tom , 444 ]
// 2.并集
// zunionstore destination numkeys key[ key ... ] [weights weght [weight ... ]] [aggregate sum|min|max]
// 参数与 zinterstore 一致,只不过是并集计算
var_dump($redis->zunionstore($k35, 2, $k31, $k32));// 7
print_r($redis->zrange($k35, 0, -1, 'withscores'));//[kris,1,james,8,mike,168,frank,200,tim,220,martin,875,tom,1139]