Redis command details and examples of usage scenarios--list

Source: Internet
Author: User
Tags redis trim
blpop key [key ...] timeout

Blpop is the list of blocked (blocking) pop-up primitives.
It is a blocked version of the Lpop command, and the connection is blocked by the Blpop command until a wait time-out or a popup element is found when no elements in the given list are available to eject.
When a number of key parameters are given, the first element of a non-empty list is ejected by checking each list in the order of key parameters.
non-blocking behavior
When Blpop is called, if there is at least one non-empty list in the given key, then the header element of the first non-empty list that is encountered is popped, together with the name of the list to which the popup element belongs, and the resulting component is returned to the caller.
When there are more than one given key, Blpop checks each list in sequence in the order given by the key parameter.
Suppose there are three lists of job, command, and request, where the job does not exist, and both command and request hold a non-empty list. Consider the following command:
Blpop Job Command Request 0
Blpop guarantees that the returned element is from command, because it is the first non-empty list found in the order of Find request with find command, search for job.

redis> DEL Job Command Request           # ensures that key is removed
(integer) 0
redis> lpush command "update system ..."  # is C Ommand list Add a value
(integer) 1
redis> lpush Request "Visit page"        # Add a value to the request List
(integer) 1
redis> blpop Job Command request 0       # The job list is empty, skipped, followed by the first element of the command list being popped.
1) The "command"                             # popup element belongs to the list
2) "Update system ..."                    # the value that the popup element belongs to

Blocking Behavior
If any given key does not exist or contains an empty list, then the Blpop command blocks the connection until the wait times out, or if another client executes the Lpush or Rpush command on any one of the given keys.
Timeout parameter timeout takes a number in seconds as the value. Setting the timeout parameter to 0 indicates that the blocking time can be extended indefinitely (block indefinitely).

redis> EXISTS Job                # ensures 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"                         # Here is push is job
2) "Do My Home Work"             # value popped
(26.26s)                         # wait seconds
redis> blpop job comma nd 5       # wait 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 put into a queue, and the Blpop command is executed for key in the order of first blocking first service (first-blpop,first-served).
The Blpop in the multi/exec transaction
Blpop can be used for pipelining (pipline, sending multiple commands in batches and reading multiple responses), but it is meaningless to use them in multi/exec blocks. This behavior prevents other clients from executing Lpush or Rpush commands because this requires the entire server to be blocked to ensure atomicity when the block executes.
Therefore, a blpop command, wrapped in a multi/exec block, behaves like Lpop, returns nil on an empty list, and no blocking action on a non-empty list pop-up list element.
To operate on a non-empty list

redis> Rpush Job Programming
(integer) 1
redis> MULTI
OK
redis> blpop job
QUEUED
Redis> EXEC           # does not block, immediately returns
1) 1) "Job"
   2) "Programming"
 operation on an empty list
redis> llen job      # empty list
(integer) 0
redis> MULTI
OK
redis> blpop Job
# QUEUED
redis> EXEC         # does not block , return immediately
1) (nil)

Available Versions:
2.0.0+
complexity of Time:
O (1)
return Value:
Returns a nil if the list is empty.
Otherwise, a list of two elements is returned, the first element is the key to which the popup element belongs, and the second element is the value of the popup element.
Mode: Event Reminder
Sometimes, in order to wait for a new element to arrive in the data, the data needs to be probed using a polling method.
Another better way is to use the system-provided blocking primitives to process the new element as soon as it arrives, and block the new element when it arrives, to avoid polling for resource usage.
For Redis, we seem to need a blocking version of the SPOP command, but in fact, using Blpop or Brpop is a good way to solve this problem.
The client (consumer) that uses the element can execute code similar to the following:

LOOP Forever while
    SPOP (key) returns elements
        ... process elements
    ... End
    Brpop helper_key
End

The client (consumer) that added the element executes the following code:

MULTI
    sadd key element
    lpush Helper_key x
EXEC
brpop key [key ...] timeout

Brpop is the list of blocked (blocking) pop-up primitives.
It is a blocked version of the Rpop command, and the connection is blocked by the Brpop command until a wait time-out or a popup element is found when no elements in the given list are available to eject.
When a number of key parameters are given, each list is checked sequentially in the order of key keys, and the trailing element of the first non-empty list pops up.
For more information about blocking operations, see the Blpop command, Brpop other than the location of the popup element and the Blpop.
Available Versions:
2.0.0+
complexity of Time:
O (1)
return Value:
If no element is ejected within the specified time, a nil and wait duration is returned.
Instead, a list of two elements is returned, the first element is the key to which the popup element belongs, and the second element is the value of the popup element.

Redis> Llen Course
(integer) 0
redis> Rpush course algorithm001
(integer) 1
redis> Rpush Course c++101
(integer) 2
redis> Brpop course
1) "Course"             # Popup element key
2) "C++101"             # The value of the popup element
Brpoplpush Source Destination Timeout

