-- Reprinted:
I. Overview:
In redis, the list type is a string linked list sorted by insertion order. Like the common linked list in the data structure, we can add new elements in the header (left) and tail (right. If the key does not exist during insertion, redis creates a new linked list for the key. In contrast, if all elements in the linked list are removed, the key will also be deleted from the database. The maximum number of elements in a list is 4294967295.
From the perspective of element insertion and deletion efficiency, If we insert or delete elements at both ends of the linked list, this will be very efficient, this operation can be completed within the constant time even if millions of records have been stored in the linked list. However, it should be noted that if the element insertion or deletion operation acts on the center of the linked list, it will be very inefficient. It is believed that this is not difficult for developers with a sound data structure.
Ii. Related command list:
Command prototype |
Time Complexity |
Command description |
Return Value |
LpushKey value [value...] |
O (1) |
Insert all values in the header of the List value associated with the specified key. If the key does not exist, this command will create an empty linked list associated with the key before insertion, and then insert the data from the head of the linked list. If the value of the key is not of the linked list type, the command returns an error message. |
The number of elements in the inserted linked list. |
LpushxKey Value |
O (1) |
Only when the key specified in the parameter exists will the command Insert the value given in the parameter in the header of the associated list value. Otherwise, no operation will occur. |
The number of elements in the inserted linked list. |
LrangeKey start stop |
O (S + n) |
In time complexity, S is the offset expressed by the start parameter, and N represents the number of elements. The start and end parameters of this command are both 0-based. 0 indicates the first element of the chain table header (leftmost. The start value can also be a negative value.-1 indicates the last element in the linked list, that is, the tail element.-2 indicates the second to the last and so on. When this command is used to obtain elements, the elements at the start and end locations will also be taken out. If the start value is greater than the number of elements in the linked list, the empty linked list will be returned. If the end value is greater than the number of elements, this command obtains all the elements from the start (including START) and linked list. |
Returns the list of elements in the specified range. |
LpopKey |
O (1) |
The first element in the linked list associated with the specified key, that is, the Header element, is displayed ,. If the key is not saved, Nil is returned. |
The element of the linked list header. |
LlenKey |
O (1) |
Returns the number of elements in the linked list associated with the specified key. If the key does not exist, 0 is returned. If the type of the value associated with the key is not a linked list, the related error message is returned. |
The number of elements in the linked list. |
LremKey Count value |
O (N) |
In time complexity, N indicates the number of elements in the linked list. In the linked list associated with the specified key, delete the elements whose previous Count value is equal to value. If count is greater than 0, it is traversed from the beginning to the end and deleted. If count is smaller than 0, it is traversed and deleted from the end. If count is equal to 0, all elements equal to value in the linked list are deleted. If the specified key does not exist, 0 is returned directly. |
Returns the number of deleted elements. |
LsetKey Index Value |
O (N) |
In time complexity, N indicates the number of elements in the linked list. However, when you set the header or tail element, the time complexity is O (1 ). Set the value of the specified position in the linked list to a new value. 0 indicates the first element, that is, the Header element, and-1 indicates the tail element. If the index value exceeds the number range of elements in the linked list, this command returns related error information. |
|
LindexKey Index |
O (N) |
In time complexity, N indicates the number of elements to be traversed when this element is found. For header or tail elements, the time complexity is O (1 ). This command returns the element of the specified position (INDEX) in the linked list. The index is 0-based, indicating the Header element. If the index is-1, it indicates the tail element. If the linked list is not associated with the key, the command returns the relevant error message. |
Returns the requested element. If the index is out of the range, Nil is returned. |
LtrimKey start stop |
O (N) |
N indicates the number of deleted elements. This command retains only the elements within the specified range to ensure that the number of elements in the link is relatively constant. The Start and Stop parameters are both 0-based. 0 indicates the Header element. Like other commands, start and stop can also be negative values.-1 indicates the tail element. If start is greater than the end of the linked list, or start is greater than stop, an error is returned, but an empty linked list is returned, and the key is also deleted. If stop is greater than the number of elements, all elements remaining from start are retained. |
|
LinsertKey before | after each value |
O (N) |
In time complexity, N indicates the number of elements that need to be traversed before the element 'struct' is found. This means that if the operator is at the head or tail of the linked list, the time complexity of this command is O (1 ). The function of this command is to insert the element value in the parameter before or after the values element. If the key does not exist, this command will not perform any operations. If the value type associated with the key is not a linked list, related error messages are returned. |
Number of elements in the linked list after successful insertion. If no marker is found,-1 is returned. If the key does not exist, 0 is returned. |
RpushKey value [value...] |
O (1) |
Insert all values given in the parameter at the end of the list value associated with the specified key. If the key does not exist, this command will create an empty linked list associated with the key before insertion, and then insert the data from the end of the linked list. If the value of the key is not of the linked list type, the command returns an error message. |
The number of elements in the inserted linked list. |
RpushxKey Value |
O (1) |
Only when the key specified in the parameter exists will the command Insert the value given in the parameter at the end of the associated list value. Otherwise, no operation will occur. |
The number of elements in the inserted linked list. |
RpopKey |
O (1) |
Returns the last element in the linked list associated with the specified key, that is, the tail element ,. If the key is not saved, Nil is returned. |
Elements at the end of the linked list. |
RpoplpushSource destination |
O (1) |
An atomic element pops up from the end of the linked list associated with the source key, and then inserts the pop-up element into the header of the linked list associated with the destination key. If the source key does not exist, this command returns nil without any other operations. If source and destination are the same key, it is equivalent to atomically moving the tail elements in the associated linked list to the head of the linked list. |
Returns the pop-up and inserted elements. |
Iii. Command example:
1. lpush/lpushx/lrange:/> redis-cli # Start the redis client tool at a shell prompt. Redis 127.0.0.1: 6379> Del mykey (integer) 1 # The mykey key does not exist. This command creates the key and its associated list, then, insert the values in the parameter from left to right. Redis 127.0.0.1: 6379> lpush mykey a B c d (integer) 4 # retrieve the three elements from position 0 to position 2. Redis 127.0.0.1: 6379> lrange mykey 0 2 1) "D" 2) "C" 3) "B" # retrieve all elements in the linked list. 0 indicates the first element, -1 indicates the last element. Redis 127.0.0.1: 6379> lrange mykey 0-1 1) "D" 2) "C" 3) "B" 4) "A" # mykey2 key does not exist at this time, therefore, this command will not perform any operation, and its return value is 0. Redis 127.0.0.1: 6379> lpushx mykey2 E (integer) 0 # You Can See That mykey2 is not associated with any list value. Redis 127.0.0.1: 6379> lrange mykey2 0-1 (empty list or set) # The mykey key already exists. Therefore, this command is successfully inserted and the number of current elements in the linked list is returned. Redis 127.0.0.1: 6379> lpushx mykey E (integer) 5 # obtain the Header element of the List Value of the key. Redis 127.0.0.1: 6379> lrange mykey 0 0 1) "E" 2. lpop/llen: 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" # After the lpop command is executed twice, the two elements in the head of the linked list have been popped up. The number of elements in the linked list is 2 redis 127.0.0.1: 6379> llen mykey (integer) 2 3. lrem/lset/lindex/ltrim: # Prepare test data for the following example. Redis 127.0.0.1: 6379> lpush mykey a B c d a C (integer) 6 # Delete two elements whose values are equal to a from the left to the right variable linked list, the return value is the number of actually deleted items. Redis 127.0.0.1: 6379> lrem mykey 2 A (integer) 2 # Shows all elements in the deleted linked list. Redis 127.0.0.1: 6379> lrange mykey 0-1 1) "C" 2) "D" 3) "C" 4) "B" # obtain the element value whose index value is 1 (the second element in the header. Redis 127.0.0.1: 6379> lindex mykey 1 "D" # Set the element value of index value 1 (the second element in the header) to the new value e. Redis 127.0.0.1: 6379> lset mykey 1 e OK # Check whether the setting is successful. Redis 127.0.0.1: 6379> lindex mykey 1 "E" # the index value 6 exceeds the number of elements in the linked list. This command returns nil. Redis 127.0.0.1: 6379> lindex mykey 6 (NiL) # The SET index value 6 exceeds the number of elements in the linked list. If the setting fails, this command returns an error message. Redis 127.0.0.1: 6379> lset mykey 6 HH (error) Err Index out of range # Only three elements between 0 and 2 are retained, note that 0th and 2nd elements are retained. Redis 127.0.0.1: 6379> ltrim mykey 0 2 OK # view the trim result. Redis 127.0.0.1: 6379> lrange mykey 0-1 1) "C" 2) "E" 3) "C" 4. linsert: # Delete this key for subsequent testing. Redis 127.0.0.1: 6379> Del mykey (integer) 1 # Prepare test data for the following example. Redis 127.0.0.1: 6379> lpush mykey a B c d e (integer) 5 # Insert a new element A1 before. Redis 127.0.0.1: 6379> linsert mykey before a A1 (integer) 6 # Check whether the insert is successful. The result shows that the insert is successful. Note that the index value of lindex is 0-based. Redis 127.0.0.1: 6379> lindex mykey 0 "E" # Insert the new element E2 after E. The result shows that the insert is successful. Redis 127.0.0.1: 6379> linsert mykey after e E2 (integer) 7 # Check whether the insert is successful again. Redis 127.0.0.1: 6379> lindex mykey 1 "E2" # Insert a new element before or after a nonexistent element. This command fails and returns-1. Redis 127.0.0.1: 6379> linsert mykey after k a (integer)-1 # Insert a new element for a nonexistent key. This command fails and returns 0. Redis 127.0.0.1: 6379> linsert mykey1 after a A2 (integer) 0 5. rpush/rpushx/rpop/rpoplpush: # Delete this key for later testing. Redis 127.0.0.1: 6379> Del mykey (integer) 1 # Insert the values parameter from the end of the linked list. The insertion sequence is from left to right. Redis 127.0.0.1: 6379> rpush mykey a B c d (integer) 4 # You can use lrange to learn the insert sequence of rpush when inserting multiple values. Redis 127.0.0.1: 6379> lrange mykey 0-1 1) "A" 2) "B" 3) "C" 4) "D" # This key already exists and contains four elements. The rpushx command runs successfully and inserts Element E to the end of the linked list. Redis 127.0.0.1: 6379> rpushx mykey E (integer) 5 # The lindex command shows that the previous rpushx command was successfully executed, because the element with the index value of 4 is already a new element. Redis 127.0.0.1: 6379> lindex mykey 4 "E" # because the mykey2 key does not exist, the command does not insert data and the return value is 0. Redis 127.0.0.1: 6379> rpushx mykey2 E (integer) 0 # Before executing the rpoplpush command, first check the elements of the linked list in mykey and pay attention to their positional relationships. Redis 127.0.0.1: 6379> lrange mykey 0-1 1) "A" 2) "B" 3) "C" 4) "D" 5) "E" # pop up the tail Element E of mykey and insert it to the header of mykey2 (perform these two steps atomically ). Redis 127.0.0.1: 6379> rpoplpush mykey mykey2 "E" # Run the lrange command to view the result of mykey after the tail element is displayed. Redis 127.0.0.1: 6379> lrange mykey 0-1 1) "A" 2) "B" 3) "C" 4) "D" # Run the lrange command to view the result of mykey2 inserting an element. Redis 127.0.0.1: 6379> lrange mykey2 0-1 1) "E" # Set source and destination as the same key and move the tail element of mykey to its header. Redis 127.0.0.1: 6379> rpoplpush mykey "D" # view the mobile result. Redis 127.0.0.1: 6379> lrange mykey 0-1 1) "D" 2) "A" 3) "B" 4) "C"
4. Tips on linked list structure:
For the value of the linked list structure, redis provides some practical skills in its official documentation, such as the rpoplpush command, which is explained below.
Redis linked lists are often used in Message Queue services to exchange messages between multiple programs. Suppose an application is executing the lpush operation to add new elements to the linked list. We usually call such a program "producer )", another application is executing the rpop operation to retrieve elements from the linked list. We call this program "consumer )". If, at this time, the consumer program crashes immediately after retrieving the message element, because the message has been removed and is not processed properly, we can think that the message has been lost, as a result, business data may be lost or the service status may be inconsistent. However, by using the rpoplpush command, the consumer program extracts the message from the master message queue and then inserts it into the backup queue, after the consumer program completes the normal processing logic, the message is deleted from the backup queue. At the same time, we can also provide a daemon process. When a message in the backup queue expires, it can be re-stored in the primary message queue for other consumer programs to continue processing.
Redis data type -- list