1. Overview:
In Redis, the List type is a linked list of strings sorted in insertion order. Like an ordinary linked list in a data structure, we can add new elements to its head (left) and tail (right). During insertion, if the key does not exist, Redis will create a new linked list for the key. (Recommended: redis video tutorial)
On the contrary, if all elements in the linked list are removed, then 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 linked list, this will be a very efficient operation, even if millions of records have been stored in the linked list. Operations can also be completed in constant time.
However, it should be noted that if the element insertion or deletion operation is performed in the middle of the linked list, it will be very inefficient. I believe this is not difficult to understand for developers with a good data structure foundation.
2. Related command list:
Command prototype | Time complexity | Command description | Return value |
LPUSH key value [value ...] | O(1) | Insert all the Values given in the parameters into 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 inserting, and then insert the data from the head of the linked list. If the value of the key is not of linked list type, this command will return relevant error information. | The number of elements in the linked list after insertion. |
LPUSHX key value | O(1) | Only when the Key specified in the parameter exists, the command will be associated with it The head of the List Value is inserted into the Value given in the parameter, otherwise no operation will occur. | The number of elements in the linked list after insertion. |
LRANGE key start stop | O(S N) | The S in time complexity is the offset represented by the start parameter, and N represents the element quantity. The parameters start and end of this command are both 0-based. That is, 0 represents the first element at the head (leftmost) of the linked list. The value of start can also be a negative value, -1 will represent the last element in the linked list, that is, the tail element, -2 will represent the second to last and so on. When this command obtains elements, the elements at the start and end positions will also be taken out. If the value of start is greater than the number of elements in the linked list, an empty linked list will be returned. If the value of end is greater than the number of elements, this command obtains all remaining elements in the linked list starting from start (including start). | Returns a list of elements within the specified range. |
LPOP key | O(1) | Return and pop up the first element in the linked list associated with the specified Key, that is, the head element. . If the Key does not exist, return nil. | The element at the head of the linked list. |
LLEN key | O(1) | Returns the number of elements in the linked list associated with the specified Key. If the Key does not exist, returns 0 . If the type of Value associated with the Key is not a linked list, relevant error information is returned. | The number of elements in the linked list. |
LREM key count value | O(N) | N in time complexity represents the number of elements in the linked list. In the linked list associated with the specified Key, delete the first count elements whose value is equal to value. If count is greater than 0, traverse from beginning to end and delete. If count is less than 0, traverse from end to beginning and delete. If count is equal to 0, delete all elements in the linked list that are equal to value. If the specified Key does not exist, 0 is returned directly. | Returns the number of deleted elements. |
LSET key index value | O(N) | N in time complexity represents the number of elements in the linked list. But when setting the head or tail elements, the time complexity is O(1). Set the value of the specified position in the linked list to the new value, where 0 represents the first element, that is, the head element, and -1 represents the tail element. If the index value Index exceeds the number of elements in the linked list, this command will return relevant error information. | |
LINDEX key index | O(N) | N in time complexity means that it needs to be traversed when finding the element the number of elements. For head or tail elements, its time complexity is O(1). This command will return the element at the specified position (index) in the linked list. The index is 0-based, which means the head element. If the index is -1, it means the tail element. If the Key is not associated with a linked list, this command will return relevant error information. | Return the requested element, or nil if the index is out of range. |
LTRIM key start stop | O(N) | N represents the number of deleted elements. This command will only retain elements within the specified range, thus keeping the number of elements in the link relatively constant. The start and stop parameters are both 0-based, with 0 indicating the header element. Like other commands, start and stop can also be negative values, and -1 represents the tail element. If start is greater than the end of the linked list, or start is greater than stop, this command will not report an error, but will return an empty linked list, and the Key will also be deleted. If stop is greater than the number of elements, all elements remaining from start are retained. | |
LINSERT key BEFORE|AFTER pivot value | O(N) | In time complexity, N means that when finding the The number of elements that need to be traversed before the element pivots. This means that if the pivot 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 pivot element. If the Key does not exist, this command will do nothing. If the Value type associated with Key is not a linked list, related error information will be returned. | The number of elements in the linked list after successful insertion. If pivot is not found, -1 is returned. If key does not exist, 0 is returned. |
RPUSH key value [value ...] | O(1) | Insert the parameter at the end of the List Value associated with the specified Key All Values given. If the Key does not exist, this command will create an empty linked list associated with the Key before inserting, and then insert the data from the end of the linked list. If the value of the key is not of linked list type, this command will return relevant error information. | The number of elements in the linked list after insertion. |
RPUSHX key value | O(1) | Only when the Key specified in the parameter exists, the command will be associated with it The value given in the parameter is inserted at the end of the List Value, otherwise no operation will occur. | The number of elements in the linked list after insertion. |
O(1) | Return and pop up the last element in the linked list associated with the specified Key, that is, the tail element. If the Key does not exist, return nil. | The element at the end of the linked list. | |
O(1) | Atomicly pop an element from the end of the linked list associated with the source key, and then pop it up again The element is inserted into the head of the linked list associated with the destination key. If the source key does not exist, the command will return nil and no other operations will be performed. If source and destination are the same key, it is equivalent to atomically moving the tail element in its associated linked list to the head of the linked list. | Returns popped and inserted elements. |
The above is the detailed content of Detailed explanation of list types and related commands in redis. For more information, please follow other related articles on the PHP Chinese website!