redis排序_如何在Redis中管理排序集

news/2024/7/8 1:52:20

redis排序

介绍 (Introduction)

Redis is an open-source, in-memory key-value data store. In Redis, sorted sets are a data type similar to sets in that both are non repeating groups of strings. The difference is that each member of a sorted set is associated with a score, allowing them to be sorted from the smallest score to the largest. As with sets, every member of a sorted set must be unique, though multiple members can share the same score.

Redis是一个开源的内存中键值数据存储。 在Redis的, 排序集合类似于一个数据类型集在这两者都是串的非重复的组。 不同之处在于,已排序集中的每个成员都与一个分数相关联,从而可以从最小分数到最大分数进行排序。 与集合一样,排序集合中的每个成员都必须是唯一的,尽管多个成员可以共享同一分数。

This tutorial explains how to create sorted sets, retrieve and remove their members, and create new sorted sets from existing ones.

本教程说明了如何创建排序集,检索和删除其成员以及如何从现有集合中创建新的排序集。

如何使用本指南 (How To Use This Guide)

This guide is written as a cheat sheet with self-contained examples. We encourage you to jump to any section that is relevant to the task you’re trying to complete.

本指南以备有完整示例的备忘单形式编写。 我们鼓励您跳至与您要完成的任务相关的任何部分。

The commands shown in this guide were tested on an Ubuntu 18.04 server running Redis version 4.0.9. To set up a similar environment, you can follow Step 1 of our guide on How To Install and Secure Redis on Ubuntu 18.04. We will demonstrate how these commands behave by running them with redis-cli, the Redis command line interface. Note that if you’re using a different Redis interface — Redli, for example — the exact output of certain commands may differ.

本指南中显示的命令已在运行Redis版本4.0.9的Ubuntu 18.04服务器上进行了测试。 要设置类似的环境,您可以按照我们的指南如何在Ubuntu 18.04上安装和保护Redis的 步骤1进行操作。 我们将通过使用Redis命令行界面redis-cli运行它们来演示这些命令的行为。 请注意,如果您使用其他Redis界面(例如Redli) ,则某些命令的确切输出可能会有所不同。

Alternatively, you could provision a managed Redis database instance to test these commands, but note that depending on the level of control allowed by your database provider, some commands in this guide may not work as described. To provision a DigitalOcean Managed Database, follow our Managed Databases product documentation. Then, you must either install Redli or set up a TLS tunnel in order to connect to the Managed Database over TLS.

另外,您可以提供一个托管的Redis数据库实例来测试这些命令,但是请注意,根据数据库提供者所允许的控制级别,本指南中的某些命令可能无法按所述方式工作。 要配置DigitalOcean托管数据库,请遵循我们的托管数据库产品文档 。 然后, 您必须 安装Redli 设置TLS隧道才能通过TLS连接到托管数据库。

创建排序集并添加成员 (Creating Sorted Sets and Adding Members)

To create a sorted set, use the zadd command. zadd accepts as arguments the name of the key that will hold the sorted set, followed by the score of the member you’re adding and the value of the member itself. The following command will create a sorted set key named faveGuitarists with one member, "Joe Pass", that has a score of 1:

要创建排序集,请使用zadd命令。 zadd接受将保留排序后的键的键的名称作为参数,后跟要添加的成员的分数和成员本身的值。 下面的命令将创建一个名为faveGuitarists的排序集合键,其成员为"Joe Pass" ,其得分为1

  • zadd faveGuitarists 1 "Joe Pass"

    zadd faveGuitarists 1“ Joe Pass”

zadd will return an integer that indicates how many members were added to the sorted set if it was created successfully.

zadd将返回一个整数,该整数指示如果已成功创建排序集,则将多少个成员添加到排序集中。


   
Output
(integer) 1

You can add more than one member to a sorted set with zadd. Note that their scores don’t need to be sequential, there can be gaps between scores, and multiple members held in the same sorted set can share the same score:

