Redis basic data types (list list)
- list list
-
- Storage type
- Common operation commands
-
- lindex: Get the value of the specified index of the list
- linsert: Insert before or after the specified element in the list
- llen: Get the length of the list
- lpop: remove and return the first element of the list corresponding to the key
- lpush: interpolation at the head of the list
- lpushx
- lrange: Get the specified range value of the specified key in the list
- In different programming languages, on the consistency of the range function
- Subscript out of range
-
- lrem: remove the specified number of values from the list
- lset: Set the value of the specified index
- ltrim: modify the list to only include the specified range
- rpop: Remove and return the last element of the list stored in key.
- rpoplpush: Put the tail element of one list at the head of another list
- rpush: interpolation at the end of the list
- rpushx
list list
Storage type
Store ordered strings (from left to right), elements can be repeated. Maximum storage amount 2^32-1 (about 4 billion)
Common operation commands
lindex: Get the value of the specified index of the list
-
command
LINDEX key index
-
Description
The index index of the element in the returned list is stored in the key. Subscripts are indexed from 0, so 0 means the first element, 1 means the second element, and so on. Negative index is used to specify the element indexed from the end of the list. In this method, -1 means the last element, -2 means the second-to-last element, and move forward.
When the value of the key position is not a list, an error will be returned.
-
return value
The corresponding element of the request, or return nil when the index exceeds the range.
-
example
redis> LPUSH mylist "World" (integer) 1 redis> LPUSH mylist "Hello" (integer) 2 redis> LINDEX mylist 0 "Hello" redis> LINDEX mylist -1 "World" redis> LINDEX mylist 3 (nil) redis>
linsert: Insert before or after the specified element in the list
-
command
LINSERT key BEFORE|AFTER pivot value
-
Description
Insert the value into the list stored in the key before or after the pivot of the reference value.
When the key does not exist, the list will be regarded as an empty list, and no operation will take place.
When the key exists, but the saved list is not a list, error will be returned.
-
return value
The length of the list after the insertion operation, or -1 when the pivot value is not found.
-
example
redis> RPUSH mylist "Hello" (integer) 1 redis> RPUSH mylist "World" (integer) 2 redis> LINSERT mylist BEFORE "World" "There" (integer) 3 redis> LRANGE mylist 0 -1 1) "Hello" 2) "There" 3) "World" redis>
llen: Get the length of the list
-
command
LLEN key
-
Description
Returns the length of the list stored in the key. If the key does not exist, it is regarded as an empty list, and the return length is 0. When the value stored in the key is not a list, error will be returned.
-
return value
The length of the list corresponding to the key.
-
example
redis> LPUSH mylist "World" (integer) 1 redis> LPUSH mylist "Hello" (integer) 2 redis> LLEN mylist (integer) 2 redis>
lpop: remove and return the first element of the list corresponding to the key
-
command
LPOP key
-
return value
Return the value of the first element, or nil when the key does not exist.
-
example
redis> RPUSH mylist "one" (integer) 1 redis> RPUSH mylist "two" (integer) 2 redis> RPUSH mylist "three" (integer) 3 redis> LPOP mylist "one" redis> LRANGE mylist 0 -1 1) "two" 2) "three" redis>
lpush: interpolation at the head of the list
-
command
LPUSH key value [value ...]
-
Description
Insert all specified values into the head of the list stored in key. If the key does not exist, an empty list will be created before the push operation. If the value corresponding to the key is not a list, an error will be returned.
You can use one command to push multiple elements into the list, just add multiple specified parameters at the end of the command. The elements are inserted into the head of the list one by one from the leftmost to the rightmost. So for this command example
LPUSH mylist a b c
, the returned list is c as the first element, b as the second element, and a as the third element. -
return value
The length of the list after the push operation.
-
example
redis> LPUSH mylist "world" (integer) 1 redis> LPUSH mylist "hello" (integer) 2 redis> LRANGE mylist 0 -1 1) "hello" 2) "world" redis>
lpushx
-
command
LPUSHX key value
-
Description
Only when the key already exists and a list is stored, insert the value at the head of the list under the key. Contrary to LPUSH, no operation is performed when the key does not exist.
-
return value
The length of the list after the push operation.
-
example
redis> LPUSH mylist "World" (integer) 1 redis> LPUSHX mylist "Hello" (integer) 2 redis> LPUSHX myotherlist "Hello" (integer) 0 redis> LRANGE mylist 0 -1 1) "Hello" 2) "World" redis> LRANGE myotherlist 0 -1 (empty list or set) redis>
lrange: Get the specified range value of the specified key in the list
-
command
LRANGE key start stop
-
Description
Returns the elements in the specified range stored in the key list. The start and end offsets are both based on the subscript of 0, that is, the subscript of the first element of the list is 0 (the head of the list), the subscript of the second element is 1, and so on.
The offset can also be a negative number, which means that the offset is counted from the end of the list. For example, -1 means the last element of the list, -2 is the second to last element, and so on.
In different programming languages, on the consistency of the range function
Note that, if you have a list, which elements are from 0-100, then
LRANGE list 0 10
this command returns the 11 elements, namely the elements of the far right will also be included. In the programming language you are using, this point may or may not be consistent with those functions related to scope. (Like Ruby's Range.new, Array#slice or Python's range() function.)Subscript out of range
No error will be generated when the subscript exceeds the range of the list. If start is greater than the tail subscript of the list, an empty list will be returned. If stop is larger than the actual tail of the list, Redis will treat it as the index of the last element.
-
return value
List elements in the specified range.
-
example
redis> RPUSH mylist "one" (integer) 1 redis> RPUSH mylist "two" (integer) 2 redis> RPUSH mylist "three" (integer) 3 redis> LRANGE mylist 0 0 1) "one" redis> LRANGE mylist -3 2 1) "one" 2) "two" 3) "three" redis> LRANGE mylist -100 100 1) "one" 2) "two" 3) "three" redis> LRANGE mylist 5 10 (empty list or set) redis>
lrem: remove the specified number of values from the list
-
command
LREM key count value
-
Description
Remove the element with the value of value from the previous count occurrences from the list stored in the key. The count parameter affects this operation in the following ways:
- count> 0: Remove elements with value from the beginning to the end.
- count <0: Remove elements with value from the end to the beginning.
- count = 0: Remove all elements whose value is value.
For example, LREM list -2 "hello" will remove the last two occurrences of "hello" from the list stored in list.
It should be noted that if the key does not exist in the list, it will be treated as an empty list, so when the key does not exist, this command will return 0.
-
return value
The number of elements removed.
-
example
redis> RPUSH mylist "hello" (integer) 1 redis> RPUSH mylist "hello" (integer) 2 redis> RPUSH mylist "foo" (integer) 3 redis> RPUSH mylist "hello" (integer) 4 redis> LREM mylist -2 "hello" (integer) 2 redis> LRANGE mylist 0 -1 1) "hello" 2) "foo" redis>
lset: Set the value of the specified index
-
command
LSET key index value
-
Description
Set the value of the list element at the index position to value.
An error will be returned when the index is out of range.
-
example
redis> RPUSH mylist "one" (integer) 1 redis> RPUSH mylist "two" (integer) 2 redis> RPUSH mylist "three" (integer) 3 redis> LSET mylist 0 "four" OK redis> LSET mylist -2 "five" OK redis> LRANGE mylist 0 -1 1) "four" 2) "five" 3) "three" redis>
ltrim: modify the list to only include the specified range
-
command
LTRIM key start stop
-
Description
Trim an existing list so that the list will only contain the specified elements in the specified range. Both start and stop count from 0, where 0 is the first element (header) in the list, 1 is the second element, and so on.
For example:
LTRIM foobar 0 2
the list stored in foobar will be trimmed, and only the first 3 elements in the list will be kept.Start and end can also use negative numbers to indicate the offset from the end of the list, such as -1 for the last element in the list, -2 for the second to last, and so on.
Subscripts that exceed the range will not cause an error: if start exceeds the end of the list, or start> end, the result will be an empty list (that is, the key will be removed). If end exceeds the end of the list, Redis will treat it as the last element of the list.
LTRIM
A common usage of is to use withLPUSH
/RPUSH
with. E.g:- LPUSH mylist someelement
- LTRIM mylist 0 99
This pair of commands will push a new element into the list and ensure that the list will not grow to more than 100 elements. This is very useful, such as when using Redis to store logs. It is important to note that when using LTRIM in this way, the complexity of the operation is O(1), because on average, only one element will be removed at a time.
-
example
redis> RPUSH mylist "one" (integer) 1 redis> RPUSH mylist "two" (integer) 2 redis> RPUSH mylist "three" (integer) 3 redis> LTRIM mylist 1 -1 OK redis> LRANGE mylist 0 -1 1) "two" 2) "three" redis>
rpop: Remove and return the last element of the list stored in key.
-
command
RPOP key
-
return value
The value of the last element, or nil when the key does not exist.
-
example
redis> RPUSH mylist "one" (integer) 1 redis> RPUSH mylist "two" (integer) 2 redis> RPUSH mylist "three" (integer) 3 redis> RPOP mylist "three" redis> LRANGE mylist 0 -1 1) "one" 2) "two" redis>
rpoplpush: Put the tail element of one list at the head of another list
-
command
RPOPLPUSH source destination
-
Description
Atomically return and remove the last element of the list stored in source (the last element of the list), and put this element in the first element position of the list stored in the destination (the head of the list).
For example: Suppose source stores the lists a, b, and c, and destination stores the lists x, y, and z. The result of executing RPOPLPUSH is that the source stores the lists a, b, and the destination stores the lists c, x, y, and z.
If the source does not exist, it will return a nil value and do nothing. If the source and destination are the same, then this operation is equivalent to removing the last element of the list and placing that element at the head of the list, so this command can also be regarded as a command to rotate the list.
-
return value
Elements removed and put in
-
example
redis> RPUSH mylist "one" (integer) 1 redis> RPUSH mylist "two" (integer) 2 redis> RPUSH mylist "three" (integer) 3 redis> RPOPLPUSH mylist myotherlist "three" redis> LRANGE mylist 0 -1 1) "one" 2) "two" redis> LRANGE myotherlist 0 -1 1) "three" redis>
rpush: interpolation at the end of the list
-
command
RPUSH key value [value ...]
-
Description
Insert all the specified values into the end of the list stored in the key. If the key does not exist, an empty list will be created and then the push operation will be performed. When the key is not a list, an error will be returned.
You can use one command to put multiple elements into the queue, you only need to specify multiple parameters after the command. The elements are inserted from the end of the list one by one from left to right. For example, the command RPUSH mylist abc will return a list whose first element is a, the second element is b, and the third element is c.
-
return value
The length of the list after the push operation.
-
example
redis> RPUSH mylist "hello" (integer) 1 redis> RPUSH mylist "world" (integer) 2 redis> LRANGE mylist 0 -1 1) "hello" 2) "world" redis>
rpushx
-
command
RPUSHX key value
-
Description
Insert the value value into the end of the list key, if and only if the key exists and is a list. And
RPUSH
command the contrary, when the key is not present, RPUSHX command does nothing. -
return value
The length of the table after the RPUSHX command is executed.
-
example
redis> RPUSH mylist "Hello" (integer) 1 redis> RPUSHX mylist "World" (integer) 2 redis> RPUSHX myotherlist "World" (integer) 0 redis> LRANGE mylist 0 -1 1) "Hello" 2) "World" redis> LRANGE myotherlist 0 -1 (empty list or set) redis>