Manually tear down the list of five basic data types of Redis (List)

1. List

Redis list is a simple string list, sorted by insertion order. You can add an element to the head (left) or tail (right) of the list

A list can contain up to 2 ^ 32 - 1 elements (4294967295, with more than 4 billion elements per list). Collection members are * * not unique, * * which means that duplicate data can appear in the collection.

Its bottom layer is actually a two-way linked list, which has high performance on both ends. The performance of operating the middle node through index subscript will be poor.

2. Basic command

All List commands start with L

2.1 lpush command

The Lpush command inserts one or more values into the list header. If the key does not exist, an empty list is created and the Lpush command is executed. If the key exists but is not a list type, an error is returned.

Concrete grammar

lpush key value [value ...]

notes

  • The inserted value is in the header
  • If the key does not exist, an empty list is created and the Lpush command is executed. If the key exists but is not a list type, an error is returned.
  • lpush commands before redis version 2.4 only accept a single value value.

case

#Set the existing key at this time
127.0.0.1:6379> mset k1 v1 k2 itbestboy
OK
#View existing key s at this time
127.0.0.1:6379> keys *
1) "k1"
2) "k2"


#The inserted key does not exist
127.0.0.1:6379> lpush l1 v1 v2 v3
(integer) 3          #Insert successful


#The inserted key exists, but it is not a list type
   #1.0 viewing k1 types
127.0.0.1:6379> type k1
string
   #2.0 insertion
127.0.0.1:6379> lpush k1 v1 v2 v3
(error) WRONGTYPE Operation against a key holding the wrong kind of value   #Insert failed

#The inserted key exists and is of list type
127.0.0.1:6379> lrange l1 0 -1
1) "v3"
2) "v2"
3) "v1"
127.0.0.1:6379> lpush l1 v1 v2 v3 v4
(integer) 7
127.0.0.1:6379> lrange l1 0 -1
1) "v4"
2) "v3"
3) "v2"
4) "v1"
5) "v3"
6) "v2"
7) "v1"
#It can be seen that it is directly appended after insertion, and duplicate elements are allowed

2.2 lpushx command

The Lpushx command is used to insert one or more values into the header of an existing list. The operation is invalid when the list does not exist.

Note the distinction between the lpush command and the lpush command:

The lpush command can be inserted whether the list exists or not. The insertion fails only when the type of value corresponding to the existing key is not the list type. Otherwise, it will not fail. However, lpushx can only be inserted when the list exists, but not when the list does not exist==

Concrete grammar

lpushx key value

Note: the list is valid only if it already exists, otherwise it is invalid. And only one value can be inserted at a time.

case

#Inserts a value into a nonexistent list
127.0.0.1:6379> lpushx l2 v2
(integer) 0       #Insert failed


#Inserts a value into an existing list
127.0.0.1:6379> lpushx l1 v8
(integer) 11
127.0.0.1:6379> lrange l1 0 -1
 1) "v8"
 2) "v7"
 3) "v6"
 4) "v5"
 5) "v4"
 6) "v3"
 7) "v2"
 8) "v1"
 9) "v3"
10) "v2"
11) "v1"

2.3 lrange command

The Lrange command is used to obtain the elements within the specified interval in the list, where 0 represents the first element of the list, 1 represents the second element of the list, and so on. Negative subscripts can be used, with - 1 representing the last element of the list, - 2 representing the penultimate element of the list, and so on.

Concrete grammar

lrange key start stop

explain

  • key: the specified list name
  • Start: start position; cannot be omitted. 0 represents the first element, and so on
  • End: end position; cannot be omitted- 1 represents the last element, and so on

case

#Gets all the elements of the list l2 (which does not exist)
127.0.0.1:6379> lrange l2 0 -1
(empty list or set)


#Get all elements in list l1
127.0.0.1:6379> lrange l1 0 -1
 1) "v8"
 2) "v7"
 3) "v6"
 4) "v5"
 5) "v4"
 6) "v3"
 7) "v2"
 8) "v1"
 9) "v3"
10) "v2"
11) "v1"

#Gets the 1st to 4th elements in the list l1
127.0.0.1:6379> lrange l1 0 3
1) "v8"
2) "v7"
3) "v6"
4) "v5"

2.4. lindex command

The Lindex command is used to get the element at the specified position in the list by index. An error is reported when the given index value exceeds the index range

Concrete grammar

lindex key index

notes

  • key: if the specified list does not exist, nil will be reported
  • Index: the index value, generally starting from 0, represents the first element, or a negative number can be used, - 1 represents the last element, and so on.

case