您可以使用zadd将多个成员添加到排序集中。 请注意,他们的分数不必是连续的,分数之间可以有间隔,并且相同排序集中的多个成员可以共享相同的分数:

  • zadd faveGuitarists 4 "Stephen Malkmus" 2 "Rosetta Tharpe" 3 "Bola Sete" 3 "Doug Martsch" 8 "Elizabeth Cotten" 12 "Nancy Wilson" 4 "Memphis Minnie" 12 "Michael Houser"

    zadd faveGuitarists 4“斯蒂芬·马克斯姆斯(Stephen Malkmus)” 2“罗塞塔·塔普(Rosetta Tharpe)” 3“博拉赛特(Bola Sete)” 3“道格·马尔奇(Doug Martsch)” 8“伊丽莎白·科腾(Elizabeth Cotten)” 12“南希·威尔逊” 4“孟菲斯·米妮” 12“迈克尔·豪瑟”

   
Output
(integer) 8

zadd can accept the following options, which you must enter after the key name and before the first member score:

zadd可以接受以下选项,您必须在密钥名称之后和第一个成员评分之前输入以下选项:

    • NX: Tells zadd not to update existing members. With this option, zadd will only add new elements.

      NX :告诉zadd 不要更新现有成员。 使用此选项, zadd将仅添加新元素。

    • XX: Tells zadd to only update existing elements. With this option, zadd will never add new members.

      XX :告诉zadd 更新现有元素。 使用此选项, zadd将永远不会添加新成员。

    NX or XX: These options have opposite effects, so you can only include one of them in any zadd operation:

    NXXX :这些选项具有相反的效果,因此您只能在任何zadd操作中包括其中之一:

  • CH: Normally, zadd only returns the number of new elements added to the sorted set. With this option included, though, zadd will return the number changed elements. This includes newly added members and members whose scores were changed.

    CH :通常, zadd仅返回添加到排序集中的新元素的数量。 但是,使用此选项时, zadd将返回更改后的元素个数。 这包括新添加的成员和分数已更改的成员。

  • INCR: This causes the command to increment the member’s score value. If the member doesn’t yet exist, the command will add it to the sorted set with the increment as its score, as if its original score was 0. With INCR included, the zadd will return the member’s new score if it’s successful. Note that you can only include one score/member pair at a time when using this option.

    INCR :这会使命令增加成员的得分值。 如果该成员还不存在,则该命令会将其添加到已排序的集合中,并将其增量作为分数,就好像其原始分数为0 。 包含INCR ,如果成功, zadd将返回成员的新分数。 请注意,使用此选项时,一次只能包含一个分数/成员对。

Instead of passing the INCR option to zadd, you can instead use the zincrby command which behaves the exact same way. Instead of giving the sorted set member the value indicated by the score value like zadd, it increments that member’s score up by that value. For example, the following command increments the score of the member "Stephen Malkmus", which was originally 4, up by 5 to 9.

不必将INCR选项传递给zadd ,而可以使用行为完全相同的zincrby命令。 而不是给排序后的集合成员一个分数值所指示的值(如zadd ,而是将该成员的分数增加该值。 例如,以下命令将原来为4的成员"Stephen Malkmus"的分数增加59

  • zincrby faveGuitarists 5 "Stephen Malkmus"

    zincrby最喜欢Guitarists 5“ Stephen Malkmus”

   
Output
"9"

As is the case with the zadd command’s INCR option, if the specified member doesn’t exist then zincrby will create it with the increment value as its score.

zadd命令的INCR选项一样,如果指定的成员不存在,那么zincrby将使用增量值作为其分数来创建它。

从排序集中检索成员 (Retrieving Members from Sorted Sets)

The most fundamental way to retrieve the members held within a sorted set is to use the zrange command. This command accepts as arguments the name of the key whose members you want to retrieve and a range of members held within it. The range is defined by two numbers that represent zero-based indexes, meaning that 0 represents the first member in the sorted set (or, the member with the lowest score), 1 represents the next, and so on.

检索排序集中的成员的最基本方法是使用zrange命令。 此命令接受要检索其成员的键的名称以及其中包含的成员范围作为参数。 该范围由两个数字定义,这些数字表示从零开始的索引,这意味着0表示排序集中的第一个成员(或得分最低的成员), 1表示下一个,依此类推。

The following example will return the first four members from the faveGuitarists sorted set created in the previous section:

下面的示例将从上一节创建的faveGuitarists排序集中返回前四个成员:

  • zrange faveGuitarists 0 3

    zrange faveGuitarists 0 3

   
Output
1) "Joe Pass" 2) "Rosetta Tharpe" 3) "Bola Sete" 4) "Doug Martsch"

