redis基础数据格式 - 有序集合
程序员文章站
2024-03-17 18:53:58
...
添加,查看
//zadd 往集合里面添加一个或多个成员
127.0.0.1:6379> zadd myzset 1 "a"
(integer) 1
127.0.0.1:6379> zadd myzset 2 "b"
(integer) 1
127.0.0.1:6379> zadd myzset 3 "c" 4 "d" 5 "c"
(integer) 2
// zrange 查看指定下标区间的成员,其中成员的位置按分数值递增(从小到大)来排序
// zrevrange 按从大到小排序
127.0.0.1:6379> zrange myzset 0 -1
1) "a"
2) "b"
3) "d"
4) "c"
127.0.0.1:6379> zrange myzset 0 -1 withscores
1) "a"
2) "1"
3) "b"
4) "2"
5) "d"
6) "4"
7) "c"
8) "5"
// 指定区间 分数的 成员数量
127.0.0.1:6379> zcount myzset 1 3
(integer) 2
// 获取指定成员分数
127.0.0.1:6379> zscore myzset a
"1"
// 获取指定成员排名(从小到大) zrevrank(从大到小),从0 开始
127.0.0.1:6379> zrank myzset b
(integer) 1
127.0.0.1:6379> zrank myzset a
(integer) 0
127.0.0.1:6379> zrank myzset c
(integer) 3
// 获取集合中成员数量
127.0.0.1:6379> zcard myzset
(integer) 4
// zlexcount 指定区间内成员数量
127.0.0.1:6379> zrange myzset 0 -1 withscores
1) "a"
2) "1"
3) "b"
4) "2"
5) "d"
6) "4"
7) "c"
8) "5"
127.0.0.1:6379> zlexcount myzset - +
(integer) 4
127.0.0.1:6379> zlexcount myzset (b [f
(integer) 2
127.0.0.1:6379> zlexcount myzset [b [f
(integer) 3
// Zrangebylex 通过字典区间返回有序集合的成员
127.0.0.1:6379> zrangebylex myzset - +
1) "a"
2) "b"
3) "d"
4) "c"
127.0.0.1:6379> zrangebylex myzset (b [f
1) "d"
2) "c"
127.0.0.1:6379> zrangebylex myzset [b [f
1) "b"
2) "d"
3) "c"
//Zrangebyscore 返回有序集合中指定分数区间的成员列表。有序集成员按分数值递增(从小到大)次序排列。
具有相同分数值的成员按字典序来排列(该属性是有序集提供的,不需要额外的计算)
// 分数所有整数
127.0.0.1:6379> zrangebyscore myzset -inf +inf
1) "a"
2) "b"
3) "d"
4) "c"
// 分数 5 < score < 10
127.0.0.1:6379> zrangebyscore myzset (5 (10
(empty array)
// 分数 1 < score < 10
127.0.0.1:6379> zrangebyscore myzset (1 (10
1) "b"
2) "d"
3) "c"
// 分数 1 < score <= 4
127.0.0.1:6379> zrangebyscore myzset (1 4
1) "b"
2) "d"
127.0.0.1:6379> zrangebyscore myzset (1 4 withscores
1) "b"
2) "2"
3) "d"
4) "4"
移除
//Zrem 命令用于移除有序集中的一个或多个成员,不存在的成员将被忽略
127.0.0.1:6379> zrange myzset 0 -1 withscores
1) "a"
2) "1"
3) "b"
4) "2"
5) "d"
6) "4"
7) "c"
8) "5"
127.0.0.1:6379> zrem myzset a
(integer) 1
127.0.0.1:6379> zrange myzset 0 -1 withscores
1) "b"
2) "2"
3) "d"
4) "4"
5) "c"
6) "5"
// 移除下标范围的成员 zremrangebyrank
127.0.0.1:6379> zrange myzset 0 -1 withscores
1) "b"
2) "2"
3) "d"
4) "4"
5) "c"
6) "5"
127.0.0.1:6379> zremrangebyrank myzset 0 1
(integer) 2
127.0.0.1:6379> zrange myzset 0 -1 withscores
1) "c"
2) "5"
//Zremrangebylex 命令用于移除有序集合中给定的字典区间的所有成员。
127.0.0.1:6379> zrange myzset 0 -1 withscores
1) "a"
2) "1"
3) "b"
4) "2"
5) "c"
6) "3"
7) "d"
8) "4"
127.0.0.1:6379> zrangebylex myzset [a [b
1) "a"
2) "b"
127.0.0.1:6379> zremrangebylex myzset [a [b
(integer) 2
127.0.0.1:6379> zrange myzset 0 -1 withscores
1) "c"
2) "3"
3) "d"
4) "4"
//Zremrangebyscore 命令用于移除有序集中,指定分数(score)区间内的所有成员
127.0.0.1:6379> zrange myzset 0 -1 withscore
(error) ERR syntax error
127.0.0.1:6379> zrange myzset 0 -1 withscores
1) "c"
2) "3"
3) "d"
4) "4"
127.0.0.1:6379> zrangebyscore myzset (3 4
1) "d"
127.0.0.1:6379> zremrangebyscore myzset (3 4
(integer) 1
127.0.0.1:6379> zrange myzset 0 -1 withscores
1) "c"
2) "3"
更多其他命令信息参照官方文档:https://www.redis.net.cn/order/3620.html