本文共 11454 字,大约阅读时间需要 38 分钟。
redis中存集合
is an open-source, in-memory key-value data store. in Redis are collections of strings stored at a given key. When held in a set, an individual record value is referred to as a member. Unlike lists, sets are unordered and do not allow repeated values.
是一个开源的内存中键值数据存储。 在Redis的是储存在一个给定的钥匙串的集合。 当保存在一个集合中时,单个记录值称为成员 。 与列表不同,集合是无序的,并且不允许重复的值。
This tutorial explains how to create sets, retrieve and remove members, and compare the members of different sets.
本教程说明如何创建集合,检索和删除成员以及比较不同集合的成员。
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 . 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 — , for example — the exact output of certain commands may differ.
本指南中显示的命令已在运行Redis版本4.0.9的Ubuntu 18.04服务器上进行了测试。 要设置类似的环境,您可以按照我们的指南 步骤1进行操作。 我们将通过使用Redis命令行界面redis-cli
运行它们来演示这些命令的行为。 请注意,如果您使用其他Redis界面(例如 ,则某些命令的确切输出可能会有所不同。
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 . Then, you must either or in order to connect to the Managed Database over TLS.
另外,您可以提供一个托管的Redis数据库实例来测试这些命令,但是请注意,根据数据库提供者所允许的控制级别,本指南中的某些命令可能无法按所述方式工作。 要配置DigitalOcean托管数据库,请遵循我们的 。 然后, 您必须 或 才能通过TLS连接到托管数据库。
The sadd
command allows you to create a set and add one or more members to it. The following example will create a set at a key named key_horror
with the members "Frankenstein"
and "Godzilla"
:
sadd
命令允许您创建一个集合并向其中添加一个或多个成员。 以下示例将在名为key_horror
的键处创建一个带有成员"Frankenstein"
和"Godzilla"
的集合:
If successful, sadd
will return an integer showing how many members it added to the set:
如果成功, sadd
将返回一个整数,显示它添加到集合中的成员数量:
Output (integer) 2
If you try adding members of a set to a key that’s already holding a non-set value, it will return an error. The first command in this block creates a named key_action
with one element, "Shaft"
. The next command tries to add a set member, "Shane"
, to the list, but this produces an error because of the clashing data types:
如果您尝试将集合的成员添加到已经持有非集合值的键中,它将返回错误。 此块中的第一个命令创建一个名为key_action
的 ,其中包含一个元素"Shaft"
。 下一条命令尝试将集合成员"Shane"
添加到列表中,但是由于数据类型冲突而产生错误:
Output (error) WRONGTYPE Operation against a key holding the wrong kind of value
Note that sets don’t allow more than one occurrence of the same member:
请注意,集合不允许同一成员出现多次:
Output (integer) 4
Even though this sadd
command specifies eight members, it discards four of the duplicate "Mad"
members resulting in a set size of 4.
即使此sadd
命令指定了八个成员,它sadd
丢弃重复的"Mad"
成员中的四个,从而将大小设置为4。
In this section, we’ll go over a number of Redis commands that return information about the members held in a set. To practice the commands outlined here, run the following command, which will create a set with six members at a key called key_stooges
:
在本节中,我们将介绍一些Redis命令,这些命令返回有关集合中持有的成员的信息。 要练习此处概述的命令,请运行以下命令,该命令将在名为key_stooges
的键处创建一个包含六个成员的key_stooges
:
To return every member from a set, run the smembers
command followed by the key you want to inspect:
要返回集合中的每个成员,请运行smembers
命令,然后运行要检查的密钥:
Output 1) "Curly"2) "Moe"3) "Larry"4) "Shemp"5) "Curly Joe"6) "Joe"
To check if a specific value is a member of a set, use the sismember
command:
要检查特定值是否是集合的成员,请使用sismember
命令:
If the element "Harpo"
is a member of the key_stooges
set, sismember
will return 1
. Otherwise, it will return 0
:
如果元素"Harpo"
是key_stooges
集的成员,则sismember
将返回1
。 否则,它将返回0
:
Output (integer) 0
To see how many members are in a given set (in other words, to find the cardinality of a given set), run scard
:
要查看给定集合中有多少成员(换句话说,找到给定集合的基数 ),请运行scard
:
Output (integer) 6
To return a random element from a set, run srandmember
:
要从集合中返回随机元素,请运行srandmember
:
Output "Larry"
To return multiple random, distinct elements from a set, you can follow the srandmember
command with the number of elements you want to retrieve:
要从集合中返回多个随机的,不同的元素,可以在srandmember
命令后加上要检索的元素数量:
Output 1) "Larry"2) "Moe"3) "Curly Joe"
If you pass a negative number to srandmember
, the command is allowed to return the same element multiple times:
如果将负数传递给srandmember
,则该命令可以多次返回相同的元素:
Output 1) "Shemp"2) "Curly Joe"3) "Curly Joe"
The random element function used in srandmember
is not perfectly random, although its performance improves in larger data sets. See for more details.
尽管在较大的数据集中其性能srandmember
提高,但在srandmember
使用的随机元素函数并非完全随机。 有关更多详细信息,请参见 。
Redis comes with three commands used to remove members from a set: spop
, srem
, and smove
.
Redis附带了三个用于从集合中删除成员的命令: spop
, srem
和smove
。
spop
randomly selects a specified number of members from a set and returns them, similar to srandmember
, but then deletes them from the set. It accepts the name of the key containing a set and the number of members to remove from the set as arguments. If you don’t specify a number, spop
will default to returning and removing a single value.
spop
从集合中随机选择指定数量的成员并返回它们,类似于srandmember
,然后从集合中删除它们。 它接受包含集合的键的名称和要从集合中删除的成员数作为参数。 如果您未指定数字,则spop
将默认返回并删除单个值。
The following example command will remove and return two randomly-selected elements from the key_stooges
set created in the previous section:
以下示例命令将从上一节创建的key_stooges
集中删除并返回两个随机选择的元素:
Output 1) "Shemp"2) "Larry"
srem
allows you to remove one or more specific members from a set, rather than random ones:
srem
允许您从集合中删除一个或多个特定成员,而不是随机成员:
Instead of returning the members removed from the set, srem
returns an integer showing how many members were removed:
srem
返回从集合中删除的成员, srem
返回一个整数,显示已删除的成员数:
Output (integer) 2
Use smove
to move a member from one set to another. This command accepts as arguments the source set, the destination set, and the member to move, in that order. Note that smove
only allows you to move one member at a time:
使用smove
将成员从一组移到另一组。 此命令以该顺序接受源集,目标集和要移动的成员作为参数。 请注意, smove
仅允许您一次移动一个成员:
If the command moves the member successfully, it will return (integer) 1
:
如果命令成功移动成员,它将返回(integer) 1
:
Output (integer) 1
If smove
fails, it will instead return (integer) 0
. Note that if the destination key does not already exist, smove
will create it before moving the member into it.
如果smove
失败,它将返回(integer) 0
。 请注意,如果目标密钥尚不存在,则smove
会在将成员移入之前创建它。
Redis also provides a number of commands that find the differences and similarities between sets. To demonstrate how these work, this section will reference three sets named presidents
, kings
, and beatles
. If you’d like to try out the commands in this section yourself, create these sets and populate them using the following sadd
commands:
Redis还提供了许多命令来查找集合之间的差异和相似性。 为了演示如何工作的,这部分将引用一个名为三套presidents
, kings
,和beatles
。 如果您想亲自尝试本节中的命令,请创建这些集合并使用以下sadd
命令填充它们:
sinter
compares different sets and returns the set intersection, or values that appear in every set:
sinter
比较不同的集合并返回集合相交或出现在每个集合中的值:
Output 1) "John"2) "George"
sinterstore
performs a similar function, but instead of returning the intersecting members it creates a new set at the specified destination containing these intersecting members. Note that if the destination already exists, sinterstore
will overwrite its contents:
sinterstore
执行类似的功能,但不返回相交成员,而是在包含这些相交成员的指定目的地创建一个新集合。 请注意,如果目的地已经存在,则sinterstore
将覆盖其内容:
Output 1) "John"2) "George"
sdiff
returns the set difference — members resulting from the difference of the first specified set from each of the following sets:
sdiff
返回集合差异 —成员是由以下每个集合的第一个指定集合的差异得出的:
Output 1) "Thomas"
In other words, sdiff
looks at each member in the first given set and then compares those to members in each successive set. Any member in the first set that also appears in the following sets is removed, and sdiff
returns the remaining members. Think of it as removing members of subsequent sets from the first set.
换句话说, sdiff
查看第一个给定集合中的每个成员,然后将其与每个连续集合中的成员进行比较。 第一组中也出现在以下组中的任何成员都将被删除,并且sdiff
返回其余成员。 可以将其视为从第一组中删除后续组的成员。
sdiffstore
performs a function similar to sdiff
, but instead of returning the set difference it creates a new set at a given destination, containing the set difference:
sdiffstore
执行与sdiff
类似的功能,但不返回集合差异,而是在给定的目的地创建一个包含集合差异的新集合:
Output 1) "Paul"2) "Ringo"
Like sinterstore
, sdiffstore
will overwrite the destination key if it already exists.
与sinterstore
一样, sdiffstore
将覆盖目标密钥(如果已存在)。
sunion
returns the set union, or a set containing every member of every set you specify:
sunion
返回集合union或包含您指定的每个集合的每个成员的集合:
Output 1) "Thomas"2) "George"3) "Paul"4) "Henry"5) "James"6) "Edward"7) "John"8) "Ringo"
sunion
treats the results like a new set in that it only allows one occurrence of any given member.
sunion
像对待新结果一样对待结果,因为它只允许出现任何给定成员。
sunionstore
performs a similar function, but creates a new set containing the set union at a given destination instead of just returning the results:
sunionstore
执行类似的功能,但是会在给定的目的地创建一个包含集合联合的新集合,而不仅仅是返回结果:
Output (integer) 8
As with sinterstore
and sdiffstore
, sunionstore
will overwrite the destination key if it already exists.
与sinterstore
和sdiffstore
, sunionstore
将覆盖目标密钥(如果已存在)。
This guide details a number of commands used to create and manage 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 .
有关Redis命令的更多信息,请参阅关于系列教程。
翻译自:
redis中存集合
转载地址:http://aihgb.baihongyu.com/