Note that if the sorted set you pass to zrange has two or more elements that share the same score, it will sort those elements in lexicographical, or alphabetical, order.

请注意,如果传递给zrange的排序集具有两个或更多具有相同分数的元素,它将按字典顺序或字母顺序对这些元素进行排序。

The start and stop indexes can also be negative numbers, with -1 representing the last member, -2 representing the second to last, and so on:

开始索引和停止索引也可以是负数, -1代表最后一个成员, -2代表倒数第二个,依此类推:

  • zrange faveGuitarists -5 -2

    zrange faveGuitarists -5 -2

   
Output
1) "Memphis Minnie" 2) "Elizabeth Cotten" 3) "Stephen Malkmus" 4) "Michael Houser"

zrange can accept the WITHSCORES argument which, when included, will also return the members’ scores:

zrange可以接受WITHSCORES参数,当包含该参数时,该参数还将返回成员的分数:

  • zrange faveGuitarists 5 6 WITHSCORES

    zrange faveGuitarists 5 6 WITHSCORES

   
Output
1) "Elizabeth Cotten" 2) "8" 3) "Stephen Malkmus" 4) "9"

zrange can only return a range of members in ascending numerical order. To reverse this and return a range in descending order, you must use the zrevrange command. Think of this command as temporarily reversing the order of the given sorted set before returning the members that fall within the specified range. So with zrevrange, 0 will represent the last member held in the key, 1 will represent the second to last, and so on:

zrange只能按数字升序返回一系列成员。 要反转并以降序返回范围,必须使用zrevrange命令。 可以将此命令视为暂时反转给定排序集的顺序,然后返回属于指定范围内的成员。 因此,对于zrevrange0表示密钥中持有的最后一个成员, 1表示倒数第二个 ,依此类推:

  • zrevrange faveGuitarists 0 5

    zrevrange喜爱的吉他手0 5

   
Output
1) "Nancy Wilson" 2) "Michael Houser" 3) "Stephen Malkmus" 4) "Elizabeth Cotten" 5) "Memphis Minnie" 6) "Doug Martsch"

zrevrange can also accept the WITHSCORES option.

zrevrange也可以接受WITHSCORES选项。

You can return a range of members based on their scores with the zrangebyscore command. In the following example, the command will return any member held in the faveGuitarists key with a score of 2, 3, or 4:

您可以使用zrangebyscore命令根据其分数返回一定范围的成员。 在以下示例中,该命令将返回faveGuitarists密钥中持有的分数为2、3或4的任何成员:

  • zrangebyscore faveGuitarists 2 4

    zrangebyscore最喜欢的吉他手2 4

   
Output
1) "Rosetta Tharpe" 2) "Bola Sete" 3) "Doug Martsch" 4) "Memphis Minnie"

The range is inclusive in this example, meaning that it will return members with scores of 2 or 4. You can exclude either end of the range by preceding it with an open parenthesis ((). The following example will return every member with a score greater than or equal to 2, but less than 4:

在此示例中,范围是包含范围的,这意味着它将返回得分为2或4的成员。您可以在范围的任一端之前加一个开放的括号( ( )。大于或等于2 ,但小于4

  • zrangebyscore faveGuitarists 2 (4

    zrangebyscore喜爱的吉他手2(4

   
Output
1) "Rosetta Tharpe" 2) "Bola Sete" 3) "Doug Martsch"

As with zrange, zrangebyscore can accept the WITHSCORES argument. It also accepts the LIMIT option, which you can use to retrieve only a selection of elements from the zrangebyscore output. This option accepts an offset, which marks the first member in the range that the command will return, and a count, which defines how many members the command will return in total. For example, the following command will look at the first six members of the faveGuitarists sorted set but will only return 3 members from it, starting from the second member in the range, represented by 1:

zrangezrangebyscore可以接受WITHSCORES参数。 它还接受LIMIT选项,您可以使用LIMIT选项zrangebyscore输出中检索元素的选择。 该选项接受一个offset(偏移量)和一个count(计数),该offset标记该命令将返回的范围内的第一个成员,该计数总共定义了该命令将返回的成员数。 例如,以下命令将查看faveGuitarists排序集中的前六个成员, faveGuitarists该集合中的第二个成员开始,仅返回3个成员,以1表示:

  • zrangebyscore faveGuitarists 0 5 LIMIT 1 3

    zrangebyscore喜爱的吉他手0 5 LIMIT 1 3

   
Output
1) "Rosetta Tharpe" 2) "Bola Sete" 3) "Doug Martsch"

