Redis lists are simple lists of strings, sorted by insertion order. You can add an element to the head (left) or tail (right) of the list
A list can contain up to 232 - 1 elements (4294967295, over 4 billion elements per list).
example
redis 127.0.0.1:6379>LPUSH runoobkey redis (integer) 1 redis 127.0.0.1:6379> LPUSH runoobkey mongodb (integer) 2 redis 127.0.0.1:6379> LPUSH runoobkey mysql (integer) 3 redis 127.0.0.1:6379 > LRANGE runoobkey 0 10 1) "mysql" 2) "mongodb" 3) "redis"
In the above example we used LPUSH to insert three values into a list named runoobkey .
Redis list command
The following table lists the basic list-related commands:
serial number | Command and Description |
---|---|
1 | BLPOP key1 [key2] timeout removes and fetches the first element of the list, if the list has no elements it blocks the list until the wait times out or a popable element is found. |
2 | BRPOP key1 [key2 ] timeout Move out and get the last element of the list, if the list has no elements it will block the list until the wait times out or a popable element is found. |
3 | BRPOPLPUSH source destination timeout pops a value from a list, inserts the popped element into another list and returns it; if the list has no elements, it blocks the list until the wait times out or an element to pop is found. |
4 | LINDEX key index Get the elements in the list by index |
5 | LINSERT key BEFORE|AFTER pivot value inserts elements before or after the elements of the list |
6 | LLEN key to get the length of the list |
7 | LPOP key move out and get the first element of the list |
8 | LPUSH key value1 [value2] inserts one or more values into the head of the list |
9 | LPUSHX key value inserts a value into the head of an existing list |
10 | LRANGE key start stop Get elements within the specified range of the list |
11 | LREM key count value removes list elements |
12 | LSET key index value Sets the value of a list element by index |
13 | LTRIM key start stop trims a list, that is to say, let the list retain only the elements in the specified range, and the elements that are not in the specified range will be deleted. |
14 | RPOP key removes and gets the last element of the list |
15 | RPOPLPUSH source destination removes the last element of a list and adds that element to another list and returns |
16 | RPUSH key value1 [value2] add one or more values to a list |
17 | RPUSHX key value adds a value to an existing list |