This article will introduce you to the list command in Redis, and learn how to use the list command through code examples. I hope it will be helpful to you!
The List type is a double-ended linked list
structure with a capacity of 2 The 32nd power minus 1 element, that is, more than 4 billion; its main functions include push, pop, getting elements, etc.; it is generally used in stacks, queues, message queues
and other scenarios. [Related recommendations: Redis video tutorial]
[l/r]push-left /Add elements to the right
Syntax: [l/r]push key value [value ...]
Insert with head or tail Insert one or more elements into the specified key queue
127.0.0.1:6379> lpush pushkey 1 2 3 (integer) 3 127.0.0.1:6379> lpush pushkey 4 (integer) 4 127.0.0.1:6379> rpush pushkey 5 (integer) 5 127.0.0.1:6379> lrange pushkey 0 -1 1) "4" 2) "3" 3) "2" 4) "1" 5) "5"
lrange-query range element
Syntax: lrange key start stop
Get elements within the specified range of the list
127.0.0.1:6379> lpush products 1 2 3 (integer) 3 127.0.0.1:6379> lpush products 4 5 6 (integer) 6 127.0.0.1:6379> lrange products 0 -1 1) "6" 2) "5" 3) "4" 4) "3" 5) "2" 6) "1"
linsert-Insert elements before and after an element
Syntax :linsert key BEFORE|AFTER pivot value
Insert elements before or after the elements in the list
127.0.0.1:6379> lrange products 0 -1 1) "6" 2) "5" 3) "4" 4) "3" 5) "2" 6) "1" 127.0.0.1:6379> linsert products before 5 a (integer) 7 127.0.0.1:6379> lrange products 0 -1 1) "6" 2) "a" 3) "5" 4) "4" 5) "3" 6) "2" 7) "1" 127.0.0.1:6379> linsert products after a b (integer) 8 127.0.0.1:6379> lrange products 0 -1 1) "6" 2) "a" 3) "b" 4) "5" 5) "4" 6) "3" 7) "2" 8) "1"
llen-get the length
Syntax: llen key
Get the length of the list
127.0.0.1:6379> lrange products 0 -1 1) "6" 2) "a" 3) "b" 4) "5" 5) "4" 6) "3" 7) "2" 8) "1" 127.0.0.1:6379> llen products (integer) 8
lindex-Get the element based on the subscript
Syntax: lindex key index
Get the elements in the list by index
127.0.0.1:6379> lrange products 0 -1 1) "6" 2) "a" 3) "b" 4) "5" 5) "4" 6) "3" 7) "2" 8) "1" 127.0.0.1:6379> lindex products 2 "b"
lset-According to the following Index setting value
Syntax:lset key index value
Set the value of the list element by index
127.0.0.1:6379> lrange products 0 -1 1) "6" 2) "a" 3) "b" 4) "5" 5) "4" 6) "3" 7) "2" 8) "1" 127.0.0.1:6379> lset products 2 B OK 127.0.0.1:6379> lrange products 0 -1 1) "6" 2) "a" 3) "B" 4) "5" 5) "4" 6) "3" 7) "2" 8) "1"
ltrim-Intercept elements
Syntax: ltrim key start end
Intercept the elements in the specified range of the queue, and delete the remaining elements
127.0.0.1:6379> lrange products 0 -1 1) "6" 2) "a" 3) "B" 4) "5" 5) "4" 6) "3" 7) "2" 8) "1" 127.0.0.1:6379> ltrim products 0 3 OK 127.0.0.1:6379> lrange products 0 -1 1) "6" 2) "a" 3) "B" 4) "5"
lrem-Remove elements
Syntax: lrem key count value
Remove list elements
127.0.0.1:6379> lpush test a 1 a 2 a 3 a 4 5 6 (integer) 10 127.0.0.1:6379> lrange test 0 -1 1) "6" 2) "5" 3) "4" 4) "a" 5) "3" 6) "a" 7) "2" 8) "a" 9) "1" 10) "a" 127.0.0.1:6379> lrem test 3 a (integer) 4 127.0.0.1:6379> lrange test 0 -1 1) "6" 2) "5" 3) "4" 4) "3" 5) "2" 6) "1" 7) "a"
[l/r]pop-pop element from left/right
##Syntax:[l/r]pop key
127.0.0.1:6379> lrange test 0 -1 1) "6" 2) "5" 3) "4" 4) "3" 5) "2" 6) "1" 127.0.0.1:6379> lpop test "6" 127.0.0.1:6379> lrange test 0 -1 1) "5" 2) "4" 3) "3" 4) "2" 5) "1" 127.0.0.1:6379> rpop test "1" 127.0.0.1:6379> lrange test 0 -1 1) "5" 2) "4" 3) "3" 4) "2"
rpoplpush-remove the right element and add it to another A list left
Syntax:rpoplpush source destination
127.0.0.1:6379> lpush src 1 2 3 (integer) 3 127.0.0.1:6379> lrange src 0 -1 1) "3" 2) "2" 3) "1" 127.0.0.1:6379> lpush dst a b c (integer) 3 127.0.0.1:6379> lrange dst 0 -1 1) "c" 2) "b" 3) "a" 127.0.0.1:6379> rpoplpush src dst "1" 127.0.0.1:6379> lrange src 0 -1 1) "3" 2) "2" 127.0.0.1:6379> lrange dst 0 -1 1) "1" 2) "c" 3) "b" 4) "a"
b[l/r]pop-blocks popping one element left/right
Syntax:b[l/r]pop key1 [key2 ...] timeout
block the listUntil the wait
times out or
finds a pop-up element.
127.0.0.1:6379> lpush list1 1 2 (integer) 2 127.0.0.1:6379> lpush list2 a b (integer) 2 127.0.0.1:6379> lrange list1 0 -1 1) "2" 2) "1" 127.0.0.1:6379> lrange list2 0 -1 1) "b" 2) "a" 127.0.0.1:6379> blpop list1 list2 10 1) "list1" #弹出元素所属的列表 2) "2" #弹出元素所属的值 127.0.0.1:6379> blpop list1 list2 10 1) "list1" 2) "1" 127.0.0.1:6379> blpop list1 list2 10 1) "list2" 2) "b" 127.0.0.1:6379> blpop list1 list2 10 1) "list2" 2) "a" 127.0.0.1:6379> blpop list1 list2 10 (nil) (10.08s) # 列表为空的时候,就等待超时10秒
Introduction to Programming! !
The above is the detailed content of Practical combat: Let's talk about the list command in Redis through sample code. For more information, please follow other related articles on the PHP Chinese website!