The zrevrangebyscore command returns a reversed range of members based on their scores. The following command returns every member of the set with a score between 10 and 6:

zrevrangebyscore命令根据成员的分数返回反向范围。 以下命令将返回分数在10到6之间的集合中的每个成员:

  • zrevrangebyscore faveGuitarists 10 6

    zrevrangebyscore最喜欢的吉他手10 6

   
Output
1) "Stephen Malkmus" 2) "Elizabeth Cotten"

As with zrangebyscore, zrevrangebyscore can accept both the WITHSCORES and LIMIT options. Additionally, you can exclude either end of the range by preceding it with an open parenthesis.

zrangebyscorezrevrangebyscore可以接受WITHSCORESLIMIT选项。 此外,您可以在范围的任一末端加上一个开放的括号,以排除该末端。

There may be times when all the members in a sorted set have the same score. In such a case, you can force redis to return a range of elements sorted lexicographically, or in alphabetical order, with the zrangebylex command. To try out this command, run the following zadd command to create a sorted set where each member has the same score:

有时,排序集中的所有成员都具有相同的分数。 在这种情况下,可以使用zrangebylex命令强制redis返回按字典顺序或字母顺序排序的元素范围。 要尝试此命令,请运行以下zadd命令以创建一个排序集,其中每个成员的得分均相同:

  • zadd SomervilleSquares 0 Davis 0 Inman 0 Union 0 porter 0 magoun 0 ball 0 assembly

    zadd SomervilleSquares 0戴维斯0英曼0联盟0搬运工0 magoun 0球0组件

zrangebylex must be followed by the name of a key, a start interval, and a stop interval. The start and stop intervals must begin with an open parenthesis (() or an open bracket ([), like this:

zrangebylex必须跟键的名称,开始间隔和停止间隔。 开始和停止间隔必须以圆括号( ( )或方括号( [ ))开头,如下所示:

  • zrangebylex SomervilleSquares [a [z

    zrangebylex SomervilleSquares [a [z

   
Output
1) "assembly" 2) "ball" 3) "magoun" 4) "porter"

Notice that this example returned only four of the eight members in the set, even though the command sought a range from a to z. This is because Redis values are case-sensitive, so the members that begin with uppercase letters were excluded from its output. To return those, you could run the following:

请注意,即使该命令查找了从az的范围,该示例也仅返回集合中八个成员中的四个。 这是因为Redis值区分大小写,因此以大写字母开头的成员将从其输出中排除。 要返回这些值,可以运行以下命令:

  • zrangebylex SomervilleSquares [A [z

    zrangebylex SomervilleSquares [A [z

   
Output
1) "Davis" 2) "Inman" 3) "Union" 4) "assembly" 5) "ball" 6) "magoun" 7) "porter"

zrangebylex also accepts the special characters -, which represents negative infinity, and +, which represents positive infinity. Thus, the following command syntax will also return every member of the sorted set:

zrangebylex还接受特殊字符-代表负无穷大, +代表正无穷大。 因此,以下命令语法还将返回排序集中的每个成员:

  • zrangebylex SomervilleSquares - +

    zrangebylex SomervilleSquares-+

Note that zrangebylex cannot return sorted set members in reverse lexicographical (ascending alphabetical) order. To do that, use zrevrangebylex:

请注意, zrangebylex无法以相反的字典顺序(字母升序)返回排序的集合成员。 为此,请使用zrevrangebylex

  • zrevrangebylex SomervilleSquares + -

    zrevrangebylex SomervilleSquares +-

   
Output
1) "porter" 2) "magoun" 3) "ball" 4) "assembly" 5) "Union" 6) "Inman" 7) "Davis"

Because it’s intended for use with sorted sets where every member has the same score, zrangebylex does not accept the WITHSCORES option. It does, however, accept the LIMIT option.

因为它旨在与有序集合,其中每个成员都有相同的比分使用, zrangebylex 接受WITHSCORES选项。 但是,它确实接受LIMIT选项。

检索有关排序集的信息 (Retrieving Information about Sorted Sets)