Brpoplpush is a blocking version of Rpoplpush, and Brpoplpush behaves as Rpoplpush when the given list source is not empty.
When the list source is empty, the Brpoplpush command blocks the connection until the wait times out, or if another client executes the Lpush or Rpush command against the source.
Timeout parameter timeout takes a number in seconds as the value. Setting the timeout parameter to 0 indicates that the blocking time can be extended indefinitely (block indefinitely).
For more information, please refer to the Rpoplpush command.
Available Versions:
2.2.0+
complexity of Time:
O (1)
return Value:
If no element is ejected within the specified time, a nil and wait duration is returned.
Instead, a list of two elements is returned, the first element is the value of the popup element, and the second element is the length of the wait.
Non-empty list

redis> brpoplpush msg reciver
"Hello moto"                        # Popup element value
(3.38s)                             # Wait Long
redis> Llen reciver
(integer) 1
redis> lrange reciver 0 0
1) "Hello moto"

Empty list

Redis> brpoplpush msg reciver 1
(nil)
(1.34s)
LINDEX Key Index

Returns the element in the list key, subscript as index.
The subscript (index) parameter start and stop all end with 0, that is, 0 represents the first element of the list, 1 represents the second element of the list, and so on.
You can also use a negative subscript to 1 for the last element of the list, 2 to represent the penultimate element of the list, and so on.
If key is not a list type, an error is returned.
Available Versions:
1.0.0+
complexity of Time:
O (n), n is the number of elements passed in the index process to the subscript.
Therefore, the LINDEX command is executed on the head and tail elements of the list, and the complexity is O (1).
return Value:
The element in the list that is labeled Index.
If the value of the index parameter is not within the range of the list (out of range), nil is returned.

redis> Lpush mylist "World"
(integer) 1
redis> lpush mylist "Hello"
(integer) 2
redis> LINDEX MyList 0
"Hello"
redis> LINDEX mylist-1
"World"
redis> LINDEX mylist 3        # index not in MyList Within the range
(nil)
Linsert Key before| After pivot value

Inserts a value in the list key, either before or after the value pivot.
When pivot does not exist in the list key, no action is taken.
When key does not exist, key is treated as an empty list, and no action is taken.
If key is not a list type, an error is returned.
Available Versions:
2.2.0+
complexity of Time:
O (n), n is the number of elements to look for in the pivot process.
return Value:
If the command executes successfully, returns the length of the list after the insert operation has completed.
If no pivot is found, return-1.
Returns 0 if key does not exist or is an empty list.

redis> rpush mylist "Hello"
(integer) 1
redis> Rpush mylist "World"
(integer) 2
redis> Linsert MyList before "World" "There"
(integer) 3
redis> lrange mylist 0-1
1) "Hello"
2) "There"
3) "Wo Rld

Inserts a non-empty list to find a pivot that does not exist

Redis> Linsert mylist before "Go" "Let's"
(integer)-1      
                          # failed

Execute the Linsert command on an empty list

Redis> EXISTS fake_list
(integer) 0
redis> Linsert fake_list before "Nono" "Gogogog"
(integer) 0  
Llen Key

Returns the length of the list key.
If key does not exist, then key is interpreted as an empty list, returning 0.
If key is not a list type, an error is returned.
Available Versions:
1.0.0
complexity of Time:
O (1)
return Value:
The length of the list key.
Empty list

redis> llen Job
(integer) 0

Non-empty list

redis> Lpush Job "Cook food"
(integer) 1
redis> Lpush job "has Lunch"
(integer) 2
redis> LLE N job
(integer) 2  
Lpop Key

Removes and returns the header element of the list key.
Available Versions:
1.0.0#
complexity of Time:
O (1)
return Value:
The head element of the list.
Returns nil when key does not exist.

Redis> Llen Course
(integer) 0
redis> Rpush course algorithm001
(integer) 1
redis> Rpush Course c++101
(integer) 2
redis> lpop Course  # Remove Header element
Lpush key value [value ...]

Inserts one or more value values into the table header of the list key
If there are multiple value values, then each value value is inserted in a left-to-right order into the header: for example, to execute a command on an empty list mylist Lpush mylist a b C, the value of the list will be C B A, which is equivalent to atomically performing Lpush mylist A, Lpush mylist B and Lpush mylist c three commands.
If the key does not exist, an empty list is created and the Lpush operation is performed.
An error is returned when key exists but is not a list type.
Before the Redis 2.4 version of the Lpush command, only a single value value is accepted.
Available Versions:
1.0.0+
complexity of Time:
O (1)
return Value:
The length of the list after the Lpush command is executed.
Join a single element

redis> Lpush languages Python
(integer) 1

Adding repeating elements

