Redis List---Rpoplpush

Source: Internet
Author: User

1.RPOPLPUSH

Lpoplpush Source Destination

Command Rpoplpush in an atomic time, perform the following two actions:

① pops the last element of the list source (the tail element) and returns it to the client.

② inserts the elements that are ejected from source into the list destination as the head element of the destination list.

For example, you have two list source and Destination,source list have element a,b,c,destination list have element x,y,z, after executing rpoplpush source destination, The source list contains the element a,b,destination list containing the element c,x,y,z, and element c is returned to the client.

If source does not exist, the value nil is returned and no other action is performed.

If source and destination are the same, the footer element in the list is moved to the header and returned to the element, which can be viewed as a rotation of the list.

return value:

The element that is ejected.

Command:

# source and destination different
redis> Lrange Alpha 0-1         # View all elements
1 "a"
2) "B"
3) "C"
4) "D"
Red is> Rpoplpush Alpha Reciver   # perform once rpoplpush see
"D"
redis> lrange Alpha 0-1
1) "a"
2) "B" 
  3) "C"
redis> lrange reciver 0-1
1) "D"
redis> Rpoplpush Alpha Reciver   # to be executed again, confirming Rpop and Lpus H's position is correct
"C"
redis> lrange Alpha 0-1
1) "A" 2 "
" B "redis> lrange reciver 0-1
1)" C "
  2) "D"
 
# source and destination same
redis> lrange number 0-1
1) "1"
2) "2"
3)
"3" 4 "
redis> rpoplpush Number number
" 4 "
redis> lrange number 0-1           # 4 is rotated to table header
1)" 4 "
2) "1"
3) "2"
4) "3"
redis> rpoplpush number
"3"
redis> lrange number 0-1           # this is 3 Rotated to Table Head
1) "3"
2) "4" 3
) "1"
4 "2"
Mode: Secure queue

Redis lists are often used as queues to exchange messages sequentially between different programs, one client puts messages into the queue through the Lpush command, and another client takes the longest waiting message in the queue through Rpop or Brpop commands.

Unfortunately, the above queue method is unsafe, because in this process, a client may crash after taking out a message, and the unhandled message will be lost.

Using the Rpoplpush command solves this problem because it not only returns a message but also adds the message to another backup list, and if it's all right, when a client finishes processing a message, you can remove the message from the backup table with the Lrem command.

Finally, you can add a client dedicated to monitoring the backup table, which automatically handles more than one

The time limit message is put back into the queue (the client responsible for processing the message may have crashed) so that no messages will be lost.

Patterns: Looping lists

By using the same key as the two parameters of the Rpoplpush command, the client can take the list element one by one, taking all the elements of the list, without having to transfer all the list elements from the server to the client as quickly as the Lrange command (the total complexity of all two ways is O (N)).

The above mode works even under two conditions:

① has multiple clients that rotate a list at the same time, they get different elements, they know that all the elements are read out, and then start from scratch.

② A client adds a new element to the tail of the list (right).

This pattern makes it easy to implement a system that has n clients, requires continuous processing of some elements, and processes must be as fast as possible. A typical example is the server's monitoring program: they need to check a set of Web sites in parallel in the shortest possible time to ensure their accessibility.

Note: Clients using this pattern are easy to expand and secure, because even if the client that receives the element fails, the element is kept in the list and is not lost until the next iteration arrives, and the other clients can continue to process the elements.

2.RPUSH

Rpush key value [value ...]

Inserts one or more value values into the footer of the list key (rightmost).

If there are multiple value values, the value values are inserted sequentially from left to right to the end of the table: for example, an empty list mylist executes Rpush mylist a B C, the resulting list is a B C, which is equivalent to executing the command Rpush mylist A, Rpush MyList B, Rpush mylist c.

If key does not exist, an empty list is created and a rpush operation is performed.

Returns an error when the key exists but is not a list type.

return value:

The length of the table after performing the Rpush operation.

Command:

# Add a single element
redis> Rpush languages C
(integer)
# 1 # Add repeating element
redis> Rpush languages C
(integer) 2
  redis> Lrange Languages 0-1 # list allows repeat element
1) "C"
2) "C"
# Add multiple elements
redis> Rpush mylist a b c
(int Eger) 3
redis> lrange mylist 0-1
1) "a"
2) "B"
3) "C"
3.RPUSHX

Rpush Key value

Inserts value values into the footer of the list key, if and only if key exists and is a non-empty list.

In contrast to the Rpush command, the Rpush command does nothing when the key does not exist.

return value:

After the RPUSHX command executes, the length of the table

Command:

# key does not exist
redis> llen greet
(integer) 0 redis> rpushx greet
"Hello"     # failed to rpushx,push the nonexistent key.
(integer) 0
# key exists and is a non-empty list
redis> rpush greet "HI"         # First insert an element with Rpush
(integer) 1
redis> Rpushx greet "Hello"     # greet is now a list type, rpushx operation succeeded.
(integer) 2
redis> lrange greet 0-1
1) "Hi"
2 "Hello"
4.BLPOP

