Redis笔记(Sorted-set有序集合)

Sorted-set的相关命令可以通过help @sorted_set查看

127.0.0.1:6379> help @sorted_set

ZADD key [NX|XX] [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

ZINCRBY key increment member
summary: Increment the score of a member in a sorted set
since: 1.2.0

ZINTERSTORE destination numkeys key [key ...] [WEIGHTS 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

ZRANGE key start stop [WITHSCORES]
summary: Return a range of members in a sorted set, by index
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

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

ZUNIONSTORE destination numkeys key [key ...] [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX]
summary: Add multiple sorted sets and store the resulting sorted set in a new key
since: 2.0.0

sorted-set与普通set的区别是有score值

使用ZADD命令添加成员,比如分别添加数学,语文,英语的成绩

在内存中是按照score分值从小到大排列的

Image for post
Image for post

可以通过ZRANGE来查询

Image for post

还可以通过withscores选项来取出分值

Image for post

还可以使用ZRANGEBYSCORE通过分值范围来取值,比如取出0到59之间的课程

Image for post

命令加上REV是按照物理内存顺序反向取值,比如取出分数最高的两门课程

Image for post

ZSCORE可以取出一个成员的分值,比如取出数学的分值

Image for post

ZRANK可以取出一个成员的排名(分值从小到大)

  • 注意:ZREVRANK反向排名同理ZREVRANGE
Image for post

ZINCRBY可以对一个成员的分值进行增减,比如给数学加4分

Image for post

使用ZCARD命令可以查询sorted-set中成员的数量

Image for post
Image for post

使用ZCOUNT命令可以查询score范围内的成员数量,比如查询分值是0到60之间的课程数量

Image for post
Image for post

Sorted-set也支持集合交集,并集,差集操作,和普通set的差异在于分值score的处理

相同成员的分值可以按照权重相加,取最大值,取最小值

底层数据结构为skip-list(跳跃表)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store