#View the location of elements in the list l1
127.0.0.1:6379> lrange l1 0 -1
 1) "v8"
 2) "v7"
 3) "v6"
 4) "v5"
 5) "v4"
 6) "v3"
 7) "v2"
 8) "v1"
 9) "v3"
10) "v2"
11) "v1"

#When the list does not exist
127.0.0.1:6379> lindex l2 0
(nil)
127.0.0.1:6379> lindex l2 3
(nil)


#Gets the first element in the l1 list
127.0.0.1:6379> lindex l1 0
"v8"

#Gets the last element in the l1 list
127.0.0.1:6379> lindex l1 -1
"v1"
#Gets l1 the penultimate element in the list
127.0.0.1:6379> lindex l1 -3
"v3"

#Gets an element that exceeds the l1 subscript value
127.0.0.1:6379> lindex l1 11
(nil)

notes

lrange is used to get the elements of the specified range.

lindex is used to get the element at the specified location.

2.5. rpush command

The rpush command inserts one or more values at the end of the list. If the key does not exist, an empty list is created and the rpush command is executed. If the key exists but is not a list type, an error is returned.

Concrete grammar

rpush key value [value ...]

notes

  • The inserted value is at the end of the table
  • If the key does not exist, an empty list is created and the rpush command is executed. If the key exists but is not a list type, an error is returned.
  • rpush commands before redis version 2.4 only accept a single value value.

case

#The inserted key does not exist
127.0.0.1:6379> rpush l2 v1 v2 v3
(integer) 3            #Insert successful
127.0.0.1:6379> lrange l2 0 -1
1) "v1"
2) "v2"
3) "v3"     

#The inserted key exists, but it is not a list type
   #1.0 viewing k1 types
127.0.0.1:6379> type k1
string
   #2.0 insertion
127.0.0.1:6379> rpush k1 v2 v3 v4 v5 v6
(error) WRONGTYPE Operation against a key holding the wrong kind of value  #Insert failed

#The inserted key exists and is of list type
127.0.0.1:6379> rpush l2 v2 v3 v4 v5 v6
(integer) 8             #Insert successful
127.0.0.1:6379> lrange l2 0 -1
1) "v1"
2) "v2"
3) "v3"
4) "v2"
5) "v3"
6) "v4"
7) "v5"
8) "v6"
#It can be seen that it is directly appended after insertion, and duplicate elements are allowed

2.6 rpushx command

The rpushx command is used to insert one or more values at the end of an existing list. The operation is invalid when the list does not exist.

Note the distinction between the rpush command and the rpush command:

1. The rpush command can be inserted whether the list exists or not. The insertion fails only when the type of value corresponding to the existing key is not the list type, otherwise it will not fail. However, rpushx can only be inserted when the list exists, but not when the list does not exist

2. The rpush command can insert multiple values at a time, while rpushx can only insert one value at a time

==

Concrete grammar

rpushx key value

Note: the list is valid only if it already exists, otherwise it is invalid. You can only insert one value at a time. If you need to insert multiple values, you can use {}

case

#Inserts a value into a nonexistent list
127.0.0.1:6379> rpushx l3 v2
(integer) 0       #Insert failed


#Inserts a value into an existing list
127.0.0.1:6379> rpushx l2 v7
(integer) 9
127.0.0.1:6379> lrange l2 0 -1
1) "v1"
2) "v2"
3) "v3"
4) "v2"
5) "v3"
6) "v4"
7) "v5"
8) "v6"
9) "v7

2.7. linsert command

The linsert command inserts an element before or after an element that already exists in the list. Equivalent to inserting

  1. When the list does not exist, it is regarded as an empty list and no operation is performed.
  2. When the specified element does not exist in the list, no action is taken.
  3. If the key is not a list type, an error is returned.

Concrete grammar

linsert key BEFORE|AFTER pivot value

explain

  • key: if the specified element does not exist, no operation will be performed
  • BEFORE|AFTER: before and after the element corresponding to the specified key.
  • pivot: which position
  • Value: Specifies the value corresponding to the key
#Insert element in l3 list (list does not exist)
127.0.0.1:6379> linsert l3 before 2 l1
(integer) 0         #Failed to insert. No processing will be done

#Insert element in k2 (type ratio matching)
127.0.0.1:6379> linsert k2 before 2 l1
(error) WRONGTYPE Operation against a key holding the wrong kind of value

#Add an element after the corresponding element of v10 in the l2 list
127.0.0.1:6379> linsert l2 after v10 v11
(integer) -1