To find out how many members are in a given sorted set (or, in other words, to determine its cardinality), use the zcard command. The following example shows how many members are held in the faveGuitarists key from the first section of this guide:

要找出给定排序集中有多少个成员(或者换句话说,确定其基数 ),请使用zcard命令。 以下示例显示了本指南第一部分中的faveGuitarists密钥中包含多少个成员:

  • zcard faveGuitarists

    zcard faveGuitarists

   
Output
(integer) 9

zcount can tell you how many elements are held within a given sorted set that fall within a range of scores. The first number following the key is the start of the range and the second one is the end of the range:

zcount可以告诉您在给定的排序集中有多少个元素落在分数范围内。 键后面的第一个数字是范围的开始,第二个数字是范围的结束:

  • zcount faveGuitarists 3 8

    zcount faveGuitarists 3 8

   
Output
(integer) 4

zscore outputs the score of a specified member of a sorted set:

zscore输出排序集中指定成员的分数:

  • zscore faveGuitarists "Bola Sete"

    zscore faveGuitarists“ Bola Sete”

   
Output
"3"

If either the specified member or key don’t exist, zscore will return (nil).

如果指定的成员或密钥都不存在,则zscore将返回(nil)

zrank is similar to zscore, but instead of returning the given member’s score, it instead returns its rank. In Redis, a rank is a zero-based index of the members of a sorted set, ordered by their score. For example, "Joe Pass" has a score of 1, but because that is the lowest score of any member in the key, it has a rank of 0:

zrank类似于zscore ,而不是返回给定成员的分数,而不是它返回它的排名。 在Redis中, 等级是排序集中成员的从零开始的索引,按其得分排序。 例如, "Joe Pass"的得分为1 ,但是由于这是键中任何成员的最低得分,因此其等级为0

  • zrank faveGuitarists "Joe Pass"

    zrank faveGuitarists“ Joe Pass”

   
Output
(integer) 0

There’s another Redis command called zrevrank which performs the same function as zrank, but instead reverses the ranks of the members in the set. In the following example, the member "Joe Pass" has the lowest score, and consequently has the highest reversed rank:

还有一个称为zrevrank的Redis命令,该命令执行与zrank相同的功能,但相反会反转集合中成员的排名。 在下面的示例中,成员"Joe Pass"的得分最低,因此其反向排名最高:

  • zrevrank faveGuitarists "Joe Pass"

    zrevrank faveGuitarists“ Joe Pass”

   
Output
(integer) 8

The only relation between a member’s score and their rank is where their score stands in relation to those of other members. If there is a score gap between two sequential members, that won’t be reflected in their rank. Note that if two members have the same score, the one that comes first alphabetically will have the lower rank.

成员的分数与其等级之间的唯一关系是其分数相对于其他成员的分数所处的位置。 如果两个连续成员之间存在分数差距,则不会在其排名中反映出来。 请注意,如果两个成员的分数相同,则按字母顺序排在第一位的成员将具有较低的排名。

Like zscore, zrank and zrevrank will return (nil) if the key or member doesn’t exist.

zscore一样,如果键或成员不存在,则zrankzrevrank将返回(nil)

zlexcount can tell you how many members are held in a sorted set between a lexicographical range. The following example uses the SomervilleSquares sorted set from the previous section:

