Tip:

  • Clear: Clears a record
  • Tab: Partial prompt
  • Up and down arrows to view history

List

The list type is essentially a bidirectional list. Add and remove elements from the top or bottom of the list via push and pop operations. This allows lists to be used as both stacks and queues. Upper up and out: stack, features: data advanced after out

1.1. The List type

Introduction of 1.1.1.

  • A Redis list is a simple list of strings, sorted by insertion order. You can add one element to the head (left) or bottom (right) of a list. A list can contain up to 232-1 elements (4294967295, over 4 billion elements per list).
  • Similar to LinkedList in JAVA

1.1.2. The command

Assignment syntax:

  • LPUSH key value1 [value2] // Insert one or more values to the head of the list (added from the left)

    Note: 0 indicates that the subscript is 0 and -1 indicates the last element

  • RPUSH key value1 [value2] // Add one or more values to the list (add from the right)

  • LPUSHX key Value // Inserts a value into an existing list header. If the list is not there, the operation is invalid
  • RPUSHX key Value // A value is inserted at the end of an existing list (rightmost). If the list is not there, the operation is invalid.

Value syntax:

  • LLEN key // Get the list length

  • LINDEX key index // Get the elements of the list by the index

    Note: -1 represents the last element, -2 represents the penultimate, and so on

  • LRANGE key start stop // Get the elements in the specified range of the list

Description: Returns an element in a list within a specified interval, specified with offsets START and END. Where 0 represents the first element of the list, 1 represents the second element of the list, and so on. Negative subscripts can also be used, with -1 representing the last element of the list, -2 representing the penultimate element of the list, and so on. Start: page size x (number of pages -1) Stop: (page size x number of pages)-1

Delete syntax:

  • LPOP Key removes and retrieves the first element of the list (removed from the left)

  • RPOP Key removes the last element in the list and returns the removed element (removed from the right)

  • BLPOP key1 [KEY2] timeout Removes and retrieves the first element of the list. If there are no elements in the list, the list is blocked until the wait times out or a popup element is found.
  • Instance redis 127.0.0.1:6379> BLPOP list1 100 In the above instance, the operation will block and return the first element if there is data in the specified list key list1, otherwise it will return nil after 100 seconds. BRPOP key1 [KEY2] timeout Removes and retrieves the last element of the list. If there are no elements in the list, the list is blocked until the wait times out or a popup element is found.
  • LTRIM Key start stop To trim a list, that is, to keep only the elements within a specified range. All elements that are not within a specified range are deleted.

Modify the syntax:

  • LSET Key Index Value Sets the value of a list element by index

  • LINSERT key BEFORE | AFTER world value in a list of elements BEFORE or AFTER the inserted element

    Description: Inserts the value value into the list key before or after the value world.

Advanced syntax:

  • RPOPLPUSH source destination removes the last element of the list, adds it to another list and returns it

Rpoplpoplpush the last element of L1 l2 //a1 to the left of A2



RPOPLPUSH l2 l2 // Loop through the list and move the last element to the left

  • BRPOPLPUSH source Destination Timeout Pops a value from the list, inserts the pop-up element into another list and returns it. If the list has no elements, it blocks until the wait times out or a popup element is found.