博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
redis中存集合_如何在Redis中管理集合
阅读量:2516 次
发布时间:2019-05-11

本文共 11454 字,大约阅读时间需要 38 分钟。

redis中存集合

介绍 (Introduction)

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.

本教程说明如何创建集合,检索和删除成员以及比较不同集合的成员。

如何使用本指南 (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 . 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连接到托管数据库。

创建集合 (Creating Sets)

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"的集合:

  • sadd key_horror "Frankenstein" "Godzilla"

    sadd key_horror“科学怪人”“哥斯拉”

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"添加到列表中,但是由于数据类型冲突而产生错误:

  • rpush key_action "Shaft"

    rpush key_action“轴”
  • sadd key_action "Shane"

    sadd key_action“ 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:

请注意,集合不允许同一成员出现多次:

  • sadd key_comedy "It's" "A" "Mad" "Mad" "Mad" "Mad" "Mad" "World"

    sadd key_comedy“是”“ A”“疯狂”“疯狂”“疯狂”“疯狂”“疯狂”“世界”
Output   
(integer) 4

Even though this saddcommand specifies eight members, it discards four of the duplicate "Mad" members resulting in a set size of 4.

即使此sadd命令指定了八个成员,它sadd丢弃重复的"Mad"成员中的四个,从而将大小设置为4。

从集合中检索成员 (Retrieving Members from Sets)

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

  • sadd key_stooges "Moe" "Larry" "Curly" "Shemp" "Joe" "Curly Joe"

    sadd key_stooges“ Moe”“ Larry”“ Curly”“ Shemp”“ Joe”“ Curly Joe”

To return every member from a set, run the smembers command followed by the key you want to inspect:

要返回集合中的每个成员,请运行smembers命令,然后运行要检查的密钥:

  • smembers key_stooges

    成员key_stooges
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命令:

  • sismember key_stooges "Harpo"

    sismember key_stooges“ Harpo”

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

  • scard key_stooges

    伤痕累累的key_stooges
Output   
(integer) 6

To return a random element from a set, run srandmember:

要从集合中返回随机元素,请运行srandmember

  • srandmember key_stooges

    资深会员key_stooges
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命令后加上要检索的元素数量:

  • srandmember key_stooges 3

    资深会员key_stooges 3
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 ,则该命令可以多次返回相同的元素:

  • srandmember key_stooges -3

    srandmember key_stooges -3
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使用的随机元素函数并非完全随机。 有关更多详细信息,请参见 。

从集合中删除成员 (Removing Members from Sets)

Redis comes with three commands used to remove members from a set: spop, srem, and smove.

Redis附带了三个用于从集合中删除成员的命令: spopsremsmove

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集中删除并返回两个随机选择的元素:

  • spop key_stooges 2

    spop key_stooges 2
Output   
1) "Shemp"2) "Larry"

srem allows you to remove one or more specific members from a set, rather than random ones:

srem允许您从集合中删除一个或多个特定成员,而不是随机成员:

  • srem key_stooges "Joe" "Curly Joe"

    srem key_stooges“ Joe”“ Curly Joe”

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仅允许您一次移动一个成员:

  • smove key_stooges key_jambands "Moe"

    smove key_stooges key_jambands“ Moe”

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会在将成员移入之前创建它。

比较集 (Comparing Sets)

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还提供了许多命令来查找集合之间的差异和相似性。 为了演示如何工作的,这部分将引用一个名为三套presidentskings ,和beatles 。 如果您想亲自尝试本节中的命令,请创建这些集合并使用以下sadd命令填充它们:

  • sadd presidents "George" "John" "Thomas" "James"

    悲伤的总统“乔治”“约翰”“托马斯”“詹姆斯”
  • sadd kings "Edward" "Henry" "John" "James" "George"

    悲伤的国王“爱德华”“亨利”“约翰”“詹姆斯”“乔治”
  • sadd beatles "John" "George" "Paul" "Ringo"

    披头士乐队的披头士“约翰”“乔治”“保罗”“林戈”

sinter compares different sets and returns the set intersection, or values that appear in every set:

sinter比较不同的集合并返回集合相交或出现在每个集合中的值:

  • sinter presidents kings beatles

    烧结总统国王披头士乐队
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将覆盖其内容:

  • sinterstore new_set presidents kings beatles

    sinterstore新总统国王披头士乐队
  • smembers new_set

    成员new_set
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返回集合差异 —成员是由以下每个集合的第一个指定集合的​​差异得出的:

  • sdiff presidents kings beatles

    总统国王披头士乐队
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类似的功能,但不返回集合差异,而是在给定的目的地创建一个包含集合差异的新集合:

  • sdiffstore new_set beatles kings presidents

    sdiffstore newset披头士乐队国王总统
  • smembers new_set

    成员new_set
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或包含您指定的每个集合的每个成员的集合:

  • sunion presidents kings beatles

    Sunion总统国王披头士乐队
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执行类似的功能,但是会在给定的目的地创建一个包含集合联合的新集合,而不仅仅是返回结果:

  • sunionstore new_set presidents kings beatles

    sunionstore新总统国王披头士乐队
Output   
(integer) 8

As with sinterstore and sdiffstore, sunionstore will overwrite the destination key if it already exists.

sinterstoresdiffstoresunionstore将覆盖目标密钥(如果已存在)。

结论 (Conclusion)

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/

你可能感兴趣的文章
linux系统目录结构
查看>>
git
查看>>
btn按钮之间事件相互调用
查看>>
Entity Framework 4.3.1 级联删除
查看>>
codevs 1163:访问艺术馆
查看>>
冲刺Noip2017模拟赛3 解题报告——五十岚芒果酱
查看>>
并查集
查看>>
sessionStorage
查看>>
代码示例_进程
查看>>
Java中关键词之this,super的使用
查看>>
人工智能暑期课程实践项目——智能家居控制(一)
查看>>
前端数据可视化插件(二)图谱
查看>>
kafka web端管理工具 kafka-manager【转发】
查看>>
获取控制台窗口句柄GetConsoleWindow
查看>>
Linux下Qt+CUDA调试并运行
查看>>
51nod 1197 字符串的数量 V2(矩阵快速幂+数论?)
查看>>
OKMX6Q在ltib生成的rootfs基础上制作带QT库的根文件系统
查看>>
zabbix
查看>>
多线程基础
查看>>
完美解决 error C2220: warning treated as error - no ‘object’ file generated
查看>>