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命令。