如何使用Redis排序列表

A sorted set associates a rank to each item in a set.

排序的集合将等级与集合中的每个项目相关联。

Sorted sets work in a similar way to sets, and they use similar commands, except S is now Z, for example:

排序集合的工作方式与集合类似,并且它们使用类似的命令,但S现在是Z ,例如:

  • SADD -> ZADD

    SADD - > ZADD

  • SPOP -> ZPOP

    SPOP - > ZPOP

But they are slightly different.

但是它们略有不同。

ZADD accepts a score:

ZADD接受分数

ZADD names 1 "Flavio"
ZADD names 2 "Syd"
ZADD names 2 "Roger"

As you can see, values must still be unique, but now they are associated to a score.

如您所见,值仍然必须是唯一的,但是现在它们已与得分相关联。

The score does not have to be unique.

分数不必是唯一的。

Items in a set are always sorted by the score.

集合中的项目始终按分数排序。

This is very useful to implement some kind of data storage tool like (usual example) a leaderboard. Or to indicate the time some item was added, with a timestamp.

这对于实现某种类型的数据存储工具(例如(页首)排行榜)非常有用。 或指示带有时间戳的添加项目的时间。

You can get the score of an item using ZRANK:

您可以使用ZRANK获取项目的得分:

ZRANK names "Flavio"

List all items in a sorted set using ZRANGE, which works similarly to LRANGE in lists:

使用ZRANGE列出排序集中的所有项目,其作用类似于列表中的LRANGE

ZRANGE names 0 -1

Add WITHSCORES to also return the scores information:

添加WITHSCORES还返回分数信息:

You can increment the score of an item in the set using ZINCRBY.

您可以使用ZINCRBY来增加项目集中的ZINCRBY

See all the sorted sets commands here.

在此处查看所有排序的set命令。

翻译自: https://flaviocopes.com/redis-sorted-lists/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值