1. List
1.1 lindex
1.1.1 基本信息
LINDEX key index
summary: Get an element from a list by its index
since: 1.0.0
Returns the element at index index in the list stored at key. The index is zero-based, so 0 means the first element, 1 the second element and so on. Negative indices can be used to designate elements starting at the tail of the list. Here, -1 means the last element, -2 means the penultimate and so forth.
返回键存储的列表中索引位置的元素。索引是从零开始的,所以0表示第一个元素,1表示第二个元素,依此类推。负索引可用于指定从列表尾部开始的元素。在这里,-1表示最后一个元素,-2表示倒数第二个元素,依此类推。
When the value at key is not a list, an error is returned.
当 key 值不是列表时,将返回错误。
1.1.2 返回
- 请求的元素,或者当 index 超出范围时为 nil
1.1.3 练习
127.0.0.1:6379[1]> lpush mylist "hello"
(integer) 1
127.0.0.1:6379[1]> lpush mylist "world"
(integer) 2
127.0.0.1:6379[1]> lpush mylist "redis"
(integer) 3
127.0.0.1:6379[1]> lpush mylist "flink"
(integer) 4
127.0.0.1:6379[1]> lindex mylist 2
"world"
1.2 linsert
1.2.1 基本信息
LINSERT key BEFORE|AFTER pivot element
summary: Insert an element before or after another element in a list
since: 2.2.0
Inserts element in the list stored at key either before or after the reference value pivot.
在引用值 pivot 之前或之后插入元素。
When key does not exist, it is considered an empty list and no operation is performed.
如果键不存在,则将其视为空列表,不执行任何操作。
An error is returned when key exists but does not hold a list value.
如果键存在但不包含列表值,则返回错误。
1.2.2 返回
- 插入操作后列表的长度,或者找不到值 pivot 时列表的长度为 -1
1.2.3 练习
127.0.0.1:6379[1]> linsert mylist after world "hadoop"
(integer) 5
127.0.0.1:6379[1]> lrange mylist 0 -1
1) "flink"
2) "redis"
3) "world"
4) "hadoop"
5) "hello"
1.3 llen
1.3.1 基本信息
LLEN key
summary: Get the length of a list
since: 1.0.0
Returns the length of the list stored at key. If key does not exist, it is interpreted as an empty list and 0 is returned. An error is returned when the value stored at key is not a list.
返回按键存储的列表的长度。如果键不存在,则将其解释为空列表并返回0。如果键上存储的值不是列表,则返回错误。
1.3.2 返回
- 列表的长度
1.3.3 练习
127.0.0.1:6379[1]> llen mylist
(integer) 5
1.4 lmove
1.4.1 基本信息
LMOVE source destination LEFT|RIGHT LEFT|RIGHT
summary: Pop an element from a list, push it to another list and return it
since: 6.2.0
Atomically returns and removes the first/last element (head/tail depending on the wherefrom argument) of the list stored at source, and pushes the element at the first/last element (head/tail depending on the whereto argument) of the list stored at destination.
原子地返回并删除源中列表的第一个/最后一个元素(head/tail 取决于 where 参数) ,并将元素推送到目的列表的第一个/最后一个元素(head/tail 取决于 where 参数)。
If source does not exist, the value nil is returned and no operation is performed. If source and destination are the same, the operation is equivalent to removing the first/last element from the list and pushing it as first/last element of the list, so it can be considered as a list rotation command (or a no-op if wherefrom is the same as whereto).
如果 source 不存在,则返回值 nil 并且不执行任何操作。如果源和目标是相同的,那么这个操作等效于从列表中删除第一个/最后一个元素,并将其作为列表的第一个/最后一个元素,因此它可以被视为一个列表轮换命令(或者一个 no-op,如果 where from 与 where 相同)。
This command comes in place of the now deprecated RPOPLPUSH. Doing LMOVE RIGHT LEFT is equivalent.
这个命令代替了现在已经废弃的 RPOPLPUSH。
1.4.2 返回
- 正在弹出和推送的元素
1.4.3 练习
127.0.0.1:6379[1]> lmove mylist newlist right left
"hello"
127.0.0.1:6379[1]> lmove mylist newlist right left
"hadoop"
127.0.0.1:6379[1]> lrange mylist 0 -1
1) "flink"
2) "redis"
3) "world"
127.0.0.1:6379[1]> lrange newlist 0 -1
1) "hadoop"
2) "hello"
1.4.4 经典使用-reliable queue
Redis is often used as a messaging server to implement processing of background jobs or other kinds of messaging tasks. A simple form of queue is often obtained pushing values into a list in the producer side, and waiting for this values in the consumer side using RPOP (using polling), or BRPOP if the client is better served by a blocking operation.
Redis 通常用作消息传递服务器,以实现后台作业或其他类型的消息传递任务的处理。一种简单的队列形式通常是在生产者端将值推入列表,然后在使用者端使用 RPOP (使用轮询)等待这些值,或者如果阻塞操作更适合客户端,则使用 BRPOP。
However in this context the obtained queue is not reliable as messages can be lost, for example in the case there is a network problem or if the consumer crashes just after the message is received but it is still to process.
但是在这种情况下,所获得的队列是不可靠的,因为消息可能会丢失,例如,在网络问题的情况下,或者如果使用者在接收到消息之后崩溃,但消息仍需要处理。
LMOVE (or BLMOVE for the blocking variant) offers a way to avoid this problem: the consumer fetches the message and at the same time pushes it into a processing list. It will use the LREM command in order to remove the message from the processing list once the message has been processed.
LMOVE (或 BLMOVE 的阻塞变体)提供了一种方法来避免这个问题: 使用者获取消息,同时将其推入处理列表。一旦消息被处理,它将使用 LREM 命令从处理列表中删除消息。
An additional client may monitor the processing list for items that remain there for too much time, and will push those timed out items into the queue again if needed.
另一个客户端可能会监视处理列表中保留时间过长的项目,并在需要时将这些超时项目再次推入队列。
1.4.5 经典使用-Circular list
Using LMOVE with the same source and destination key, a client can visit all the elements of an N-elements list, one after the other, in O(N) without transferring the full list from the server to the client using a single LRANGE operation.
使用具有相同源和目标键的 LMOVE,客户端可以访问 N 元素列表中的所有元素,一个接一个,而不需要使用单个 LRANGE 操作将完整的列表从服务器传输到客户端。
The above pattern works even if the following two conditions:
即使有以下两个条件,上述模式仍然有效:
- There are multiple clients rotating the list: they'll fetch different elements, until all the elements of the list are visited, and the process restarts.
- 有多个客户机在轮换列表: 它们将获取不同的元素,直到访问列表的所有元素,然后重新启动进程。
- Even if other clients are actively pushing new items at the end of the list.
- 即使其他客户在列表末尾积极推出新项目。
The above makes it very simple to implement a system where a set of items must be processed by N workers continuously as fast as possible. An example is a monitoring system that must check that a set of web sites are reachable, with the smallest delay possible, using a number of parallel workers.
以上使得实现一个系统非常简单,其中一组项目必须由 N 个工作人员以尽可能快的速度连续处理。一个例子是一个监控系统,它必须检查一组网站是否可以访问,使用大量的并行工作者,并尽可能减少延迟。
Note that this implementation of workers is trivially scalable and reliable, because even if a message is lost the item is still in the queue and will be processed at the next iteration.
请注意,这个 worker 的实现是可伸缩和可靠的,因为即使消息丢失,该项仍然在队列中,并将在下一次迭代中处理。
1.5 lpop
1.5.1 基本信息
LPOP key [count]
summary: Remove and get the first elements in a list
since: 1.0.0
Removes and returns the first elements of the list stored at key.
移除并返回以键存储的列表的第一个元素。
By default, the command pops a single element from the beginning of the list. When provided with the optional count argument, the reply will consist of up to count elements, depending on the list's length.
默认情况下,该命令从列表的开头弹出一个元素。当提供了可选的 count 参数时,根据列表的长度,应答最多包含 count 元素。
1.5.2 返回
- 当不使用 count 参数调用时,第一个元素的值,如果键不存在,则为 nil
- 当使用 count 参数调用时,弹出元素列表,或者键不存在时为空
1.5.3 练习
127.0.0.1:6379[1]> lpop mylist
"flink"
127.0.0.1:6379[1]> lpop mylist
"redis"
127.0.0.1:6379[1]> lrange mylist 0 -1
1) "world"
1.6 lpos
1.6.1 基本信息
LPOS key element [RANK rank] [COUNT num-matches] [MAXLEN len]
summary: Return the index of matching elements on a list
since: 6.0.6
The command returns the index of matching elements inside a Redis list. By default, when no options are given, it will scan the list from head to tail, looking for the first match of "element". If the element is found, its index (the zero-based position in the list) is returned. Otherwise, if no match is found, nil is returned.
该命令返回 Redis 列表中匹配元素的索引。默认情况下,当没有给出选项时,它将从头到尾扫描列表,寻找“ element”的第一个匹配项。如果找到元素,则返回其索引(列表中从零开始的位置)。否则,如果未找到匹配项,则返回 nil。
The optional arguments and options can modify the command's behavior. The RANK option specifies the "rank" of the first element to return, in case there are multiple matches. A rank of 1 means to return the first match, 2 to return the second match, and so forth.
可选参数和选项可以修改命令的行为。如果有多个匹配项,RANK 选项指定要返回的第一个元素的“等级”。秩为1表示返回第一个匹配项,2表示返回第二个匹配项,依此类推。
Note that the indexes are still reported in the "natural" way, that is, considering the first element starting from the head of the list at index 0, the next element at index 1, and so forth. This basically means that the returned indexes are stable whatever the rank is positive or negative.
请注意,索引仍然以“自然”的方式报告,也就是说,考虑从索引0处的列表头部开始的第一个元素,索引1处的下一个元素,等等。这基本上意味着无论排名是正还是负,返回的索引都是稳定的。
When COUNT is used, it is possible to specify 0 as the number of matches, as a way to tell the command we want all the matches found returned as an array of indexes. This is better than giving a very large COUNT option because it is more general.
在使用 COUNT 时,可以将0指定为匹配项的数目,这样可以告诉命令,我们希望找到的所有匹配项作为索引数组返回。这比给出一个非常大的 COUNT 选项要好,因为它更通用。
When COUNT is used and no match is found, an empty array is returned. However when COUNT is not used and there are no matches, the command returns nil.
当使用 COUNT 并且未找到匹配项时,将返回一个空数组。但是,当不使用 COUNT 并且没有匹配项时,该命令将返回 nil。
Finally, the MAXLEN option tells the command to compare the provided element only with a given maximum number of list items. So for instance specifying MAXLEN 1000 will make sure that the command performs only 1000 comparisons, effectively running the algorithm on a subset of the list (the first part or the last part depending on the fact we use a positive or negative rank). This is useful to limit the maximum complexity of the command. It is also useful when we expect the match to be found very early, but want to be sure that in case this is not true, the command does not take too much time to run.
最后,MAXLEN 选项告诉命令仅将提供的元素与给定的最大列表项数进行比较。例如,指定 MAXLEN 1000将确保命令只执行1000次比较,有效地在列表的子集上运行算法(第一部分或最后一部分取决于我们使用的是正排名还是负排名)。这对于限制命令的最大复杂性非常有用。当我们期望很早就能找到匹配项时,它也很有用,但是要确保在这种情况下,命令不会花费太多的时间来运行。
When MAXLEN is used, it is possible to specify 0 as the maximum number of comparisons, as a way to tell the command we want unlimited comparisons. This is better than giving a very large MAXLEN option because it is more general.
当使用 MAXLEN 时,可以指定0作为最大比较次数,作为告诉命令我们希望无限比较的一种方式。这比给出一个非常大的 MAXLEN 选项要好,因为它更通用。
1.6.2 返回
- 返回表示匹配元素的整数,如果没有匹配,则返回 nil
- 如果给定了 COUNT 选项,命令将返回一个数组(如果没有匹配,则为空)
1.6.3 练习
127.0.0.1:6379[1]> RPUSH mylist a b c d 1 2 3 4 3 3 3
(integer) 12
127.0.0.1:6379[1]> lpos mylist 3
(integer) 7
127.0.0.1:6379[1]> lpos mylist 3 count 0
1) (integer) 7
2) (integer) 9
3) (integer) 10
4) (integer) 11
127.0.0.1:6379[1]> lpos mylist 3 count 0 rank 2
1) (integer) 9
2) (integer) 10
3) (integer) 11
1.7 lpush
1.7.1 基本信息
LPUSH key element [element ...]
summary: Prepend one or multiple elements to a list
since: 1.0.0
Insert all the specified values at the head of the list stored at key. If key does not exist, it is created as empty list before performing the push operations. When key holds a value that is not a list, an error is returned.
将所有指定的值插入存储在 key 上的列表的顶部。如果键不存在,则在执行推送操作之前将其创建为空列表。当 key 包含非列表值时,将返回一个错误。
It is possible to push multiple elements using a single command call just specifying multiple arguments at the end of the command. Elements are inserted one after the other to the head of the list, from the leftmost element to the rightmost element. So for instance the command LPUSH mylist a b c will result into a list containing c as first element, b as second element and a as third element.
可以使用单个命令调用在命令末尾指定多个参数来推送多个元素。元素一个接一个地插入列表的头部,从最左边的元素插入到最右边的元素。例如,命令 LPUSH mylist a b c 将生成一个列表,其中 c 作为第一个元素,b 作为第二个元素,a 作为第三个元素。
1.7.2 返回
- 推送操作之后列表的长度
1.7.3 练习
127.0.0.1:6379[1]> lrange push 0 -1
1) "6"
2) "5"
3) "4"
4) "3"
5) "2"
6) "111"
1.8 lpushx
1.8.1 基本信息
LPUSHX key element [element ...]
summary: Prepend an element to a list, only if the list exists
since: 2.2.0
Inserts specified values at the head of the list stored at key, only if key already exists and holds a list. In contrary to LPUSH, no operation will be performed when key does not yet exist.
仅当 key 已经存在并保存列表时,才在存储在 key 的列表头部插入指定的值。与 LPUSH 不同的是,当 key 还不存在时,不会执行任何操作。
1.8.2 返回
- 推送操作后列表的长度
1.8.3 练习
127.0.0.1:6379[1]> lpushx list1 sss
(integer) 0
1.9 lrange
1.9.1 基本信息
LRANGE key start stop
summary: Get a range of elements from a list
since: 1.0.0
Returns the specified elements of the list stored at key. The offsets start and stop are zero-based indexes, with 0 being the first element of the list (the head of the list), 1 being the next element and so on.
返回按键存储的列表的指定元素(左闭右闭)。偏移量 start 和 stop 是从零开始的索引,0是列表的第一个元素(列表的头) ,1是下一个元素,依此类推。
These offsets can also be negative numbers indicating offsets starting at the end of the list. For example, -1 is the last element of the list, -2 the penultimate, and so on.
这些偏移量也可以是负数,表示从列表末尾开始的偏移量。例如,-1是列表的最后一个元素,-2是倒数第二个元素,依此类推。
Out of range indexes will not produce an error. If start is larger than the end of the list, an empty list is returned. If stop is larger than the actual end of the list, Redis will treat it like the last element of the list.
超出范围的索引不会产生错误。如果 start 大于列表的末尾,则返回空列表。如果 stop 大于列表的实际末尾,Redis 会将其视为列表的最后一个元素。
1.9.2 返回
- 指定范围内的元素列表
1.9.3 练习
127.0.0.1:6379[1]> lrange push 0 -1
1) "6"
2) "5"
3) "4"
4) "3"
5) "2"
6) "111"
1.10 lrem
1.10.1 基本信息
LREM key count element
summary: Remove elements from a list
since: 1.0.0
Removes the first count occurrences of elements equal to element from the list stored at key. The count argument influences the operation in the following ways:
从按键存储的列表中移除与元素相等的元素的第一个计数出现次数。Count 参数通过以下方式影响操作:
- count > 0: Remove elements equal to element moving from head to tail.
- Count > 0: 删除等于从头到尾移动的元素。
- count < 0: Remove elements equal to element moving from tail to head.
- Count < 0: 删除等于从尾部移动到头部的元素。
- count = 0: Remove all elements equal to element.
- Count = 0: 删除与元素相等的所有元素。
For example, LREM list -2 "hello" will remove the last two occurrences of "hello" in the list stored at list.
例如,LREM list -2“ hello”将删除存储在 list 中的列表中的最后两个“ hello”。
Note that non-existing keys are treated like empty lists, so when key does not exist, the command will always return 0.
注意,不存在的键被视为空列表,因此当键不存在时,命令总是返回0。
1.10.2 返回
- 已删除元素的数量
1.10.3 练习
127.0.0.1:6379[1]> lrem push 2 2
(integer) 1
127.0.0.1:6379[1]> lrange push 0 -1
1) "6"
2) "5"
3) "4"
4) "3"
5) "111"
1.11 lset
1.11.1 基本信息
LSET key index element
summary: Set the value of an element in a list by its index
since: 1.0.0
Sets the list element at index to element. For more information on the index argument, see LINDEX.
将索引处的列表元素设置为元素。
An error is returned for out of range indexes.
返回超出范围索引的错误。
1.11.2 返回
- ok
1.11.3 练习
127.0.0.1:6379[1]> lrange push 0 -1
1) "set"
2) "5"
3) "4"
4) "3"
5) "111"
1.12 ltrim
1.12.1 基本信息
LTRIM key start stop
summary: Trim a list to the specified range
since: 1.0.0
Trim an existing list so that it will contain only the specified range of elements specified. Both start and stop are zero-based indexes, where 0 is the first element of the list (the head), 1 the next element and so on.
修剪现有列表,使其仅包含指定的元素范围。Start 和 stop 都是从零开始的索引,其中0是列表的第一个元素(head) ,1是下一个元素,依此类推。
For example: LTRIM foobar 0 2 will modify the list stored at foobar so that only the first three elements of the list will remain.
例如: LTRIM foobar 02将修改存储在 foobar 中的列表,以便只保留列表的前三个元素。
start and end can also be negative numbers indicating offsets from the end of the list, where -1 is the last element of the list, -2 the penultimate element and so on.
Start 和 end 也可以是负数,表示从列表末尾开始的偏移量,其中 -1是列表的最后一个元素,-2是倒数第二个元素,依此类推。
Out of range indexes will not produce an error: if start is larger than the end of the list, or start > end, the result will be an empty list (which causes key to be removed). If end is larger than the end of the list, Redis will treat it like the last element of the list.
超出范围的索引不会产生错误: 如果 start 大于列表的末尾,或 start > end,结果将是一个空列表(这会导致删除键)。如果 end 大于列表的末尾,Redis 会将其视为列表的最后一个元素。
1.12.2 返回
- ok
1.12.3 练习
127.0.0.1:6379[1]> lpush key12 11 22 33 44 55 66 77 99 000
(integer) 9
127.0.0.1:6379[1]> ltrim key12 0 3
OK
127.0.0.1:6379[1]> lrange key12 0 -1
1) "000"
2) "99"
3) "77"
4) "66"
1.13 rpop
1.13.1 基本信息
RPOP key [count]
summary: Remove and get the last elements in a list
since: 1.0.0
Removes and returns the last elements of the list stored at key.
移除并返回按键存储的列表的最后一个元素。
By default, the command pops a single element from the end of the list. When provided with the optional count argument, the reply will consist of up to count elements, depending on the list's length.
默认情况下,该命令从列表末尾弹出一个元素。当提供了可选的 count 参数时,根据列表的长度,应答最多包含 count 元素。
1.13.2 返回
- 当不使用 count 参数调用时,最后一个元素的值,如果键不存在,则为 nil
- 当使用 count 参数调用时,弹出元素列表,或者键不存在时为空
1.13.3 练习
127.0.0.1:6379[1]> lrange key12 0 -1
1) "000"
2) "99"
3) "77"
4) "66"
127.0.0.1:6379[1]> rpop key12 2
1) "66"
2) "77"
127.0.0.1:6379[1]> lrange key12 0 -1
1) "000"
2) "99"
1.14 rpush
1.14.1 基本信息
RPUSH key element [element ...]
summary: Append one or multiple elements to a list
since: 1.0.0
Insert all the specified values at the tail of the list stored at key. If key does not exist, it is created as empty list before performing the push operation. When key holds a value that is not a list, an error is returned.
将所有指定的值插入存储在 key 上的列表的尾部。如果键不存在,则在执行推送操作之前将其创建为空列表。当 key 包含非列表值时,将返回一个错误。
It is possible to push multiple elements using a single command call just specifying multiple arguments at the end of the command. Elements are inserted one after the other to the tail of the list, from the leftmost element to the rightmost element. So for instance the command RPUSH mylist a b c will result into a list containing a as first element, b as second element and c as third element.
可以使用单个命令调用在命令末尾指定多个参数来推送多个元素。元素一个接一个地插入到列表的尾部,从最左边的元素插入到最右边的元素。例如,命令 RPUSH mylist a b c 将生成一个包含第一个元素、第二个元素 b 和第三个元素 c 的列表。
1.14.2 返回
- 推送操作后列表的长度
1.14.3 练习
127.0.0.1:6379[1]> rpush key12 88 99 88 88
(integer) 6
127.0.0.1:6379[1]> lrange key12 0 -1
1) "000"
2) "99"
3) "88"
4) "99"
5) "88"
6) "88"
1.15 rpushx
1.15.1 基本信息
RPUSHX key element [element ...]
summary: Append an element to a list, only if the list exists
since: 2.2.0
Inserts specified values at the tail of the list stored at key, only if key already exists and holds a list. In contrary to RPUSH, no operation will be performed when key does not yet exist.
仅当 key 已经存在并保存列表时,才在存储在 key 上的列表的尾部插入指定的值。与 RPUSH 相反,当键还不存在时,将不执行任何操作。
1.15.2 返回
- 推送操作后列表的长度
1.15.3 练习
127.0.0.1:6379[1]> rpushx key333 22
(integer) 0
1.16 blmove
1.16.1 基本信息
BLMOVE source destination LEFT|RIGHT LEFT|RIGHT timeout
summary: Pop an element from a list, push it to another list and return it; or block until one is available
since: 6.2.0
BLMOVE is the blocking variant of LMOVE. When source contains elements, this command behaves exactly like LMOVE. When used inside a MULTI/EXEC block, this command behaves exactly like LMOVE. When source is empty, Redis will block the connection until another client pushes to it or until timeout (a double value specifying the maximum number of seconds to block) is reached. A timeout of zero can be used to block indefinitely.
BLMOVE 是 LMOVE 的阻塞变体。当源包含元素时,此命令的行为与 LMOVE 完全相同。当在 MULTI/EXEC 块中使用时,此命令的行为与 LMOVE 完全相同。当源为空时,Redis 将阻塞连接,直到另一个客户端推送到该连接,或者直到超时(指定最大阻塞秒数的双重值)。可以使用零超时来无限期地阻塞。
1.16.2 返回
- 从源弹出并推送到目标的元素。如果达到超时,则返回 Null
1.17 blpop
1.17.1 基本信息
BLPOP key [key ...] timeout
summary: Remove and get the first element in a list, or block until one is available
since: 2.0.0
BLPOP is a blocking list pop primitive. It is the blocking version of LPOP because it blocks the connection when there are no elements to pop from any of the given lists. An element is popped from the head of the first list that is non-empty, with the given keys being checked in the order that they are given.
BLPOP 是一个阻塞式列表弹出原语。它是 LPOP 的阻塞版本,因为当没有要从任何给定列表中弹出的元素时,它会阻塞连接。从第一个非空列表的头部弹出一个元素,按照给定的顺序检查给定的键。
1.17.2 返回
- 当没有元素可以弹出并且超时过期时,返回 nil
- 一个元素的有两部分,第一个是弹出元素的键的名称,第二个是弹出元素的值
1.17.3 练习
127.0.0.1:6379[1]> lpush key88 222
(integer) 1
127.0.0.1:6379[1]> blpop key88 0
1) "key88"
2) "222"
(11.00s)
1.18 brpop
1.18.1 基本信息
BRPOP key [key ...] timeout
summary: Remove and get the last element in a list, or block until one is available
since: 2.0.0
BRPOP is a blocking list pop primitive. It is the blocking version of RPOP because it blocks the connection when there are no elements to pop from any of the given lists. An element is popped from the tail of the first list that is non-empty, with the given keys being checked in the order that they are given.
BRPOP 是一个阻塞式列表弹出原语。它是 RPOP 的阻塞版本,因为当没有元素从任何给定列表中弹出时,它会阻塞连接。从第一个非空列表的尾部弹出一个元素,按照给定的顺序检查给定的键。
1.18.2 返回
- 当没有元素可以弹出并且超时过期时,返回 nil
- 一个元素的有两部分,第一个是弹出元素的键的名称,第二个是弹出元素的值