#Add an element after the v3 corresponding element in the l2 list
127.0.0.1:6379> linsert l2 after v3 v10
(integer) 11
127.0.0.1:6379> lrange l2 0 -1
 1) "v1"
 2) "v2"
 3) "v3"
 4) "v10"
 5) "v2"
 6) "v3"
 7) "v4"
 8) "v5"
 9) "v6"
10) "v7"
11) "{v7,v8}"
#Add an element before the v3 corresponding element in the l2 list
127.0.0.1:6379> linsert l2 before v3 v10
(integer) 12
127.0.0.1:6379> lrange l2 0 -1
 1) "v1"
 2) "v2"
 3) "v10"
 4) "v3"
 5) "v10"
 6) "v2"
 7) "v3"
 8) "v4"
 9) "v5"
10) "v6"
11) "v7"
12) "{v7,v8}"

2.8 lset command

Lset sets the value of an element through an index, which is equivalent to an update operation. An error is returned when the index parameter is out of range or lset an empty list.

Concrete grammar

lset key index value

explain

  • key: the specified list. If it does not exist, an error is reported
  • index: subscript. Generally, it starts from 0 to represent the first element, or negative numbers can be used, - 1 to represent the last element, and so on
  • Value: added value

case

#The list does not exist
127.0.0.1:6379> lset l3 0 v1
(error) ERR no such key

#type mismatch
127.0.0.1:6379> lset k1 0 v1
(error) WRONGTYPE Operation against a key holding the wrong kind of value
#Index value out of range
127.0.0.1:6379> lrange l2 0 -1
 1) "v1"
 2) "v2"
 3) "v10"
 4) "v3"
 5) "v10"
 6) "v2"
 7) "v3"
 8) "v4"
 9) "v5"
10) "v6"
11) "v7"
12) "{v7,v8}"
127.0.0.1:6379> lset l2 12 v12
(error) ERR index out of range

#Change the last element in the l2 list to v11
127.0.0.1:6379> lset l2 11 v11   #Equivalent to lset l2 -1 v11
OK
127.0.0.1:6379> lrange l2 0 -1
 1) "v1"
 2) "v2"
 3) "v10"
 4) "v3"
 5) "v10"
 6) "v2"
 7) "v3"
 8) "v4"
 9) "v5"
10) "v6"
11) "v7"
12) "v11"
127.0.0.1:6379> 

2.9. llen command

The Llen command returns the length of the list. If the list key does not exist, 0 is returned. If the key is not a list type, an error is returned.

Concrete grammar

llen key

case

#Gets the length of a nonexistent list
127.0.0.1:6379> llen l3
(integer) 0

#Gets a length that is not a list type
127.0.0.1:6379> llen k1
(error) WRONGTYPE Operation against a key holding the wrong kind of value

#Gets the length of the l2 list
127.0.0.1:6379> llen l2
(integer) 12

2.10. ltrim command

The ltrim command is used to trim a list, that is, to make the list retain only the elements within the specified interval, and the elements not within the specified interval will be deleted.

Concrete grammar

ltrim key start stop

explain

  • key: specified list

  • Start: start position

  • stop: end position

    The subscript generally starts from 0 to indicate the first element, or negative numbers can be used, - 1 to indicate the last element, and so on

case

#View existing key s
127.0.0.1:6379> keys *
1) "l2"
2) "l1"
3) "k1"
4) "k2"
#The list does not exist
127.0.0.1:6379> ltrim l3 0 -1
OK


#Type does not match
127.0.0.1:6379> ltrim k1 0 -1
(error) WRONGTYPE Operation against a key holding the wrong kind of value

#Position out of bounds
127.0.0.1:6379> lrange l2 0 -1
 1) "v1"
 2) "v2"
 3) "v10"
 4) "v3"
 5) "v10"
 6) "v2"
 7) "v3"
 8) "v4"
 9) "v5"
10) "v6"
11) "v7"
12) "v12"
127.0.0.1:6379> ltrim l2 12 14
OK   
127.0.0.1:6379> lrange l2 0 -1
(empty list or set)    #The list becomes empty

#normal
127.0.0.1:6379> lpush l2 v1 v2 v3 v4
(integer) 4
127.0.0.1:6379> lrange l2 0 -1
1) "v4"
2) "v3"
3) "v2"
4) "v1"
127.0.0.1:6379> ltrim l2 0 2
OK
127.0.0.1:6379> lrange l2 0 -1
1) "v4"
2) "v3"
3) "v2"
127.0.0.1:6379> 

2.11 lrem command

The lrem command removes elements from a list. Remove the elements in the list equal to the parameter VALUE according to the VALUE of the parameter count

Concrete grammar

lrem key count value

notes

Lrem removes the elements in the list equal to the parameter VALUE according to the VALUE of the parameter count.