Blpop key [key ...] timeout

Blpop is the blocked pop-up primitive of the list.

It is a blocked version of the Lpop command, and the connection is blocked by the Blpop command until a timeout is waiting or a popup element is found when there is no element available to eject in the given list.

When a number of key parameters are given, the list is checked sequentially in the order of the parameter key, and the header element of the first Non-empty list is popped.

Non-blocking behavior

When Blpop is invoked, if there is at least one Non-empty list within the given key, the first non-empty list of the forehead elements encountered pops up and is returned to the caller by the name of the list to which the pop-up element belongs.

When more than one given key is present, Blpop examines each list sequentially, in order of the given key parameters.

Suppose you now have a list of Job,command and request three, where the job does not exist, and both command and request hold a non-empty list. Consider the order:

Blpop Job Command Request 0

Blpop guarantees that the returned element comes from the command because it is the first Non-empty list found in the order of find job----> Find command-----> Find request.

Command:

redis> DEL Job Command           # ensures that key is deleted
(integer) 0
redis> lpush command "update system ..."  # for C Ommand list Add one value
(integer) 1
redis> lpush Request "Visit page"        add a value for the request List
(integer) 1
The redis> blpop Job command request 0       # Job list is empty, skipped, followed by the first element of the command list being ejected.
1) "Command"                             # pop-up element to list
2) "Update system ..."                    # pop-up element belongs to value
Blocking behavior

If none of the given keys exists or contains an empty list, the Blpop command blocks the connection until the wait time out, or if another client executes the Lpush or Rpush command for any one of the given key.

The timeout parameter timeout accepts a number in seconds as a value. Setting the timeout parameter to 0 indicates that blocking time can be extended indefinitely

Command:

redis> EXISTS Job                # Ensure that two keys do not exist
(integer) 0
redis> EXISTS command
(integer) 0
redis> Blpop job Command     # because the key does not exist at first, the operation is blocked until another client makes a PUSH operation on the job or command list.
1) "Job"                         # is pushed here is Job
2) "Do My Home Work"             # The value that is ejected
(26.26s)                         # Number of seconds to wait
redis> blpop job comma nd 5       # waiting timeout
(nil)
(5.66s)                          # Number of seconds to wait


The same key is blocked by multiple clients at the same time

The same key can be blocked by multiple clients at the same time

Different clients are placed in a queue, and the key is executed Blpop command in the order of first blocking first service.

The Blpop in multi/exec things

Blpop can be used in pipelining (pipline, bulk sending multiple commands and read multiple replies), but it doesn't make sense to use it in multi/exec blocks. Because this requires the entire service to be blocked to ensure the atomicity of block execution, the behavior prevents other clients from executing Lpush or Rpush commands.

As a result, a blpop command wrapped in a multi/exec block behaves like Lpop, returning nil to an empty list, pop-up list elements from a non-empty list, and no blocking operations.

Command:

# operation on a non-empty list
redis> rpush job Programming
(integer) 1
redis> MULTI
OK
redis> blpop Job 30< c5/>queued
redis> EXEC           # No blocking, immediate return
1 1) "Job"
   2 "Programming"
# to operate on an empty list
redis> Llen Job      # Empty list
(integer) 0
redis> MULTI
OK
redis> blpop job
QUEUED
EXEC         # No blocking, return
1 Now) (nil)
return value:

Returns a nil if the list is empty.

Otherwise, a list containing two elements is returned, the first element is the key to which the pop-up element belongs, and the second element is the value of the element being ejected.

Patterns: Event Reminders

Sometimes, in order to wait for a new element to arrive in the data, polling is used to probe the data.

Another better way is to use the blocking primitives provided by the system to process the new element as soon as it arrives, blocking it until the new element arrives, and avoiding polling for resource usage.

For Redis, we seem to need a blocked version of the Spop command, but in fact, using Blpop or Brpop is a good solution to this problem.

Use element clients (consumers) to perform code similar to the following:

Loop Forever while
    Spop (key) returns elements ...
        process elements ...
    End
    Brpop helper_key
End
The client (producer) that adds the element executes the following code:

MULTI
    sadd key element
    lpush Helper_key x
EXEC
5.BRPOP

Brpop key [key ...] timeout

Brpop is the blocked pop-up primitive of the list.

It is a blocked version of the Rpop command, and the connection is blocked by the Brpop command until a timeout is waiting or a popup element is found when there is no element available to eject in the given list.

When multiple key parameters are given, the list is checked sequentially by the parameter key, and the trailing element of the first Non-empty list is popped.

return value:

If no elements are ejected within a specified time, return a nil and wait time.

Instead, returns a list of two elements, the first element being the key to which the pop-up element belongs, and the second element being the value of the pop-up element.

Command:

Redis> Llen Course
(integer) 0
redis> Rpush course algorithm001
(integer) 1
redis> Rpush Course c++101
(integer) 2
redis> Brpop course
1) "Course"             # is the list key that the pop-up element belongs to (
2) "C++101"             # Elements that are ejected

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: