List is one of the underlying data types of Redis, similar to LinkedList in Java. A List can contain up to 232 elements and is often used to simulate queue operations. Let’s look at list-related commands in detail.

BLPOP

Earliest available version: 2.0.0

Time complexity: O(1)

Usage:

BLPOP key [key ...]  timeoutCopy the code

BLPOP is a blocking version of LPOP, and the connection is blocked when there are no elements in the list that can be popped. Given multiple keys, the lists are checked in the order of argument keys, and the first non-empty list element is popped. Timeout indicates the maximum number of seconds for blocking, and timeout 0 indicates infinite blocking.

There is a question about what the blocking BLPOP command does when multiple elements are pushed into a list at the same time. Before explaining this, let’s consider how this would happen:

  • Execute LPUSH myList a b c on list
  • Multiple push operations on the same list are performed within a transaction
  • Use versions later than Redis2.6 to execute Lua scripts for push

Versions of Redis2.4 and later deal with this issue differently.

Suppose client A runs A command

BLPOP mylist 0
Copy the code

Mylist will be empty, client A will block, and client B will execute

LPUSH mylist a b c
Copy the code

After Redis2.6, client A would return C, because client Bpush elements A, B, and C in left-to-right order C, B, and A. But in Redis2.4, the client would be in the push context, so when LPUSH started pushing the first element in the list, It gets sent to client A, so client A receives client A.

Sometimes we have a need to block a queue waiting for a new element of a Set, which would require a blocking version of SPOP, but such commands are not currently supported. However, we can use the BLPOP command to do this, and here is the pseudo-code:

Consumer:

LOOP forever
    WHILE SPOP(key) returns elements
        ... process elements ...
    END
    BRPOP helper_key
END
Copy the code

Producers:

MULTI
SADD key element
LPUSH helper_key x
EXEC
Copy the code

BRPOP

Earliest available version: 2.0.0

Time complexity: O(1)

It is basically the same as BLPOP, except that it pops elements from the tail, whereas BLPOP pops elements from the head.

BRPOPLPUSH

Earliest available version: 2.2.0

Time complexity: O(1)

Usage:

BRPOPLPUSH source destination timeout
Copy the code

It is a blocked version of RPOPLPUSH and behaves like RPOPLPUSH when the source contains elements. When the source is empty, Redis blocks until another client pushes the element or reaches the timeout limit.

LINDEX

Earliest available version: 1.0.0

Time complexity: O(N), where N is the number of elements across to find the target element. When the target element is the first or last, the time complexity is O(1).

This command is used to return the element at a specified position in the list. Index starts at 0, -1 indicates the penultimate element, -2 indicates the penultimate element, and so on. An error is returned when the key is not a list. Returns nil when index is out of range.

LINSERT

Earliest available version: 2.2.0

Time complexity: O(N), where N is the number of elements crossed before the benchmark value is found. That is, O(1) if it goes to the head, O(N) if it goes to the tail.

Usage:

LINSERT key BEFORE|AFTER pivot value
Copy the code

This command inserts the value before or after the base value pivot, and if the key does not exist, the list is treated as an empty list and nothing happens. If the key stores something other than a list, an error is reported. The return value of this command is the length of the list after the insertion, or -1 if no reference pivot can be found.

LLEN

Earliest available version: 1.0.0

Time complexity: O(1)

Returns the length of the list with the specified key, or 0 if the key does not exist. If the key stores something other than a list, an error is reported.

LPOP

Earliest available version: 1.0.0

Time complexity: O(1)

This command is used to delete and return the first element of a list. Returns nil if key does not exist.

LPUSH

Earliest available version: 1.0.0

Time complexity: O(1)

Inserts all specified values into the head of the list. If the key does not exist, an empty list is created and inserted. If the key does not store a list, an error is returned. We can insert multiple elements at once, and they are inserted into the list from left to right, so,

LPUSH mylist a b c
Copy the code

C is the first element and a is the third element. The return value from this command is the length of the list after the insert operation. One thing to note: prior to Redis2.4 (not including 2.4), inserting multiple elements at once was not supported.

LPUSHX

Earliest available version: 2.2.0

Time complexity: O(1)

Insert the specified element in the header if the key exists. Insert the element in the header if the key does not exist.

LRANGE

Earliest available version: 1.0.0

Time complexity: O(S+N), where S is the offset of the start element and N is the number of elements in the specified range

Usage:

LRANGE key start stop
Copy the code

Return the specified range of elements with the specified key. Start and stop are subscripts (starting at 0). Again, the subscripts can be negative, -1 for the penultimate and -2 for the penultimate. The command returns results that contain elements with the subscript stop. If start returns beyond the length of the list, an empty list is returned, and if stop returns beyond the length of the list, the last element is returned.

LREM

Earliest available version: 1.0.0

Time complexity: O(1)

Usage:

LREM key count value
Copy the code

Remove the last count value from the list

  • If count>0: Matches value from beginning to end
  • If count=0, remove all elements matching value
  • If count<0, match value from tail to header

When the key does not exist, it is treated as an empty list and returns 0.

LSET

Earliest available version: 1.0.0

Time complexity: O(N), where N is the length of the list

Sets the value of the specified subscript, or returns an error if the subscript is out of range.

LTRIM

Earliest available version: 1.0.0

Time complexity: O(N), N The number of deleted elements

This command is used to trim an existing list that contains only a specified range of elements. Start and stop are indexes that start at 0, for example,

LTRIM foobar 0 2
Copy the code

Keep only the first three elements of the foobar. Start and stop can also be negative, -1 for the penultimate element, -2 for the penultimate element, and so on. If the index is out of range, an error is not reported. Instead, the result is an empty list if start is greater than the index of the last element of the list, or start>end. If end is greater than the maximum index, Redis treats it as the last element.

RPOP

Earliest available version: 1.0.0

Time complexity: O(1)

Removes and returns the last element of the list. Returns nil if key does not exist.

RPOPLPUSH

Earliest available version: 1.2.0

Time complexity: O(1)

Atomic removes the last element of the source and stores it at the location of the first element of the destination. For example, source stores elements A, B, and c, and Destination stores elements X, y, and z

RPOPLPUSH source destination
Copy the code

After that, source stores a, B and destination stores C, x, y, and z. The return value of this command is the element removed from the source and stored to destination.

RPUSH

Earliest available version: 1.0.0

Time complexity: O(1)

Inserts the specified element at the end of the specified key. If the key does not exist, an empty list is created. If the key holds something other than a list, an error is returned. After version 2.4, you can use a single command to insert multiple values at once, from left to right.

RPUSHX

Earliest available version: 2.2.0

Time complexity: O(1)

The only difference with RPUSH is that if the key does not exist, nothing is done.