LINSERT

Syntax

  1. LINSERT key <BEFORE | AFTER> pivot element

Time complexity: O(N) where N is the number of elements to traverse before seeing the value pivot. This means that inserting somewhere on the left end on the list (head) can be considered O(1) and inserting somewhere on the right end (tail) is O(N).

Inserts element in the list stored at key either before or after the reference value pivot.

When key does not exist, it is considered an empty list and no operation is performed.

An error is returned when key exists but does not hold a list value.

Return

Integer reply: the list length after a successful insert operation, 0 if the key doesn’t exist, and -1 when the pivot wasn’t found.

Examples

  1. dragonfly> RPUSH mylist "Hello"
  2. (integer) 1
  3. dragonfly> RPUSH mylist "World"
  4. (integer) 2
  5. dragonfly> LINSERT mylist BEFORE "World" "There"
  6. (integer) 3
  7. dragonfly> LRANGE mylist 0 -1
  8. 1) "Hello"
  9. 2) "There"
  10. 3) "World"