Insert and eject
The command line | Action and return value |
---|---|
lpush |
If there are more than one value, each value is inserted in the header from left to right. If the key does not exist, an empty list is created and an LPUSH operation is performed.An error is returned when the key exists but is not of list type. |
lpop |
Removes and returns the header element of the list key. Return value: the first element of the list. Returns nil if key does not exist. |
rpush |
Inserts one or more values value at the end of the list key (rightmost). If there are multiple values, each value is inserted at the end of the table in left-to-right order |
rpop |
Removes and returns the last element of the list key. |
lrange |
Returns the element in the list key within the range specified by the offsets start and stop.details |
127.0.0.1:6379> lpush mylist a b c d e f Insert from left,
(integer) 6 127.0.0.1:6379> lrange myList 0-1 # query 1)"f"
2) "e"
3) "d"
4) "c"
5) "b"
6) "a"127.0.0.1:6379> rpush mylist2 a b c d e finteger) 6
127.0.0.1:6379> lrange mylist2 0 -1
1) "a"
2) "b"
3) "c"
4) "d"
5) "e"
6) "f"127.0.0.1:6379> lpop mylist# pop from the left"f"127.0.0.1:6379> lrange myList 0-1 1)"e"
2) "d"
3) "c"
4) "b"
5) "a"127.0.0.1:6379 > rpop mylist# Eject from the right
"a"127.0.0.1:6379> lrange myList 0-1 1)"e"
2) "d"
3) "c"
4) "b"
Copy the code
Get the length and remove
The command | Action and return value |
---|---|
lrem |
Removes elements in the list that are equal to value based on the value of count. The value of count can be one of the following: count > 0: Searches from the top of the table to the bottom of the table and removes elements equal to value. Count < 0: Searches from the end of the table to the top, removing elements equal to value, the absolute value of count.Count = 0: Removes all values equal to value from the table. |
llen |
Returns the length of the list key. If the key does not exist, the key is interpreted as an empty list and 0 is returned. If the key is not a list type, an error is returned. |
lindex |
Returns the index element in the list key. The index arguments start and stop have a base of 0, that is, 0 for the first element of the list, 1 for the second element of the list, and so on. You can also use negative subscripts, -1 for the last element of the list, -2 for the next-to-last element of the list, and so on.If the key is not a list type, an error is returned. |
127.0.0.1:6379> lpush mylist a a b c d e
(integer) 6 127.0.0.1:6379> lrange myList 0-1 1)"e"
2) "d"
3) "c"
4) "b"
5) "a"
6) "a"
127.0.0.1:6379> lrem mylist 1 b # remove a b
(integer) 1 127.0.0.1:6379> lrange myList 0-1 1)"e"
2) "d"
3) "c"
4) "a"
5) "a"127.0.0.1:6379> lrem myList 2a# Remove two as
(integer) 2 127.0.0.1:6379> lrange myList 0-1 1)"e"
2) "d"
3) "c"127.0.0.1:6379 > llen mylistGet the number of elements in the list
(integer) 3
127.0.0.1:6379> lindex mylist 1 Get the element with subscript 1, starting at 0
"d"127.0.0.1:6379 >Copy the code
The interception
The command | Action and return value |
---|---|
ltrim |
To trim a list, that is, to keep only elements within a specified range, and to remove all elements that are not within a specified range. |
127.0.0.1:6379> rpush mylist a b c d e f g
(integer) 7 127.0.0.1:6379> lrange myList 0-1 1)"a"
2) "b"
3) "c"
4) "d"
5) "e"
6) "f"
7) "g"
127.0.0.1:6379> ltrim mylist 2 5 Keep only elements within the specified rangeOK 127.0.0.1:6379> lrange myList 0-1 1)"c"
2) "d"
3) "e"
4) "f"127.0.0.1:6379 >Copy the code
Combined command
rpoplpush |
Pops the last element (tail element) in the list source and returns it to the client. Insert the element that pops up from source into the list Destination as the header element of the Destination list.If source does not exist, the value nil is returned and no other action is performed. If the source and destination are the same, the end-of-table element in the list is moved to the table head and returned. You can think of this as a rotation operation for the list. |
127.0.0.1:6379> rpush mylist a b c d e f
(integer) 6 127.0.0.1:6379> lrange myList 0-1 1)"a"
2) "b"
3) "c"
4) "d"
5) "e"
6) "f"127.0.0.1:6379 > rpoplpush mylist mylist2# Pop the current list to the new list
"f"127.0.0.1:6379> lrange myList 0-1 1)"a"
2) "b"
3) "c"
4) "d"
5) "e"
127.0.0.1:6379> lrange mylist2 0 -1
1) "f"
Copy the code
Determine and specify position set values
exists |
Returns 1 if key exists, 0 otherwise. |
lset |
Sets the value of the list key subscript index to value.An error is returned when the index argument is out of range, or when LSET is performed on an empty list (key does not exist). |
linsert |
Inserts the value value into the list key, before or after the value pivot. When pivot does not exist in the list key, nothing is done. If the key does not exist, the key is regarded as an empty list and no operation is performed. If the key is not a list type, an error is returned. |
127.0.0.1:6379 > exists myslit3 (integer) 0 # there is no127.0.0.1:6379 > exists mylist (integer) 1 127.0.0.1:6379> lrange myList 0-1 1)"a"
2) "b"
3) "c"
4) "d"
5) "e"
127.0.0.1:6379> lset mylist 2 zz # set the subscript 2 in the list to zzOK 127.0.0.1:6379> lrange myList 0-1 1)"a"
2) "b"
3) "zz"
4) "d"
5) "e"
127.0.0.1:6379> rpush mylist3 kk
(integer) 1
127.0.0.1:6379> lrange mylist3 0 0
1) "kk"127.0.0.1:6379> linsert mylist3 before kk BBBInsert BBB before kk in list
(integer) 2
127.0.0.1:6379> lrange mylist3 0 -1
1) "bbb"
2) "kk"
127.0.0.1:6379> linsert mylist3 after kk aaa Insert aaa after kk in list
(integer) 3
127.0.0.1:6379> lrange mylist3 0 -1
1) "bbb"
2) "kk"
3) "aaa"
Copy the code