sorted set是redis存储数据的一种结构,即有序的set。其中每一个元素,除了元素自身,还包括元素的分数score、以及元素的正负向索引。需要注意的是,sorted set中的所有元素,会按照分数从低到高在内存中以跳跃表的形式排序。如果分数一样,会按照字典序排序。
sorted set的命令如下:
127.0.0.1:6379> help @sorted-set
BZMPOP timeout numkeys key [key ...] MIN|MAX [COUNT count]
summary: Remove and return members with scores in a sorted set or block until one is available
since: 7.0.0
BZPOPMAX key [key ...] timeout
summary: Remove and return the member with the highest score from one or more sorted sets, or block until one is available
since: 5.0.0
BZPOPMIN key [key ...] timeout
summary: Remove and return the member with the lowest score from one or more sorted sets, or block until one is available
since: 5.0.0
ZADD key [NX|XX] [GT|LT] [CH] [INCR] score member [score member ...]
summary: Add one or more members to a sorted set, or update its score if it already exists
since: 1.2.0
ZCARD key
summary: Get the number of members in a sorted set
since: 1.2.0
ZCOUNT key min max
summary: Count the members in a sorted set with scores within the given values
since: 2.0.0
ZDIFF numkeys key [key ...] [WITHSCORES]
summary: Subtract multiple sorted sets
since: 6.2.0
ZDIFFSTORE destination numkeys key [key ...]
summary: Subtract multiple sorted sets and store the resulting sorted set in a new key
since: 6.2.0
ZINCRBY key increment member
summary: Increment the score of a member in a sorted set
since: 1.2.0
ZINTER numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX] [WITHSCORES]
summary: Intersect multiple sorted sets
since: 6.2.0
ZINTERCARD numkeys key [key ...] [LIMIT limit]
summary: Intersect multiple sorted sets and return the cardinality of the result
since: 7.0.0
ZINTERSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]
summary: Intersect multiple sorted sets and store the resulting sorted set in a new key
since: 2.0.0
ZLEXCOUNT key min max
summary: Count the number of members in a sorted set between a given lexicographical range
since: 2.8.9
ZMPOP numkeys key [key ...] MIN|MAX [COUNT count]
summary: Remove and return members with scores in a sorted set
since: 7.0.0
ZMSCORE key member [member ...]
summary: Get the score associated with the given members in a sorted set
since: 6.2.0
ZPOPMAX key [count]
summary: Remove and return members with the highest scores in a sorted set
since: 5.0.0
ZPOPMIN key [count]
summary: Remove and return members with the lowest scores in a sorted set
since: 5.0.0
ZRANDMEMBER key [count [WITHSCORES]]
summary: Get one or multiple random elements from a sorted set
since: 6.2.0
ZRANGE key start stop [BYSCORE|BYLEX] [REV] [LIMIT offset count] [WITHSCORES]
summary: Return a range of members in a sorted set
since: 1.2.0
ZRANGEBYLEX key min max [LIMIT offset count]
summary: Return a range of members in a sorted set, by lexicographical range
since: 2.8.9
ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]
summary: Return a range of members in a sorted set, by score
since: 1.0.5
ZRANGESTORE dst src min max [BYSCORE|BYLEX] [REV] [LIMIT offset count]
summary: Store a range of members from sorted set into another key
since: 6.2.0
ZRANK key member
summary: Determine the index of a member in a sorted set
since: 2.0.0
ZREM key member [member ...]
summary: Remove one or more members from a sorted set
since: 1.2.0
ZREMRANGEBYLEX key min max
summary: Remove all members in a sorted set between the given lexicographical range
since: 2.8.9
ZREMRANGEBYRANK key start stop
summary: Remove all members in a sorted set within the given indexes
since: 2.0.0
ZREMRANGEBYSCORE key min max
summary: Remove all members in a sorted set within the given scores
since: 1.2.0
ZREVRANGE key start stop [WITHSCORES]
summary: Return a range of members in a sorted set, by index, with scores ordered from high to low
since: 1.2.0
ZREVRANGEBYLEX key max min [LIMIT offset count]
summary: Return a range of members in a sorted set, by lexicographical range, ordered from higher to lower strings.
since: 2.8.9
ZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count]
summary: Return a range of members in a sorted set, by score, with scores ordered from high to low
since: 2.2.0
ZREVRANK key member
summary: Determine the index of a member in a sorted set, with scores ordered from high to low
since: 2.0.0
ZSCAN key cursor [MATCH pattern] [COUNT count]
summary: Incrementally iterate sorted sets elements and associated scores
since: 2.8.0
ZSCORE key member
summary: Get the score associated with the given member in a sorted set
since: 1.2.0
ZUNION numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX] [WITHSCORES]
summary: Add multiple sorted sets
since: 6.2.0
ZUNIONSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]
summary: Add multiple sorted sets and store the resulting sorted set in a new key
since: 2.0.0
示例如下:
zadd:zadd key score1 member1 score2 member2...。添加key和sorted set为键值对,向sorted set中添加value1,分数为score1,value2 score2...
zrange :zrange key start end [withscores],返回索引start到end位置的所有元素,正序(分数低到高)返回。
加上withsocres可以带分数返回。
127.0.0.1:6379> zadd k1 8 apple 2 banana 3 orange
(integer) 3
127.0.0.1:6379> zrange k1 0 -1
1) "banana"
2) "orange"
3) "apple"
127.0.0.1:6379> zrange k1 0 -1 withscores
1) "banana"
2) "2"
3) "orange"
4) "3"
5) "apple"
6) "8"
zrangebyscore:zrangebyscore key score1 score2,返回key对应的sorted set中符合分数score1-score2的元素
127.0.0.1:6379> zrangebyscore k1 3 8
1) "orange"
2) "apple"
zrevrange :zrevrange key start end,类似zrange,但是逆序(分数高到低返回)。
需要注意的是,对于下列的两个命令,同样都是返回了最后的2个元素,但是zrevrange k1 0 1是以逆序返回了从高到低排序的前2个(即apple是第一个),zrange k1 -2 -1是以正序排序返回了从低到高排序的最后2个(即apple是最后一个)
。
127.0.0.1:6379> zrevrange k1 0 1
1) "apple"
2) "orange"
127.0.0.1:6379> zrange k1 -2 -1
1) "orange"
2) "apple"
zrank:zrank key member,返回sorted set中value对应的索引
127.0.0.1:6379> zrank k1 apple
(integer) 2
zincrby: zincrbykey increment member,member对应的分数加incrememt(可能是浮点数,可能是负数)
127.0.0.1:6379> ZINCRBY k1 2.5 banana
"4.5"
127.0.0.1:6379> zrange k1 0 -1 withscores
1) "orange"
2) "3"
3) "banana"
4) "4.5"
5) "apple"
6) "8"
集合操作
zunionstore: zunionstore destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX],numkeys是有几个集合要做合集操作,后面跟对应的key。
因为sorted set和其他结构不同,每个元素都有分数,所以对于分数也有相关的参数可选:weight为在计算合集元素分数时,不同key对应的参与运算的sorted set中元素的权重,按key的顺序与key一一对应。每个key的set都有自己的权重,分数*权重累加得到合集的元素的分数。
最后是这些set的分数可选的操作,默认是累加,aggregate平均,sum求和,min最小值,max最大值。
127.0.0.1:6379> zadd k1 80 tom 60 sean 70 baby
(integer) 3
127.0.0.1:6379> zadd k2 60 tom 100 sean 40 xiaoming
(integer) 3
127.0.0.1:6379> zunionstore unkey 2 k1 k2
(integer) 4
127.0.0.1:6379> zrange unkey 0 -1 withscores
1) "xiaoming"
2) "40"
3) "baby"
4) "70"
5) "tom"
6) "140"
7) "sean"
8) "160"
127.0.0.1:6379> zunionstore unkey1 2 k1 k2 weight 1 0.5
(error) ERR syntax error
127.0.0.1:6379> zunionstore unkey1 2 k1 k2 weights 1 0.5
(integer) 4
127.0.0.1:6379> zrange unkey1 0 -1
1) "xiaoming"
2) "baby"
3) "sean"
4) "tom"
127.0.0.1:6379> zrange unkey1 0 -1 withscores
1) "xiaoming"
2) "20"
3) "baby"
4) "70"
5) "sean"
6) "110"
7) "tom"
8) "110"
127.0.0.1:6379> zunionstore unkey2 2 k1 k2 aggregate max
(integer) 4
127.0.0.1:6379> zrange unkey2 0 -1 withscores
1) "xiaoming"
2) "40"
3) "baby"
4) "70"
5) "tom"
6) "80"
7) "sean"
8) "100"