First, overview:
In Redis, the list type is a list of strings sorted in the order of insertion. As with the regular linked list in the data structure, we can add new elements to the head (left) and tail (right). When inserted, if the key does not exist, Redis creates a new linked list for the key. Conversely, if all elements in a linked list are removed, the key will also be deleted from the database. The maximum number of elements that can be contained in a list is 4294967295.
From the efficiency perspective of element insertion and deletion, if we insert or delete elements at both ends of the list, this can be a very efficient operation, even if the millions record is already stored in the list, and the operation is completed in constant time. It is, however, important to note that if the element insertion or deletion is acting on the middle of the list, it will be very inefficient. It's not hard to understand that for developers with good data structure fundamentals.
Ii. List of related commands:
| Command prototypes |
Complexity of Time |
Command description |
return value |
| lpushkey value [value ...] |
O (1) |
All values given in the header insert parameter of the list value associated with the specified key. If the key does not exist, the command creates an empty list associated with the key before inserting it, and then inserts the data from the head of the list. If the value of the key is not a linked list type, the command returns the associated error message. |
The number of elements in the linked list after insertion. |
| lpushx Key value |
O (1) |
Only if the key specified in the argument exists, the command will be given value in the header of its associated list value, otherwise no action will occur. |
The number of elements in the linked list after insertion. |
| Lrange key Start stop |
O (S+n) |
The offset of s in the time complexity represented by the start parameter, and n represents the number of elements. The parameter start and end of the command are 0-based. 0 represents the first element of a linked list header (leftmost). Where the value of start can also be negative,-1 will represent the last element in the list, the tail element,-2 for the penultimate and so on. When the command gets an element, the elements at the start and end positions are also taken out. If the value of start is greater than the number of elements in the list, the empty list is returned. If the value of end is greater than the number of elements, the command gets all the remaining elements in the list starting with start, including start. |
Returns a list of elements within the specified range. |
| LpopKey |
O (1) |
Returns and pops the first element in the linked list associated with the specified key, that is, the header element. If the key does not exist, return to nil. |
The element of the head of the linked list. |
| LlenKey |
O (1) |
Returns the number of elements in the linked list associated with the specified key, or 0 if the key does not exist. If the type of value associated with the key is not a linked list, the associated error message is returned. |
The number of elements in the linked list. |
| LremKey Count value |
O (N) |
In time complexity, n represents the number of elements in a linked list. Deletes the element with the previous count value equal to value in the linked list associated with the specified key. If count is greater than 0, traverse and delete from head to tail, and if count is less than 0, traverse and delete from the tail head. If count is equal to 0, all elements equal to value in the linked list are deleted. If the specified key does not exist, it returns 0 directly. |
Returns the number of elements that were deleted. |
| LSETKey index value |
O (N) |
In time complexity, n represents the number of elements in a linked list. But when the head or tail element is set, its time complexity is O (1). The value of the specified position in the list is the new value, where 0 represents the first element, the head element, and-1 represents the tail element. If the index value index exceeds the number of elements in the list, the command returns the associated error message. |
|
| lindex Key index |
O (N) |
The time complexity of n indicates the number of elements that need to be traversed when the element is found. For the head or tail element, the time complexity is O (1). The command returns the element of the specified position in the list (index), which is 0-based, which represents the head element, and if index is-1, the tail element. If the key is not associated with a linked list, the command returns the associated error message. |
Returns the requested element and returns nil if Index is out of range. |
| LTRIMkey Start stop |
O (N) |
n represents the number of elements that are deleted. This command retains only the elements within the specified range, ensuring that the number of elements in the link is relatively constant. Both start and stop parameters are 0-based,0 representing the head element. As with other commands, start and stop can also be negative,-1 for the tail element. If start is greater than the end of the list, or start is greater than stop, the command is a good error, but returns an empty list, while the key is also deleted. If the stop is greater than the number of elements, all elements remaining from start are retained. |
|
| Linsert Key before| After pivot value |
O (N) |
The time complexity n indicates the number of elements that need to be traversed before the element pivot is found. This means that the time complexity of the command is O (1) If the pivot is in the head or tail of the linked list. The function of this command is to insert the element value in the argument before or after the pivot element. If key does not exist, the command will not perform any action. If the value type associated with the key is not a linked list, the associated error message is returned. |
After successfully inserting the number of elements in the linked list, if no pivot is found, return-1, and return 0 if key does not exist. |
| rpush key value [value ...] |
O (1) |
All values given in the trailing insert parameter of the list value associated with the specified key. If the key does not exist, the command creates an empty list associated with the key before inserting it, and then inserts the data from the end of the list. If the value of the key is not a linked list type, the command returns the associated error message. |
The number of elements in the linked list after insertion. |
| rpushx Key value |
O (1) |
Only if the key specified in the parameter exists, the command will be given value in the end of its associated list value, otherwise no action will occur. |
The number of elements in the linked list after insertion. |
| RpopKey |
O (1) |
Returns and pops the last element in the linked list associated with the specified key, that is, the trailing element. If the key does not exist, return to nil. |
The element at the tail of the list. |
| rpoplpushSource destination |
O (1) |
atomically pops an element from the tail of the linked list associated with the source key, and then inserts the pop-up element into the head of the linked list associated with the destination key. If the source key does not exist, the command returns nil and no other action is done. If source and destination are the same key, the equivalent of atomicity moves the tail element in its associated list to the head of the list. |
Returns the elements that are ejected and inserted. |
Third, the command example:
1. Lpush/lpushx/lrange:
Copy Code code as follows:
/> Redis-cli #在Shell提示符下启动redis客户端工具.
Redis 127.0.0.1:6379> del MyKey
(integer) 1
#mykey键并不存在, the command creates the key and the list associated with it, and then inserts values from left to right in the parameter.
Redis 127.0.0.1:6379> Lpush MyKey a b c D
(integer) 4
#取从位置0开始到位置2结束的3个元素.
Redis 127.0.0.1:6379> lrange MyKey 0 2
1) "D"
2) "C"
3) "B"
#取链表中的全部元素, where 0 represents the first element,-1 represents the last element.
Redis 127.0.0.1:6379> lrange MyKey 0-1
1) "D"
2) "C"
3) "B"
4) "A"
#mykey2键此时并不存在, the command will not take any action and the return value is 0.
Redis 127.0.0.1:6379> lpushx Mykey2 E
(integer) 0
#可以看到mykey2没有关联任何List Value.
Redis 127.0.0.1:6379> lrange Mykey2 0-1
(empty list or set)
#mykey键此时已经存在, the command is inserted successfully and returns the number of the current element in the list.
Redis 127.0.0.1:6379> lpushx MyKey E
(integer) 5
#获取该键的List the head element of value.
Redis 127.0.0.1:6379> lrange MyKey 0 0
1) "E"
2. Lpop/llen:
Copy Code code as follows:
Redis 127.0.0.1:6379> Lpush MyKey a b c D
(integer) 4
Redis 127.0.0.1:6379> Lpop MyKey
"D"
Redis 127.0.0.1:6379> Lpop MyKey
C
#在执行lpop命令两次后, two elements in the head of the list have been ejected, and the number of elements in the list is 2.
Redis 127.0.0.1:6379> Llen MyKey
(integer) 2
3. Lrem/lset/lindex/ltrim:
Copy Code code as follows:
#为后面的示例准备测试数据.
Redis 127.0.0.1:6379> Lpush MyKey a b c d a C
(integer) 6
#从头部 (left) to the tail (right) variable list, delete 2 values equal to a, the return value is the actual number of deletes.
Redis 127.0.0.1:6379> Lrem MyKey 2 A
(integer) 2
#看出删除后链表中的全部元素.
Redis 127.0.0.1:6379> lrange MyKey 0-1
1) "C"
2) "D"
3) "C"
4) "B"
The element value of the #获取索引值为1 (the second element of the head).
Redis 127.0.0.1:6379> lindex MyKey 1
"D"
The element value of the #将索引值为1 (the second element of the head) is set to the new value E.
Redis 127.0.0.1:6379> LSet MyKey 1 E
Ok
#查看是否设置成功.
Redis 127.0.0.1:6379> lindex MyKey 1
E
#索引值6超过了链表中元素的数量, this command returns nil.
Redis 127.0.0.1:6379> lindex MyKey 6
(nil)
#设置的索引值6超过了链表中元素的数量, the setting fails, and the command returns an error message.
Redis 127.0.0.1:6379> LSet MyKey 6 hh
(Error) ERR index out of range
#仅保留索引值0到2之间的3个元素, note that both the No. 0 and 2nd elements are preserved.
Redis 127.0.0.1:6379> LTrim MyKey 0 2
Ok
#查看trim后的结果.
Redis 127.0.0.1:6379> lrange MyKey 0-1
1) "C"
2) "E"
3) "C"
4. Linsert:
Copy Code code as follows:
#删除该键便于后面的测试.
Redis 127.0.0.1:6379> del MyKey
(integer) 1
#为后面的示例准备测试数据.
Redis 127.0.0.1:6379> Lpush MyKey a b c d E
(integer) 5
#在a的前面插入新元素a1.
Redis 127.0.0.1:6379> Linsert MyKey before a A1
(integer) 6
#查看是否插入成功, from the result look has been inserted. Note that the index value of Lindex is 0-based.
Redis 127.0.0.1:6379> lindex MyKey 0
E
#在e的后面插入新元素e2, from the return result look has been inserted successfully.
Redis 127.0.0.1:6379> Linsert MyKey after e E2
(integer) 7
#再次查看是否插入成功.
Redis 127.0.0.1:6379> lindex MyKey 1
"E2"
#在不存在的元素之前或之后插入新元素, the command operation failed and returned-1.
Redis 127.0.0.1:6379> Linsert MyKey after k a
(integer)-1
#为不存在的Key插入新元素, the command operation failed, returning 0.
Redis 127.0.0.1:6379> Linsert Mykey1 after a A2
(integer) 0
5. Rpush/rpushx/rpop/rpoplpush:
Copy Code code as follows:
#删除该键 to facilitate the subsequent test.
Redis 127.0.0.1:6379> del MyKey
(integer) 1
#从链表的尾部插入参数中给出的values, the insertion order is inserted from left to right.
Redis 127.0.0.1:6379> Rpush MyKey a b c D
(integer) 4
#通过lrange的可以获悉rpush在插入多值时的插入顺序.
Redis 127.0.0.1:6379> lrange MyKey 0-1
1) "A"
2) "B"
3) "C"
4) "D"
#该键已经存在并且包含4个元素, the RPUSHX command succeeds and inserts element e into the tail of the list.
Redis 127.0.0.1:6379> rpushx MyKey E
(integer) 5
#通过lindex命令可以看出之前的rpushx命令确实执行成功, because an element with an index value of 4 is already a new element.
Redis 127.0.0.1:6379> lindex MyKey 4
E
#由于mykey2键并不存在, the command does not insert data, and its return value is 0.
Redis 127.0.0.1:6379> rpushx Mykey2 E
(integer) 0
#在执行rpoplpush命令前, take a look at the elements of the list in MyKey and pay attention to their position.
Redis 127.0.0.1:6379> lrange MyKey 0-1
1) "A"
2) "B"
3) "C"
4) "D"
5) "E"
#将mykey的尾部元素e弹出, and then insert into the head of the Mykey2 (the atomic completion of these two-step operation).
Redis 127.0.0.1:6379> Rpoplpush MyKey Mykey2
E
#通过lrange命令查看mykey在弹出尾部元素后的结果.
Redis 127.0.0.1:6379> lrange MyKey 0-1
1) "A"
2) "B"
3) "C"
4) "D"
#通过lrange命令查看mykey2在插入元素后的结果.
Redis 127.0.0.1:6379> lrange Mykey2 0-1
1) "E"
#将source和destination设为同一键, moves the tail element in the MyKey to its head.
Redis 127.0.0.1:6379> Rpoplpush MyKey MyKey
"D"
#查看移动结果.
Redis 127.0.0.1:6379> lrange MyKey 0-1
1) "D"
2) "A"
3) "B"
4) "C"
four, the link table structure small skill:
The Value,redis of the list structure gives some practical tips in its official documentation, such as the Rpoplpush command, which is explained in detail below. The
Redis list is often used for Message Queuing services to complete message exchange between multiple programs. Suppose an application is performing a lpush operation to add new elements to the list, we usually call such a program "producer (Producer)" while another application is performing RPOP operations taking elements out of the list, which we call "consumer (Consumer) "。 If at this point, the consumer program crashes immediately after taking out the message element, because the message has been removed and not handled properly, then we can assume that the message has been lost and may result in loss of business data or inconsistency in business status. However, by using the Rpoplpush command, the consumer program inserts a message from the main message queue before inserting it into the backup queue until the consumer program completes its normal processing logic before removing the message from the backup queue. We can also provide a daemon that, when the message in the backup queue is found to expire, can be reset back into the main message queue for other consumer programs to continue processing.