redis> Lpush languages Python
(integer) 2
redis> Lrange languages 0-1     # list allows repeating elements
1) "Python"
2) "Python"
joins multiple elements
redis> Lpush mylist a b C
(integer) 3
redis> lrange mylist 0-1
1) "C"
2 ) "B"
3) "a"   
lpushx Key Value

Inserts the value values into the table header of the list key when and only if key exists and is a list.
As opposed to the Lpush command, when key does not exist, the LPUSHX command does nothing.
Available Versions:
2.2.0+
complexity of Time:
O (1)
return Value:
The length of the table after the LPUSHX command executes.
Perform lpushx on an empty list

Redis> Llen greet                       # greet is an empty list
(integer) 0
redis> lpushx greet "Hello"             # try Lpushx, failed because the list is empty
(integer) 0
to non-empty list execution lpushx
redis> lpush greet "Hello"              # first create a list with one element with Lpush
(integer) 1<  C26/>redis> lpushx greet "Good Morning"      # This time lpushx execution succeeded
(integer) 2
redis> lrange greet 0-1
1) "Good Morning"
2) "Hello"   
lrange key Start Stop

Returns the element in the specified interval in the list key, with the interval specified by the offset start and stop.
The subscript (index) parameter start and stop all end with 0, that is, 0 represents the first element of the list, 1 represents the second element of the list, and so on.
You can also use a negative subscript to 1 for the last element of the list, 2 to represent the penultimate element of the list, and so on.
Note the difference between the Lrange command and the programming language interval function
If you have a list of 100 elements, perform lrange list 0 10 for that listing, the result is a list of 11 elements, indicating that the stop subscript is also within the range of the Lrange command (closed interval), which may be inconsistent with some of the language's interval functions, such as Ruby's The range () function for Range.new, Array#slice, and Python.
Subscript out of Range
An out-of-range subscript value does not cause an error.
If the start subscript is larger than the list's maximum subscript end (Llen list minus 1), then Lrange returns an empty list.
If the stop subscript is larger than the end subscript, Redis sets the value of stop to end.
Available Versions:
1.0.0+
complexity of Time:
O (s+n), S is the offset start, and N is the number of elements within the specified interval.
return value:
A list that contains the elements within the specified interval.

redis> Rpush fp-language lisp
(integer) 1
redis> lrange fp-language 0 0
1) "Lisp"
redis> Rpush F P-language Scheme
(integer) 2
redis> lrange fp-language 0 1
1) "Lisp"
2) "scheme"
Lrem Key Count value

Removes the element in the list that is equal to the parameter value, based on the value of the parameter count.
The value of count can be several of the following:
Count > 0: Searches from the header to the end of the table, removing elements equal to value, count.
Count < 0: Searches the header starting at the end of the table, removing the element equal to value, and the number is the absolute value of count.
Count = 0: Removes all values that are equal to value in the table.
Available Versions:
1.0.0+
complexity of Time:
O (n), n is the length of the list.
return Value:
The number of elements being removed.
Because the nonexistent key is considered an empty table, the Lrem command always returns 0 when key does not exist.
First, create a table with the contents arranged
Morning Hello morning Helllo morning

Redis> lpush greet "Morning"
(integer) 1
redis> lpush greet "Hello"
(integer) 2
redis> Lpush Greet "Morning"
(integer) 3
redis> lpush greet "Hello"
(integer) 4
redis> lpush greet "Morning" c8/> (integer) 5
redis> lrange greet 0 4         # View all elements
1) "Morning"
2) "Hello"
3) "Morning"
4 ) "Hello"
5) "Morning"
redis> lrem greet 2 morning     # Removes the first two morning
(integer) 2 # from the header to the end of the table                     Two elements removed
redis> llen greet               # 3 elements left
(integer) 3
redis> lrange greet 0 2
1) "Hello"
2 ) "Hello"
3) "Morning"
redis> lrem greet-1 Morning    # Remove from the end of the table to the header, first morning
(integer) 1
Redis> Llen greet               # Two elements left
(integer) 2
redis> lrange greet 0 1
1) "Hello"
2) "Hello"
redis> lrem greet 0 Hello      # Remove all Hello
(integer) 2 # in table                    two Hello is removed
redis> llen greet
  
    (integer) 0
  
LSET Key index value

Set the value of the element with the list key labeled Index to value.
An error is returned when the index parameter is out of range, or when an empty list (key does not exist) is LSET.
Refer to the LINDEX command for more information on List subscripts.
Available Versions:
1.0.0+
complexity of Time:
The LSET operation is performed on the head element or the tail element, and the complexity is O (1).
In other cases, O (n), n is the length of the list.
return Value:
The operation returned OK successfully, otherwise an error message is returned.
LSET The empty list (key does not exist)