zlexcount可以告诉您在词典范围之间的排序集中有多少个成员。 以下示例使用上一部分中的SomervilleSquares排序集:

  • zlexcount SomervilleSquares [M [t

    zlexcount SomervilleSquares [M [t

   
Output
(integer) 5

This command follows the same syntax as the zrangebylex command, so refer to the previous section for details on how to define a string range.

该命令的语法与zrangebylex命令的语法相同,因此,有关如何定义字符串范围的详细信息,请参考上一节 。

从排序集中删除成员 (Removing Members from Sorted Sets)

The zrem command can remove one or more members from a sorted set:

zrem命令可以从排序集中删除一个或多个成员:

  • zrem faveGuitarists "Doug Martsch" "Bola Sete"

    zrem faveGuitarists“ Doug Martsch”“ Bola Sete”

zrem will return an integer indicating how many members it removed from the sorted set:

zrem将返回一个整数,指示从排序集中删除了多少个成员:


   
Output
(integer) 2

There are three Redis commands that allow you to remove members of a sorted set based on a range. For example, if each member in a sorted set has the same score, you can remove members based on a lexicographical range with zremrangebylex. This command uses the same syntax as zrangebylex. The following example will remove every member that begins with a capital letter from the SomervilleSquares key created in the previous section:

有三个Redis命令,可让您根据范围删除排序集的成员。 例如,如果排序集中的每个成员都具有相同的分数,则可以使用zremrangebylex基于词典范围删除成员。 此命令使用与zrangebylex相同的语法。 下面的示例将从上一部分中创建的SomervilleSquares键中删除所有以大写字母开头的成员:

  • zremrangebylex SomervilleSquares [A [Z

    zremrangebylex SomervilleSquares [A [Z

zremrangebylex will output an integer indicating how many members it removed:

zremrangebylex将输出一个整数,指示其删除了多少个成员:


   
Output
(integer) 3

You can also remove members based on a range of scores with the zremrangebyscore command, which uses the same syntax as the zrangebyscore command. The following example will remove every member held in faveGuitarists with a score of 4, 5, or 6:

还可以基于一系列的分数与所述的删除成员zremrangebyscore命令,它使用相同的语法zrangebyscore命令。 以下示例将删除faveGuitarists持有的得分为4、5或6的每个成员:

  • zremrangebyscore faveGuitarists 4 6

    zremrangebyscore喜爱吉他演奏家4 6

   
Output
(integer) 1

You can remove members from a set based on a range of ranks with the zremrangebyrank command, which uses the same syntax as zrangebyrank. The following command will remove the three members of the sorted set with the lowest rankings, which are defined by a range of zero-based indexes:

您可以从基于一系列与队伍的一组删除成员zremrangebyrank命令,该命令使用相同的语法zrangebyrank 。 以下命令将删除排名最低的排序集中的三个成员,这些成员由一系列从零开始的索引定义:

  • zremrangebyrank faveGuitarists 0 2

    zremrangebyrank喜爱的吉他手0 2

   
Output
(integer) 3

Note that numbers passed to remrangebyrank can also be negative, with -1 representing the highest rank, -2 the next highest, and so on.

请注意,传递给remrangebyrank数字也可以为负, -1代表最高等级, -2代表第二最高等级,依此类推。

从现有集合创建新的排序集合 (Creating New Sorted Sets from Existing Ones)

Redis includes two commands that allow you to compare members of multiple sorted sets and create new ones based on those comparisons: zinterstore and zunionstore. To experiment with these commands, run the following zadd commands to create some example sorted sets.

Redis包含两个命令,可用于比较多个排序集的成员并基于这些比较创建新的集合: zinterstorezunionstore 。 要试验这些命令,请运行以下zadd命令以创建一些示例排序集。

  • zadd NewKids 1 "Jonathan" 2 "Jordan" 3 "Joey" 4 "Donnie" 5 "Danny"

    zadd NewKids 1“乔纳森” 2“乔丹” 3“乔伊” 4“唐尼” 5“丹尼”
  • zadd Nsync 1 "Justin" 2 "Chris" 3 "Joey" 4 "Lance" 5 "JC"

    zadd Nsync 1“ Justin” 2“ Chris” 3“ Joey” 4“ Lance” 5“ JC”

zinterstore finds the members shared by two or more sorted sets — their intersection — and produces a new sorted set containing only those members. This command must include, in order, the name of a destination key where the intersecting members will be stored as a sorted set, the number of keys being passed to zinterstore, and the names of the keys you want to analyze:

zinterstore查找两个或多个排序集(它们的交集)共享的成员,并生成仅包含那些成员的新排序集。 此命令必须依次包含相交成员将作为排序集存储的目标键的名称,传递给zinterstore的键的数量以及要分析的键的名称:

  • zinterstore BoyBands 2 NewKids Nsync

    zinterstore BoyBands 2 NewKids Nsync

zinterstore will return an integer showing the number of elements stored to the destination sorted set. Because NewKids and Nsync only share one member, "Joey", the command will return 1:

zinterstore将返回一个整数,该整数显示存储到目标排序集的元素数。 由于NewKidsNsync仅共享一个成员"Joey" ,因此该命令将返回1


   
Output
(integer) 1

Be aware that if the destination key already exists, zinterstore will overwrite its contents.

请注意,如果目标键已经存在,则zinterstore将覆盖其内容。

zunionstore will create a new sorted set holding every member of the keys passed to it. This command uses the same syntax as zinterstore, and requires the name of a destination key, the number of keys being passed to the command, and the names of the keys:

zunionstore将创建一个新的排序集,其中包含传递给它的每个键成员。 此命令使用与zinterstore相同的语法,并且需要目标键的名称,传递给该命令的键的数量以及键的名称:

  • zunionstore SuperGroup 2 NewKids Nsync

    zunionstore SuperGroup 2 NewKids Nsync

Like zinterstore, zunionstore will return an integer showing the number of elements stored in the destination key. Even though both of the original sorted sets held five members, because sorted sets can’t have repeating members and each key has one member named "Joey", the resulting integer will be 9:

zinterstore一样, zunionstore将返回一个整数,该整数显示存储在目标键中的元素数。 即使两个原始排序集都包含五个成员,但由于排序集不能包含重复成员,并且每个键都有一个名为"Joey"成员,因此所得的整数将为9


   
Output
(integer) 9

Like zinterstore, zunionstore will overwrite the contents of the destination key if it already exists.

zinterstore一样, zunionstore将覆盖目标键的内容(如果已存在)。

To give you more control over member scores when creating new sorted sets with zinterstore and zunionstore, both of these commands accept the WEIGHTS and AGGREGATE options.

为了让您对成员分数更多的控制创造新的有序集合与当zinterstorezunionstore ,这两个命令的接受WEIGHTSAGGREGATE的选择。

The WEIGHTS option is followed by one number for every sorted set included in the command which weight, or multiply, the scores of each member. The first number after the WEIGHTS option weights the scores of the first key passed to the command, the second number weights the second key, and so on.

对于每个包含在命令中的排序集, WEIGHTS选项后跟一个数字,该数字加权或乘以每个成员的分数。 WEIGHTS选项之后的第一个数字对传递给命令的第一个键的分数进行加权,第二个数字对第二个键进行加权,依此类推。

The following example creates a new sorted set holding the intersecting keys from the NewKids and Nsync sorted sets. It weights the scores in the NewKids key by a factor of three, and weights those in the Nsync key by a factor of seven:

下面的示例创建一个新的排序集,其中NewKidsNsync排序集的相交键。 它将NewKids密钥中的分数加权三倍,并将Nsync密钥中的分数加权七倍:

  • zinterstore BoyBandsWeighted 2 NewKids Nsync WEIGHTS 3 7

    zinterstore BoyBandsWeighted 2 NewKids Nsync WEIGHTS 3 7

If the WEIGHTS option isn’t included, the weighting defaults to 1 for both zinterstore and zunionstore.

如果WEIGHTS选项中不包含时,加权默认为1两者zinterstorezunionstore

AGGREGATE accepts three sub-options. The first of these, SUM, implements zinterstore and zunionstore’s default behavior by adding the scores of matching members in the combined sets.

AGGREGATE接受三个子选项。 其中的第一个SUM通过在组合集中添加匹配成员的得分来实现zinterstorezunionstore的默认行为。

If you run a zinterstore or zunionstore operation on two sorted sets that share one member, but this member has a different score in each set, you can force the operation to assign the lower of the two scores in the new set with the MIN suboption.

如果在共享一个成员的两个排序集合上运行zinterstorezunionstore操作,但是该成员在每个集合中具有不同的分数,则可以使用MIN子选项强制该操作分配新集合中两个分数中的较低者。

  • zinterstore BoyBandsWeightedMin 2 NewKids Nsync WEIGHTS 3 7 AGGREGATE MIN

    zinterstore BoyBandsWeightedMin 2 NewKids Nsync WEIGHTS 3 7 AGGREGATE MIN

Because the two sorted sets only have one matching member with the same score (3), this command will create a new set with a member that has the lower of the two weighted scores:

因为这两个排序的集合只有一个具有相同分数( 3 )的匹配成员,所以此命令将创建一个新集合,其成员具有两个加权分数中的较低者:

  • zscore BoyBandsWeightedMin "Joey"

    zscore BoyBandsWeightedMin“ Joey”

   
Output
"9"

Likewise, AGGREGATE can force zinterstore or zunionstore to assign the higher of the two scores with the MAX option:

同样, AGGREGATE可以通过MAX选项强制zinterstorezunionstore分配两个分数中的较高者:

  • zinterstore BoyBandsWeightedMax 2 NewKids Nsync WEIGHTS 3 7 AGGREGATE MAX

    zinterstore BoyBandsWeightedMax 2 NewKids Nsync WEIGHTS 3 7 AGGREGATE MAX

This command creates a new set with on one member, "Joey", that has the higher of the two weighted scores:

此命令创建一个新集合,其中一个成员"Joey"具有两个加权得分中的较高者:

  • zscore BoyBandsWeightedMax "Joey"

    zscore BoyBandsWeightedMax“ Joey”

   
Output
"21"

It can be helpful to think of WEIGHTS as a way to temporarily manipulate members’ scores before they’re analyzed. Likewise, it’s helpful to think of the AGGREGATE option as a way to decide how to control members’ scores before they’re added to their new sets.

WEIGHTS视为在分析成员之前临时操纵其分数的一种方式可能会有所帮助。 同样,将AGGREGATE选项视为一种决定如何在将成员添加到新集合中之前决定如何控制其分数的方法将很有帮助。

结论 (Conclusion)

This guide details a number of commands used to create and manage sorted sets in Redis. If there are other related commands, arguments, or procedures you’d like to see outlined in this guide, please ask or make suggestions in the comments below.

本指南详细介绍了用于在Redis中创建和管理排序集的许多命令。 如果您想在本指南中概述其他相关的命令,参数或过程,请在下面的注释中提出疑问或提出建议。

For more information on Redis commands, see our tutorial series on How to Manage a Redis Database.

有关Redis命令的更多信息,请参阅关于如何管理Redis数据库的系列教程。

翻译自: https://www.digitalocean.com/community/cheatsheets/how-to-manage-sorted-sets-in-redis

redis排序


http://www.niftyadmin.cn/n/3649308.html

相关文章

Android版本和API Level的对应关系

在开发Android时,老是不知道Android版本号和对应API level,这个问题真是麻烦,我们在发布声波传输SDK时也遇到这样的问题,版本号是对外发布的版本号,一般都是主版本号.子版本号.修正版本号的命名规则,说白了…

[dotNET]使用HttpWebRequest请求远端服务器时如何加载SSL证书

使用HttpWebRequest请求远端服务器时如何加载SSL证书编写者:郑昀UltraPower首先加上引用“System.Security.DLL”,其次在工程中using System.Security.Cryptography.X509Certificates;这样就可以使用“X509Certificate Class”了,它的定义参见…

JDBC的使用(一)

Java 中的数据存储技术 在Java中,数据库存取技术可分为如下几类: ①JDBC直接访问数据库 ②JDO技术 ③第三方O/R工具,如Hibernate, ibatis 等 JDBC是java访问数据库的基石,JDO, Hibernate等…

若依项目(前后端分离)

最近在基于若依项目二次开发中,遇到表设计问题,然后看了若依想项目前后端分离视频中,也是不太懂表的设计问题

Android常用Adapter代码例子

ArrayAdapter   总是感觉写自己的博客才更能够学到东西,网上尽管有很多好的资料,但是参差不齐,需要浪费大量时间才能够找到最需要的,索性写自己最需要的东西。 Adapter是适配器的意思,在Android中大量的使用到了List…

node中的数据持久化之mongoDB

一、什么是mongoDB MongoDB是一种开源的非关系型数据库,正如它的名字所表示的,MongoDB支持的数据结构非常松散,是一种以bson格式(一种json的存储形式)的文档存储方式为主,支持的数据结构类型更加丰富的NoS…

在JavaScript中将toLocaleString与数字,数组或日期一起使用

toLocaleString is a built-in JavaScript method used to convert the date and time to a string using the system locales. 🤓🚀 toLocaleString是内置JavaScript方法,用于使用系统区域设置将日期和时间转换为字符串。 🤓&…

Android开发过程中的几个小知识点

1. 在程序的Manifest里面对应的Activity里面添加android:windowSoftInputMode"adjustResize"属性,可以实现打开输入法时,界面自动上移,不被输入法遮盖。 2. 添加按钮的按下效果时,可以在drawable文件夹下新建一个xml文件…