Document directory
- Blpop key1 [key2...] timeout
Redis provides a wide range of data structures, such as string, list, set, sorted set, and hashs. There are several complex data structures, such as list,
Set, sorted, and hashs are internally stored strings. The string here can be understood as a byte array with a length,
In actual use, you can store or convert the data type you need. For example, you can store an image or a video...
List is the most commonly used type in complex data structures. redis list is actually a two-way linked list. The internal storage element is string. You can check the source code of redis,
Use git to download source code:
Git clone git: // github.com/antirez/redis.git
Redis-Master
After completion, the src directory in the redis-Master Directory contains the adlist. h and adlist. C source files.
Open adlist. h and you can see that the definition of list is:
The listnode definition is:
From the code, we can see that each list uses LEN members to store the number of elements. Therefore, the time complexity of using the llen Command provided by redis to retrieve the list is O (1 ).
The List also records the headers and tails of the queue. Therefore, for the push and pop commands of the queue headers and tails, their time complexity is also O (1 ).
Redis provides a series of command operation lists. For details, see:
Http://www.redis.io/commands#list
Among them, the most interesting is the three commands starting with B: blpop, brpop, brpoplpush. These three commands involve the blocking version of the elements popped up from the queue,
When there are no elements in the queue, calling these three commands will wait until an element enters the queue or the timeout condition is reached. blocking pop commands can avoid database polling,
This reduces some pressure.
The formats of the blpop, brpop, and brpoplpush commands are similar. The blpop command format is as follows:
Blpop key1 [key2...] timeout
Timeout is used to specify the timeout time of this command, in seconds. When this value is set to 0, it indicates infinite blocking until an element is returned.
Key1, key2 ,... This command allows you to check whether multiple keys have elements at the same time. You can view the elements in sequence from beginning to end. First, check whether key1 has elements. If yes, return,
If no key is available, view key2, and so on. If all the queues with the specified key are empty, the blocking mode is enabled. During the blocking process, any list contains content and will be returned immediately.
Because blpop can specify multiple keys, when a command is returned, it must indicate that the elements of the key are returned. The return value of this command is two. When an element is returned,
The value 1st indicates the key name of the List, and the value 2nd stores the content of the element. When the time-out mode is used and no element is returned, the value 1st is nil, and the second value is the blocking time.
With the pop command of the blocking version, we can use redis to implement the producer-consumer mode. The producer stores data in a specific queue in the redis database, and the consumer extracts data from the queue,
When the data is empty, the consumer is blocked.
In the following example, two Lua programs are used to simulate the Order System of an online shopping website, which can be executed on two terminals separately. The following is the execution of the producer at Terminal 1:
The following Terminal 2 executes the consumer's:
Corresponding code, Producer:
-- Producer. Lua
Require "redis"
Local myclient = redis. Connect ()
Assert (myclient)
Local orderid = 0
While true do
Print ("Please input order ID :")
Orderid =
Io. Read ("* Number ")
If orderid = nil or orderid <0 then
Break
Else
Myclient: rpush ("orders", orderid)
-- Place order ID to orders queue
Print ("Order sucess .")
End
End
Consumer:
-- Consumer. Lua
Require "redis"
Myclient = redis. Connect ()
Assert (myclient)
While true do
Orderids = myclient: blpop ("orders", 0)
If
Orderids [1] ~ = Nil then
Print ("pop a value form", orderids [1])
Print ("/tprocessing order, ID:", orderids [2])
End
End