Redis List
Redis lists are simple lists of strings, sorted by insertion order. You can add an element to the head (left) or tail (right) of a list.
A list can contain up to 232- 1 elements (4294967295, more than 4 billion elements per list).
Example
redis 127.0.0.1:6379> LPUSH examplekey redis (integer) 1 redis 127.0.0.1:6379> LPUSH examplekey mongodb (integer) 2 redis 127.0.0.1:6379> LPUSH examplekey mysql (integer) 3 redis 127.0.0.1:6379> LRANGE examplekey 0 10 1) "mysql" 2) "mongodb" 3) "redis"
In the above example we usedLPUSHto insert three values into a list namedexamplekeylist.
Redis List Commands
The following table lists the basic commands related to lists:
| No. | Command and Description |
|---|---|
| 1 | BLPOP key1 [key2 ] timeout Remove and get the first element of a list. If the list has no elements, it will block the list until waiting times out or a popable element is found. |
| 2 | BRPOP key1 [key2 ] timeout Remove and get the last element of a list. If the list has no elements, it will block the list until waiting times out or a popable element is found. |
| 3 | BRPOPLPUSH source destination timeout Pop a value from a list, insert the popped element into another list and return it; if the list has no elements, it will block the list until waiting times out or a popable element is found. |
| 4 | LINDEX key index Get an element in a list by its index |
| 5 | LINSERT key BEFORE|AFTER pivot value Insert an element before or after an element in the list |
| 6 | LLEN key Get the length of a list |
| 7 | LPOP key Remove and get the first element of a list |
| 8 | LPUSH key value1 [value2] Insert one or more values at the head of a list |
| 9 | LPUSHX key value Insert a value at the head of an existing list |
| 10 | LRANGE key start stop Get elements in a specified range from a list |
| 11 | LREM key count value Remove list elements |
| 12 | LSET key index value Set the value of a list element by index |
| 13 | LTRIM key start stop Trim a list, that is, make the list keep only elements in the specified range, and all elements outside the specified range will be deleted. |
| 14 | RPOP key Remove the last element of a list, and the return value is the removed element. |
| 15 | RPOPLPUSH source destination Remove the last element of a list, add it to another list, and return it |
| 16 | RPUSH key value1 [value2] Add one or more values to the tail of a list |
| 17 | RPUSHX key value Add values to an existing list |