sorted sets是有序集合,它在set的基础上增加了顺序属性,可理解为有两列的mysql表。
- zadd
添加元素score,member(如果值相同则更新排序号)
127.0.0.1:6379> srandmember myset3
"five"
127.0.0.1:6379> srandmember myset3
"five"
127.0.0.1:6379> srandmember myset3
"four"
127.0.0.1:6379> srandmember myset3
"five"
127.0.0.1:6379> zadd myzset 1 one
(integer) 1
127.0.0.1:6379> zadd myzset 2 two
(integer) 1
127.0.0.1:6379> zadd myzset 3 two
(integer) 0
127.0.0.1:6379> zrange myzset 0 -1 withscores
1) "one"
2) "1"
3) "two"
4) "3"
- zrem
删除
127.0.0.1:6379> zrange myzset 0 -1 withscores
1) "one"
2) "1"
3) "two"
4) "3"
127.0.0.1:6379> zrem myzset one
(integer) 1
127.0.0.1:6379> zrange myzset 0 -1 withscores
1) "two"
2) "3"
- zincrby
对顺序号进行增加(存在元素则增加顺序号,不存在则创建元素)
127.0.0.1:6379> zrange myzset 0 -1 withscores
1) "two"
2) "3"
127.0.0.1:6379> zincrby myzset 2 two
"5"
127.0.0.1:6379> zrange myzset 0 -1 withscores
1) "two"
2) "5"
127.0.0.1:6379> zincrby myzset 2 one
"2"
127.0.0.1:6379> zrange myzset 0 -1 withscores
1) "one"
2) "2"
3) "two"
4) "5"
- zrank
按照元素的score从小到大进行排序,排序后返回查找元素的索引值
127.0.0.1:6379> zrange myzset 0 -1 withscores
1) "one"
2) "2"
3) "two"
4) "5"
127.0.0.1:6379> zadd myzset 4 three
(integer) 1
127.0.0.1:6379> zrange myzset 0 -1 withscores
1) "one"
2) "2"
3) "three"
4) "4"
5) "two"
6) "5"
127.0.0.1:6379> zrank myzset three
(integer) 1
- zrevrank(
(与zrank相反,降序) - zrevrange
降序排序
127.0.0.1:6379> zrange myzset 0 -1 withscores
1) "one"
2) "2"
3) "three"
4) "4"
5) "two"
6) "5"
127.0.0.1:6379> zrank myzset three
(integer) 1
127.0.0.1:6379> zrevrange myzset 0 -1 withscores
1) "two"
2) "5"
3) "three"
4) "4"
5) "one"
6) "2"
- zrangebyscore
排序后返回score在给定范围内的元素
127.0.0.1:6379> zrange myzset 0 -1 withscores
1) "one"
2) "2"
3) "three"
4) "4"
5) "two"
6) "5"
127.0.0.1:6379> zrangebyscore myzset 2 4 withscores
1) "one"
2) "2"
3) "three"
4) "4"
- zcount
score在给定范围内的数量
127.0.0.1:6379> zrange myzset 0 -1 withscores
1) "one"
2) "1"
3) "two"
4) "2"
5) "three"
6) "3"
7) "four"
8) "4"
9) "five"
10) "5"
127.0.0.1:6379> zcount myzset 2 3
(integer) 2
- zcard
返回所有元素个数
127.0.0.1:6379> zrange myzset 0 -1 withscores
1) "one"
2) "1"
3) "two"
4) "2"
5) "three"
6) "3"
7) "four"
8) "4"
9) "five"
10) "5"
127.0.0.1:6379> zcount myzset 2 3
(integer) 2
127.0.0.1:6379> zcard myzset
(integer) 5
- zremrangebyrank
删除给定区间的元素(区间指索引的区间)
不要与zremrangebyscore方法混淆(它的区间指score下标的区间)
127.0.0.1:6379> zrange myzset 0 -1 withscores
1) "one"
2) "1"
3) "two"
4) "2"
5) "three"
6) "3"
7) "four"
8) "4"
9) "five"
10) "5"
127.0.0.1:6379> zremrangebyrank myzset 2 3
(integer) 2
127.0.0.1:6379> zrange myzset 0 -1 withscores
1) "one"
2) "1"
3) "two"
4) "2"
5) "five"
6) "5"