Redis Linsert Command

Redis List

The Redis Linsert command is used to insert an element before or after an element in the list. When the specified element does not exist in the list, no operation is performed.

When the list does not exist, it is treated as an empty list, and no operation is performed.

If key is not a list type, an error is returned.

Syntax

The basic syntax of the Redis Linsert command is as follows:

LINSERT key BEFORE|AFTER pivot value

Insert the value value into the list key, before or after the value pivot.

Available Versions

>= 1.0.0

Return Value

If the command executes successfully, the length of the list after the insert operation is completed is returned. If the specified element is not found, -1 is returned. If key does not exist or is an empty list, 0 is returned.

Example

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) "World"
redis> 

Redis List

Other Extensions