The value of count can be the following:

  • COUNT > 0: search from the header to the footer to remove elements equal to VALUE, and the quantity is COUNT.
  • COUNT < 0: search from the end of the table to the header, remove elements equal to VALUE, and the quantity is the absolute VALUE of COUNT.
  • count = 0: remove all values equal to VALUE in the table.

case

#The list does not exist
127.0.0.1:6379> lrem l3 1 v1
(integer) 0

#type mismatch
127.0.0.1:6379> lrem k1 1 v1
(error) WRONGTYPE Operation against a key holding the wrong kind of value

#View values in list l2
127.0.0.1:6379> lrange l2 0 -1
 1) "v5"
 2) "v5"
 3) "v5"
 4) "v5"
 5) "v4"
 6) "v3"
 7) "v2"
 8) "v2"
 9) "v4"
10) "v3"
11) "v2"

#Remove non-existent values
127.0.0.1:6379> lrem l2 2 v1
(integer) 0

#Remove 2 v2 from header to footer
127.0.0.1:6379> lrem l2 2 v2
(integer) 2
127.0.0.1:6379> lrange l2 0 -1
1) "v5"
2) "v5"
3) "v5"
4) "v5"
5) "v4"
6) "v3"
7) "v4"
8) "v3"
9) "v2"

#When one v2 remains, continue to remove two v2 without error
127.0.0.1:6379> lrem l2 2 v2
(integer) 1
127.0.0.1:6379> lrange l2 0 -1
1) "v5"
2) "v5"
3) "v5"
4) "v5"
5) "v4"
6) "v3"
7) "v4"
8) "v3"

#Remove 2 v3 s from footer to header
127.0.0.1:6379> lrem l2 -2 v3
(integer) 2
127.0.0.1:6379> lrange l2 0 -1
1) "v5"
2) "v5"
3) "v5"
4) "v5"
5) "v4"
6) "v4"

#Remove all v5
127.0.0.1:6379> lrem l2 0 v5
(integer) 4
127.0.0.1:6379> lrange l2 0 -1
1) "v4"
2) "v4"

2.12 lpop command

The Lpop command is used to delete the first element in the header and return the deleted element. When the list is empty, it returns nil.

Delete a single element. If you need to delete the elements in the whole list or delete the list, you need to delete recursively.

Concrete grammar

lpop key

case

#View existing key values
127.0.0.1:6379> keys *
1) "l2"
2) "l1"
#View value in l1
127.0.0.1:6379> lrange l1 0 -1
1) "v3"
2) "v2"
3) "v1"

#Delete nonexistent list
127.0.0.1:6379> lpop l3
(nil)
#When the element in the list is empty, continue to delete
127.0.0.1:6379> lpop l1
(nil)       #nil is returned and the corresponding list is deleted
#Normal deletion
127.0.0.1:6379> lpop l1
"v3"
127.0.0.1:6379> lpop l1
"v2"
127.0.0.1:6379> lpop l1
"v1"

2.13 rpop command

The rpop command is used to delete the first element in the footer and return the deleted element. When the list is empty, it returns nil.

Delete a single element. If you need to delete the elements in the whole list or delete the list, you need to delete recursively.

Concrete grammar

rpop key

case

#View value in l1
127.0.0.1:6379> lrange l1 0 -1
1) "v1"
2) "v2"
3) "v3"
4) "v4"
#Delete nonexistent list
127.0.0.1:6379> rpop l3
(nil)
#Normal deletion
127.0.0.1:6379> rpop l1
"v4"
127.0.0.1:6379> rpop l1
"v3"
127.0.0.1:6379> rpop l1
"v2"
127.0.0.1:6379> rpop l1
"v1"
#When the element in the list is empty, continue to delete
127.0.0.1:6379> rpop l1
(nil)            #nil is returned and the corresponding list is deleted

2.14. blpop command

The blpop command is used to delete the first element in the header and return the deleted element. If there is no element in the list, the list will be blocked until the waiting timeout or pop-up element is found

Delete a single element. If you need to delete all elements or delete the list, you need to delete recursively

Concrete grammar

blpop key [key ...] timeout

explain

  • key: if the specified element does not exist in the list, it will first enter the blocking queue and wait for timeout. After timeout, nil and time will be returned
  • timeout: supermarket time, unit: s

case

#Delete nonexistent list
127.0.0.1:6379> blpop l3 5
(nil)
(5.07s)
#Normal deletion
127.0.0.1:6379> blpop l1 6
1) "l1"
2) "v4"