redis> EXISTS list
(integer) 0
redis> LSET list 0 Item
(Error) ERR no such key
to non-empty list LSET
Redi s> Lpush Job "Cook food"
(integer) 1
redis> lrange Job 0 0
1) "Cook food"
redis> LSET job 0 "Pl Ay game "
ok
redis> lrange job  0 0
1)" Play Game "
index out of range
redis> llen List                    # List length 1
(integer) 1
redis> LSET List 3 ' Out of range '
(Error) ERR index out of range
LTRIM key Start Stop

Trim (Trim) a list, which means that the list retains only the elements within the specified interval, and the elements that are not within the specified range are deleted.
For example, execute the command LTRIM list 0 2, which means that only the first three elements of the list are preserved, and all the remaining elements are deleted.
The subscript (index) parameter start and stop all end with 0, that is, 0 represents the first element of the list, 1 represents the second element of the list, and so on.
You can also use a negative subscript to 1 for the last element of the list, 2 to represent the penultimate element of the list, and so on.
When key is not a list type, an error is returned.
LTRIM commands are usually used in conjunction with Lpush commands or Rpush commands, for example:
Lpush Log Newest_log
LTRIM Log 0 99
This example simulates a log program that places the latest log newest_log in the log list each time and keeps only the latest 100 items. Note that when you use the LTRIM command in this way, the time complexity is O (1) because, on average, only one element is removed each time.
Note the difference between the LTrim command and the programming language interval function
If you have a list of 100 elements, perform LTRIM list 0 10 on the list, the result is a list of 11 elements, indicating that the stop subscript is also within the range of the LTRIM command (closed interval), which may be inconsistent with some language interval functions, such as Ruby The Range () function of the range.new, Array#slice, and Python.
Subscript out of Range
An out-of-range subscript value does not cause an error.
If the start subscript is larger than the list's maximum subscript end (Llen list minus 1), or Start > Stop, LTRIM returns an empty list (because LTRIM has emptied the entire list).
If the stop subscript is larger than the end subscript, Redis sets the value of stop to end.
Available Versions:
1.0.0+
complexity of Time:
O (n), n is the number of elements removed.
return Value:
Returns OK when the command executes successfully.
Scenario 1: Common case, start and stop are within the index range of the list

redis> Lrange Alpha 0-1       # alpha is a list containing 5 strings
1) "H"
2) "E"
3) "L"
4) "L"
5) "O"
REDIS&G T LTRIM Alpha 1-1        # Delete an alpha list index of 0 element
OK
redis> lrange Alpha 0-1       # "H" was deleted
1) "E"
2) "L"
3) "L"
4) "O"

The situation 2:stop bigger than the list's maximum subscript.

redis> LTRIM Alpha 1 10086     # keep Alpha list index 1 to element on index 10086
OK
redis> lrange Alpha 0-1       # only meta on index 0 "E" was removed, the other elements were still
1) "L"
2) "L"
3) "O"

Conditions 3:start and stop are larger than the maximum subscript for the list, and start < stop

redis> LTRIM Alpha 10086 123321
OK
redis> lrange Alpha 0-1        # list is emptied
(empty list or set)

Conditions 4:start and stop are larger than the maximum subscript for the list, and start > Stop

redis> rpush New-alpha "h" "E" "L" "L" "O"     # re-establish a new list
(integer) 5
redis> lrange
new-alpha 0-1 1) "H"
2) "E"
3) "L"
4) "L"
5) "O"
redis> LTRIM new-alpha 123321 10086    # execute LTRIM
o K
redis> lrange new-alpha 0-1           # is also emptied
(empty list or set)
Rpop Key

Removes and returns the tail element of the list key.
Available Versions:
1.0.0+
complexity of Time:
O (1)
return Value:
The tail element of the list.
Returns nil when key does not exist.

Redis> Rpush mylist "one"
(integer) 1
redis> Rpush mylist "one"
(integer) 2
redis> Rpush mylist "Three"
(integer) 3
redis> rpop mylist           # Returns the element that was popped
"three"    redis> lrange mylist 0-1 # List of remaining elements
1) "One"
2) "One"
Rpoplpush Source Destination

Command Rpoplpush performs the following two actions within an atomic time:
POPs the last element in the list source (the trailing element) and returns it to the client.
Inserts a source popup element into the list destination, as the head element of the destination list.
For example, you have two lists of source and destination, the source list has elements a, B, C, destination list has element x, Y, Z, executes Rpoplpush sourcedestination, so The Urce list contains elements a, B, destination list contains elements 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 elements in the list are moved to the table header and returned, which can be considered as a rotation (rotation) operation of the list.
Available Versions:
1.2.0+
complexity of Time:
O (1)
return Value:
The element that is ejected.
Source and destination are different

redis> Lrange Alpha 0-1         # View all elements
1) "a"
2) "B"
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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.