ZRANGE && ZREVRANGE

ZRANGE && ZREVRANGE

  • 返回有序集中,指定区间内的成员。其中成员的位置按分数值递增(从小到大/从大到小)来排序。
  • 使用ZRANGE时,具有相同分数值的成员按字典序(lexicographical order)来排列。
  • 使用ZREVRANGE时,具有相同分数值的成员按字典序的逆序(reverse lexicographical order)来排列。

Command

$ redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> zrange s2 0 -1
(empty list or set)
127.0.0.1:6379> ZADD fruit 1 cherry 2 plum 2 pear 3 grape
(integer) 4
127.0.0.1:6379> ZRANGE fruit 0 -1 WITHSCORES
1) "cherry"
2) "1"
3) "pear"
4) "2"
5) "plum"
6) "2"
7) "grape"
8) "3"
127.0.0.1:6379> ZREVRANGE fruit 0 -1 WITHSCORES
1) "grape"
2) "3"
3) "plum"
4) "2"
5) "pear"
6) "2"
7) "cherry"
8) "1"

Code

ZREVRANGE就只是ZRANGE相反,此处便不用代码示例

func zrange(c redis.Conn) {
    defer c.Do("DEL", "fruit")
    c.Do("ZADD", "fruit", 1, "cherry", 2, "plum", 2, "pear", 3, "grape")
    membersWithScore, _ := redis.Strings(c.Do("ZRANGE", "fruit", 0, -1, "WITHSCORES"))
    fmt.Println("If some members' scores are same, will sort them by lexicographical order.")
    for i, v := range membersWithScore {
        if i%2 == 0 {
            fmt.Println("member is:", v)
        } else {
            fmt.Println("Score is:", v)
        }
    }
}

Output

$ go run main.go
If some members' scores are same, will sort them by lexicographical order.
member is: cherry
Score is: 1
member is: pear
Score is: 2
member is: plum
Score is: 2
member is: grape
Score is: 3
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Redis 有序集合和集合一样也是string类型元素的集合,且不允许重复的成员。不同的是每个元素都会关联一个do...
    佛心看世界阅读 3,440评论 1 0
  • 有序集合对象的编码可以是ziplist或者skiplistziplist编码的压缩列表对象使用压缩列表作为底层实现...
    猪大金阅读 2,985评论 0 0
  • 和Sets相比,SortedSets增加了一个权重参数score,使得集合中的元素能够按score进行有序排列, ...
    架构飞毛腿阅读 9,351评论 0 0
  • 游戏中存在各种各样的排行榜,比如玩家的等级排名、分数排名等。玩家在排行榜中的名次是其实力的象征,位于榜单前列的玩家...
    ChinaLeee阅读 4,911评论 0 15
  • 游戏中存在各种各样的排行榜,比如玩家的等级排名、分数排名等。玩家在排行榜中的名次是其实力的象征,位于榜单前列的玩家...
    Gundy_阅读 10,722评论 0 11

友情链接更多精彩内容