List Type (list) can store an ordered list of strings, the internal implementation is a doubly linked list , with list type, it may also be used as the Redis queue used, and the same number with the hash key type field can accommodate up of a list of key type It can accommodate up to 2 ^ 32-1 elements
Common Commands
Left \ right insert elements
lpush key value [value ....] add elements from the left to the list rpush key value [value ....] add elements from the right to the list Numbers LPUSH . 1 LPUSH Numbers 2 . 3 in this case . 3 2 . 1 Numbers RPUSH 0 RPUSH Numbers - . 1 in this case . 3 2 . 1 0 - . 1
Left \ right pop-up element (the return value of the element)
lpop key from the left pop-up rpop key right pop A list of numbers . 3 2 . 1 0 - . 1 lpop numbers "3" rpop numbers "-1"
Some other uses 1
The number of elements in the list obtained: llen key llen number 3 Get a list of fragments: lrange key start stop lrange number 0 2 1) "2" 2) "1" 3) "0" This command also supports negative indexes lrange number -2 -1 1) "1" 2) "0" It represents the right of the second element to the first element on the right To display all stored string number and do not know the number of lrange number 0 -1 Delete List specified value: lrem key count value If the list is 2 1 0 2 lrem a Numbers - 1 2 : first 2 will be deleted from the right of lrange number 0 -1 1) "2" 2) "1" 3) "0"
Obtaining / setting element values specified index
Get / set the specified value of the index element
lindex key index lset key index value 对于number ["2", "1", "0"] lindex number 0 "2" The first number is obtained from the left lindex number -1 "0" The first number to get the right start Number LSET . 1 7 [ 2 , . 1 , 0 ] to [ 2 , 7 , 0 ] The index value of 1 to " 7 "
Preserve the specified segment
ltrim key start end You can delete the specified index outside the range of all the elements, as the index specified range and lrange number[1,2,7,3] ltrim number 1 2 1,2 是 "2","7" To delete an index outside the scope of So is " 2 " , " 7 "
Insert elements
linsert key before|after pivot value From left to right pivot find elements of value, then according to bofore | the After considering the elements inserted in front of or behind number [2, 7, 0] linset number after 7 3 number [2, 7, 3, 0]