linsert
LINSERT
Apply for Redis Developer Course
|
Redis Regular Inspection / Technical Support
|
Redis Enterprise Server
|
|---|
Insert a value at a position relative to a pivot
The usage is linsert key BEFORE|AFTER pivot value.
pivot is the reference value. The new value is inserted before or after it.
Example
| Command> | lpush mylist value1 value3 |
| Result> | 1 |
| Command> | lrange mylist 0 -1 |
| Result> | 0) value2 1) value1 |
| Command> | linsert mylist after value3 value2 Insert value2 after value3 |
| Result> | 3 Returns the total number of elements after the insert |
| Command> | linsert mylist before value3 value4 Insert value4 before value3 |
| Result> | 4 |
| Command> | lrange mylist 0 -1 |
| Result> |
1) value4 2) value3 3) value2 4) value1 |
When the pivot value appears more than once
It is processed based on the first matching element.
Example
| Command> | lpush mylist value3 value3 |
| Result> | 2 |
| Command> | linsert mylist before value3 value4 |
| Result> | 3 |
| Command> | linsert mylist after value3 value2 |
| Result> | 4 |
| Command> | lrange mylist 0 -1 |
| Result> |
1) value4 2) value3 3) value2 4) value3 |
When the pivot value does not exist
Nothing is inserted. In this case -1 is returned.
Example
| Command> | linsert mylist after value5 value6 |
| Result> | -1 |
Insert a value at a given index
You can insert a value by specifying an index.
Indexes start at 0. For the last index you can specify -1 or the result of LLEN.
A reverse index can also be used.
If you know the index, it is faster than using a value.
When the value appears more than once, using an index is also more effective.
Example
| Command> | linsert mylist after 1 value7 by index |
| Result> | 5 |
| Command> | linsert mylist before -1 value8 by index |
| Result> | 6 |
| This feature is available on Enterprise Server. |
Syntax
LINSERT key BEFORE|AFTER [pivot value] | [index by index]
- Available since version 2.2.0.
- Time complexity is O(N).
- After the command runs, it returns the total number of elements in the list.
| Related commands | LINDEX, LSET, LTRIM |
| Clients for Java | Jedis, Lettuce, Redisson | Clients for C | Hiredis |
| << LSET | LINSERT | LREM >> |
|---|
Email
답글이 올라오면 이메일로 알려드리겠습니다.
