【redis】1-10-5 Redis数据类型 - zset
目录
1. 内容概要
- zset:有序set(sorted set,值不重复 , 默认升序)
1.1 总结
指令 | 描述 |
redis-cli | 使用命令行工具 |
auth xxxxxx | 登录redis,验证密码 xxxxxx |
zadd key [NX|XX] [CH] [INcR] score member [score member ...] 创建zset | |
zadd key score member [score member ...] | 创建zset(多组) >zadd zset 10 duck 20 pig 30 chicken 40 beef 50 sheep (integer) 5 >zadd zset 10 duck 20 pig 30 chicken 40 beef 50 sheep (integer) 0
|
zrange key start stop [withscores] | 查看zset 的 member 127.0.0.1:6379> zrange zset 0 -1 1) "duck" 2) "pig" 3) "chicken" 4) "beef" 5) "sheep" |
查看zset的member + score 127.0.0.1:6379> zrange zset 0 -1 withscores 1) "duck'" 2) "10" 3) "pig" 4) "20" 5) "chicken" 6) "30" 7) "beef" 8) "40" 9) "sheep'" 10) "50" | |
默认升序 | 127.0.0.1:6379> zadd zset 25 abc 35 xyz (integer) 2 127.0.0.1:6379> zrange zset 0 -1 withscores 1) "duck" 2) "10" 3) "pig" 4) "20" 5) "abc" 6) "25" 7) "chicken" 8) "30" 9) "xyz" 10) "35" 11) "beef" 12) "40" 13) "sheep" 14) "50" |
zscore key member | 获取分数(scores) 127.0.0.1:6379> zscore zset beef "40" |
zcard key | 获取zset长度 127.0.0.1:6379> zcard zset (integer) 7 |
获取分数区间项目(总数) 127.0.0.1:6379> zcount zset 20 40 (integer) 5 | |
zrangebyscore key min max [WITHSCORES] [LIMIT offset count] | 获取分数区间项目 [30,40] 127.0.0.1:6379> zrangebyscore zset 30 40 1) "pig" 2) "abc" 3) "chicken" 4) "xyz" 5) "beef |
[20,40) 127.0.0.1:6379> zrangebyscore zset 20 (40 withscores 1) "pig' 2) "20" 3) "abc" 4) "25" 5) "chicken" 6) "30" 7) "xyz" 8) "35" | |
(20,40) 127.0.0.1:6379> zrangebyscore zSet (20 (40 withscores 1) "abc" 2) "25 3) "chicken" 4) "30" 5) "xyz" 6) "35" | |
在结果集的基础上,限制起始下标+显示总数 127.0.0.1:6379> ZRANGEBYSC0RE zset 20 40 limit 1 2 1) "abc" 2) "chicken" 127.0.0.1:6379> zrangebyscore ZSet 20 40 1) "pig" 2) "abc" 3) "chicken" 4) "xyz" 5) "beef" 127.0.0.1:6379> zrangebyscore zset 20 40 limit 2 3 1) "chicken" 2) "xyz" 3) "beef" | |
zrem key member [member ...] | 删除member(多个) 127.0.0.1:6379> zrem zset pig (integer) 1 127.0.0.1:6379> zrange zset 0 -1 withscores 1) "duck" 2) "10" 3) "abc" 4) "25" 5) "chicken" 6) "30*" 7) "xyz" 8) "35" 9) "beef" 10) "40" 11) "sheep" 12) "50" 127.0.0.1:6379> zrem zset sheep beef (integer) 2 |