本文共 8525 字,大约阅读时间需要 28 分钟。
is an open-source, in-memory key-value data store. In Redis, a is a collection of strings sorted by insertion order, similar to . This tutorial covers how to create and work with elements in Redis lists.
是一个开源的内存中键值数据存储。 在Redis中, 是按插入顺序排序的字符串的集合,类似于 。 本教程介绍了如何在Redis列表中创建和使用元素。
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连接到托管数据库。
A key can only hold one list, although any list can hold over four billion elements. Redis reads lists from left to right, and you can add new list elements to the head of a list (the “left” end) with the lpush
command or the tail (the “right” end) with rpush
. You can also use lpush
or rpush
to create a new list:
密钥只能包含一个列表,尽管任何列表都可以包含40亿个元素。 Redis的读取列表由左到右,你可以添加新的列表元素的列表(“左”端)与头lpush
命令或尾巴(“正确”的年底)与rpush
。 您还可以使用lpush
或rpush
创建新列表:
lpush key value
按键 值
Both commands will output an integer showing how many elements are in the list. To illustrate, run the following commands to create a list containing the dictum “I think therefore I am”:
这两个命令都将输出一个整数,以显示列表中有多少个元素。 为了说明这一点,请运行以下命令以创建包含“我认为是我”的格言的列表:
The output from the last command will read:
最后一条命令的输出将显示为:
Output (integer) 5
Note that you can add multiple list elements with a single lpush
or rpush
statement:
请注意,您可以使用单个lpush
或rpush
语句添加多个列表元素:
The lpushx
and rpushx
commands are also used to add elements to lists, but will only work if the given list already exists. If either command fails, it will return (integer) 0
:
lpushx
和rpushx
命令还用于将元素添加到列表,但是仅在给定列表已经存在的情况下才起作用。 如果任何一个命令失败,它将返回(integer) 0
:
Output (integer) 0
To change an existing element in a list, run the lset
command followed by the key name, the index of the element you want to change, and the new value:
要更改列表中的现有元素,请运行lset
命令,后跟键名,要更改的元素的索引以及新值:
If you try adding a list element to an existing key that does not contain a list, it will lead to a clash in data types and return an error. For example, the following set
command creates a key holding a string, so the following attempt to add a list element to it with lpush
will fail:
如果尝试将list元素添加到不包含列表的现有键中,则它将导致数据类型冲突并返回错误。 例如,以下set
命令创建一个包含字符串的键,因此以下使用lpush
向其添加列表元素的尝试将失败:
Output (error) WRONGTYPE Operation against a key holding the wrong kind of value
It isn’t possible to convert Redis keys from one data type to another, so to turn key_philosophy3
into a list you would need to delete the key and start over with an lpush
or rpush
command.
不可能将Redis密钥从一种数据类型转换为另一种数据类型,因此要将key_philosophy3
转换为列表,您需要删除该密钥并使用lpush
或rpush
命令重新开始。
To retrieve a range of items in a list, use the lrange
command followed by a start and a stop offset. Each offset is a zero-based index, meaning that 0
represents the first element in the list, 1
represents the next, and so on.
要检索列表中的项目范围,请使用lrange
命令,后跟一个开始和一个停止偏移量。 每个偏移量都是从零开始的索引,这意味着0
表示列表中的第一个元素, 1
表示下一个元素,依此类推。
The following command will return all the elements from the example list created in the previous section:
以下命令将从上一节创建的示例列表中返回所有元素:
Output 1) "I"2) "think"3) "therefore"4) "I"5) "am"6) "sayeth"7) "Rene"8) "Decartes"
The offsets passed to lrange
can also be negative numbers. When used in this case, -1
represents the final element in the list, -2
represents the second-to-last element in the list, and so on. The following example returns the last three elements of the list held in key_philosophy1
:
传递给lrange
的偏移量也可以为负数。 在这种情况下使用时, -1
表示列表中的最后一个元素, -2
表示列表中的倒数第二个元素,依此类推。 以下示例返回key_philosophy1
中保存的列表的最后三个元素:
Output 1) "I"2) "am"3) "sayeth"
To retrieve a single element from a list, you can use the lindex
command. However, this command requires you to supply the element’s index as an argument. As with lrange
, the index is zero-based, meaning that the first element is at index 0
, the second is at index 1
, and so on:
要从列表中检索单个元素,可以使用lindex
命令。 但是,此命令要求您提供元素的索引作为参数。 与lrange
,索引从零开始,这意味着第一个元素在索引0
,第二个元素在索引1
,依此类推:
Output "am"
To find out how many elements are in a given list, use the llen
command, which is short for “list length”:
要查找给定列表中有多少个元素,请使用llen
命令,该命令是“ l ist len gth”的缩写:
Output (integer) 8
If the value stored at the given key does not exist, llen
will return an error.
如果存储在给定键上的值不存在,则llen
将返回错误。
The lrem
command removes the first of a defined number of occurrences that match a given value. To experiment with this, create the following list:
lrem
命令除去与给定值匹配的已定义次数的第一个。 要对此进行试验,请创建以下列表:
The following lrem
example will remove the first occurence of the value "Live"
:
以下lrem
示例将删除值"Live"
的首次出现:
This command will output the number of elements removed from the list:
此命令将输出从列表中删除的元素数:
Output (integer) 1
The number passed to an lrem
command can also be negative. The following example will remove the last two occurences of the value "Never"
:
传递给lrem
命令的数字也可以为负数。 以下示例将删除值"Never"
的最后两个出现:
Output (integer) 2
The lpop
command removes and returns the first, or “leftmost” element from a list:
lpop
命令删除并返回列表中的第一个或“最左边”的元素:
Output "Never"
Likewise, to remove and return the last or “rightmost” element from a list, use rpop
:
同样,要从列表中删除并返回最后或“最右边”的元素,请使用rpop
:
Output "Dies"
Redis also includes the rpoplpush
command, which removes the last element from a list and pushes it to the beginning of another list:
Redis还包括rpoplpush
命令,该命令从列表中删除最后一个元素,并将其推入另一个列表的开头:
Output "Tomorrow"
If the source and destination keys passed to rpoplpush
command are the same, it will essentially rotate the elements in the list.
如果传递给rpoplpush
命令的源键和目标键相同,则它将实质上旋转列表中的元素。
This guide details a number of commands that you can use to create and manage lists 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命令的更多信息,请参阅关于系列教程。
翻译自:
转载地址:http://kshgb.baihongyu.com/