Previous: redis learning notes – (4) – set
List related commands:
lpush/rpush/lpop/rpop/brpop/blpop
5.1 stack = lpush + lpop (in and out of the same end)
lpush+lpop
127.0.0.1:6379> lpush juc synchronized volatile aqs thread
(integer) 4
127.0.0.1:6379 > lpop JUC # last in, first out
"thread"
127.0.0.1:6379 > lpop JUC # penultimate
"aqs"
127.0.0.1:6379 > lpop JUC # penultimate
"volatile"
127.0.0.1:6379>
5.2 queue = lpush + rpop (both ends in and out)
lpush+rpop
127.0.0.1:6379> del juc
(integer) 1
127.0.0.1:6379> lpush juc synchronized volatile aqs thread
(integer) 4
127.0.0.1:6379 > rpop JUC # first in first out
"synchronized"
127.0.0.1:6379 > rpop JUC # first in first out
"volatile"
127.0.0.1:6379 > rpop JUC # first in first out
"aqs"
127.0.0.1:6379>
5.3 blocking queue = lpush + brpop (pop blocking)
lpush+brpop
Brpop syntax:
brpop key [key...] timout
timeout = 0Indicates that if there is no data insertion, it will be blocked all the time;
timeout = 5Indicates blocking for 5 seconds. If there is no value at the time, null will be returned;
Example:
127.0.0.1:6379> del juc
(integer) 0
127.0.0.1:6379 > lpush JUC synchronized volatile AQS thread # key = JUC 4 words
(integer) 4
127.0.0.1:6379 > brpop JUC 5 # pop one from JUC. If there is no element, wait for 5S
1) "juc"
2) "synchronized"
127.0.0.1:6379> brpop juc 5
1) "juc"
2) "volatile"
127.0.0.1:6379> brpop juc 5
1) "juc"
2) "aqs"
127.0.0.1:6379> brpop juc 5
1) "juc"
2) "thread"
127.0.0.1:6379 > brpop JUC 5 # pop one from JUC. If there is no element, and there is no new push after 5S, null will be returned
(nil)
(5.07s)
#At this time, another terminal redis cli is opened and executed: ` lpush JUC semaphore`
127.0.0.1:6379 > brpop JUC 0 # pop one from JUC. If there is no element, the block will wait
1) "juc"
2) "semaphore"
(15.96s)
127.0.0.1:6379>