127.0.0.1:6379> lrange l1 0 -1
1) "v3"
2) "v2"
3) "v1"
127.0.0.1:6379> blpop l1 6
1) "l1"
2) "v3"
127.0.0.1:6379> lrange l1 0 -1
1) "v2"
2) "v1"
127.0.0.1:6379> blpop l1 6
1) "l1"
2) "v2"
127.0.0.1:6379> blpop l1 6
1) "l1"
2) "v1"
#When the elements in the list are deleted, nil will be returned if you continue to delete them
127.0.0.1:6379> blpop l1 6
(nil)
(6.06s)

Note: if there is an element in the set supermarket time, it will be deleted normally. If there is no element, it will enter the blocking state. When an element is written in the supermarket time, it will be awakened again to delete the element.

2.15. brpop command

The brpop command is used to delete the first element in the footer and return the deleted element. If there are no elements in the list, the list will be blocked until the waiting timeout or pop-up elements are found

Delete a single element. If you need to delete all elements or delete the list, you need to delete recursively

Concrete grammar

brpop key [key ...] timeout

explain

  • key: if the specified element does not exist in the list, it will first enter the blocking queue and wait for timeout. After timeout, nil and time will be returned
  • timeout: supermarket time, unit: s

case

#Delete nonexistent list
127.0.0.1:6379> rlpop l3 5
(nil)
(5.07s)
#Normal deletion
127.0.0.1:6379> lrange l1 0 -1
1) "v1"
2) "v2"
3) "v3"
4) "v4"
127.0.0.1:6379> brpop l3 6
(nil)
(6.05s)
127.0.0.1:6379> brpop l1 6
1) "l1"
2) "v4"
127.0.0.1:6379> brpop l1 6
1) "l1"
2) "v3"
127.0.0.1:6379> brpop l1 6
1) "l1"
2) "v2"
127.0.0.1:6379> brpop l1 6
1) "l1"
2) "v1"

#When the elements in the list are deleted, nil will be returned if you continue to delete them
127.0.0.1:6379> rlpop l1 6
(nil)
(6.06s)

#At that time, if the element can be written within these 6s, it will be deleted normally, that is, it will be deleted normally within the set supermarket time. If there is no element, it will enter the blocking state. When the element is written within the supermarket time, it will be awakened again to delete the element.
127.0.0.1:6379> lpush l1 v5
(integer) 1
127.0.0.1:6379> brpop l1 6
1) "l1"
2) "v5"
(3.10s)

Note: if there is an element in the set supermarket time, it will be deleted normally. If there is no element, it will enter the blocking state. When an element is written in the supermarket time, it will be awakened again to delete the element.

2.16. brpoplpush command

The Brpoplpush command pops up a value from the list, inserts the popped element into another list and returns it; If the list has no elements, the list is blocked until the wait times out or a pop-up element is found. If no element is ejected within the specified time, a nil and waiting time are returned. Otherwise, the element is returned.

Concrete grammar

brpoplpush source destination timeout

explain

  • Source: source list
  • Destination: destination list
  • Timeout: timeout

case

127.0.0.1:6379> keys *
1) "l1"
2) "l2"
127.0.0.1:6379> lrange l1 0 -1
1) "v1"
2) "v2"
127.0.0.1:6379> lrange l2 0 -1
1) "v2"
2) "v1"
#normal
127.0.0.1:6379> brpoplpush l1 l2 3
"v2"
#It can be seen that there is only one element left in the l1 list and one element is added in the l2 list. This operation is equivalent to mv
127.0.0.1:6379> lrange l2 0 -1
1) "v2"
2) "v2"
3) "v1"
127.0.0.1:6379> lrange l1 0 -1
1) "v1"

#If the destination list does not exist, it is created
127.0.0.1:6379> brpoplpush l2 l3 3
"v1"
127.0.0.1:6379> keys *
1) "l3"
2) "l1"
3) "l2"
#If the source list does not exist, nil is returned
127.0.0.1:6379> brpoplpush l4 l3 3
(nil)
(3.07s)

The rpopplush command is similar to b rpoplpush, except that there is no timeout, and the rest are similar.

2.17. del command

The del command is used to delete the entire list. If the deleted list does not exist, you will be prompted.

Concrete grammar

 del key [key ...]

case

#View existing
127.0.0.1:6379> keys *
1) "l1"
2) "l2"

#Delete nonexistent list
127.0.0.1:6379> del l3
(integer) 0

#Delete existing list
127.0.0.1:6379> del l1 l2
(integer) 2
127.0.0.1:6379> keys *
(empty list or set)

Move your hands and pay attention. hey


Thank you very much.

Keywords: Database Redis list

Added by crwtrue on Fri, 19 Nov 2021 09:22:12 +0200