In Redis, lists can be cleverly used as stacks, queues, blocking queues, etc.

Push insert element

1. Lpush, insert in the header

Inserts one or more values into the head of the list.

lpush list one
lpush list two
lpush list three
Copy the code

With lpush, the L here can be viewed as left, which is to insert to the left, so the list now looks like [three, two, one].

2. Rpush, insert at the tail

Next, use Rpush to insert elements to the right, at the end of the list.

rpush list right1 right2
Copy the code

At this point, the list looks like this [three, two, one, right1, right2]. Lrange list 0-1

Range Retrieves elements from the range

Think of it like range() in Python, which gets elements by passing in starting and ending subscripts.

lrange list 1 4
Copy the code

Pop removes elements

Since the above can be left or right to add operations, then remove naturally also have left or right.

The remove operation returns the removed element.

1. Lpop left side removed

lpop list
Copy the code

2. Rpop right removed

rpop list
Copy the code

4. Lindex retrieves elements by subscript

lindex list 1
Copy the code

Llen obtain the length of the list

Returns the length of the list.

llen list
Copy the code

6. Lrem removes the specified element

You can specify the elements to be removed, and specify the number.

lrem list 2 yi222
Copy the code

Now there are 3 yi222 in the list, and I need to remove 2.

7. Ltrim

Ltrim changes the truncated list by retaining only the specified portion and removing the rest.

ltrim list 1 4
Copy the code

I’m going to keep everything from 1 to 4, and I’m going to get rid of everything else.

Rpoplpush remove and add

This is the composition command that removes the last element of a list and adds it to another list.

rpoplpush list list2
Copy the code

herelistIs the original list,list2Is the target list, the target list does not exist to create.

9. Exists Determine whether the key exists

exists list
Copy the code

Returns 1 if it exists and 0 if it doesn’t.

Lset sets the value of the specified subscript

lset list 1 test
Copy the code

An error is returned when an index parameter is out of range, or when LSET is performed on an empty list.

Linsert, insert values before/after the specified position

before

linsert list before test before_test
Copy the code

This is before the test element, inserting the element before_test.

after

linsert list after test after_test
Copy the code

After the test element, after_test is inserted.

Twelve, summary

From the above list operation, you can observe some of its characteristics:

  • It’s actually a linked list, and the left and right sides of the nodes are easy to insert.
  • If the key does not exist, a new linked list is created.
  • If the key exists, the new element is added.
  • If you remove all the values, it’s an empty list, which means it doesn’t exist.
  • It is most efficient to insert or alter values on both sides. The middle element operates relatively inefficiently.

List can be flexibly used in practice, such as queue, stack, can be implemented with list.