Redis Practical Instruction (3) - List

Keywords: Redis network

Lpush: The command inserts one or more values into the header of the list. If key does not exist, an empty list is created and LPUSH is performed. When the key exists but is not a list type, an error is returned.
Lpushx: Inserts one or more values into the existing list header, and the operation is invalid if the list does not exist.
Note the similarities and differences between the two instructions:
The same points: 1. All the values are put at the head of the list (index is 0); 2. Multiple values can be inserted at the same time; 3. When the type of key does not match, errors will be returned; 4. The returned results are the length of the list after the operation.
The difference: When the key of the operation does not exist, lpush creates one, lpushx returns 0, and no list is created.

redis 127.0.0.1:6379> LPUSH list1 "foo"
(integer) 1
redis 127.0.0.1:6379> LPUSHX list1 "bar"
(integer) 2
redis 127.0.0.1:6379> LPUSHX list2 "bar"
(integer) 0
redis 127.0.0.1:6379> LRANGE list1 0 -1
1) "foo"
2) "bar"

Lset: Set the value of an element by index. An error is returned when the index parameter is out of range or when LSET is performed on an empty list.
What I'm talking about here is setting the value of an element through an index. I think it's more appropriate to modify it, because the lset here is not created, it's just used to modify the existing value.

127.0.0.1:6379> lrange mylist 0 -1
1) "kun"
2) "pang"
127.0.0.1:6379> lset mylist 2 aa
(error) ERR index out of range
127.0.0.1:6379> lset mylist 1 aa
OK
127.0.0.1:6379> lrange mylist 0 -1
1) "kun"
2) "aa"
127.0.0.1:6379> 

Lindex: Used to retrieve elements in a list through an index. You can also use negative subscripts to represent the last element of the list with - 1 and - 2 to represent the penultimate element of the list, and so on.

127.0.0.1:6379> lindex mylist 2
(nil)
127.0.0.1:6379> lindex mylist 1
"aa"
127.0.0.1:6379> 

Linsert: Commands are used to insert elements before or after elements in a list. When the specified element does not exist in the list, no action is performed. When the list does not exist, it is regarded as an empty list and does not perform any operations. If key is not a list type, an error is returned.

127.0.0.1:6379> linsert mylist before kun pang
(integer) 3
127.0.0.1:6379> linsert mylist after kun k2
(integer) 4
127.0.0.1:6379> linsert mylist after kun1 k2
(integer) -1
127.0.0.1:6379> lrange mylist 0 -1
1) "pang"
2) "kun"
3) "k2"
4) "aa"
127.0.0.1:6379> 

To use before in an instruction is to insert before the specified element, and to use after is to insert after.
There is one point that is not explained here. If there are two identical values in a list, which value is the standard for insertion. Here I test the following:

127.0.0.1:6379> lpush mylist k2
(integer) 5
127.0.0.1:6379> lrange mylist 0 -1
1) "k2"
2) "pang"
3) "kun"
4) "k2"
5) "aa"
127.0.0.1:6379> linsert mylist after k2 af
(integer) 6
127.0.0.1:6379> lrange mylist 0 -1
1) "k2"
2) "af"
3) "pang"
4) "kun"
5) "k2"
6) "aa"
127.0.0.1:6379> linsert mylist after k2 af
(integer) 7
127.0.0.1:6379> lrange mylist 0 -1
1) "k2"
2) "af"
3) "af"
4) "pang"
5) "kun"
6) "k2"
7) "aa"
127.0.0.1:6379> linsert mylist before k2 af
(integer) 8
127.0.0.1:6379> lrange mylist 0 -1
1) "af"
2) "k2"
3) "af"
4) "af"
5) "pang"
6) "kun"
7) "k2"
8) "aa"
127.0.0.1:6379> 

The results here show that if there are two identical values, they will be executed according to the first value standard, and many times the same, you can try.

Llen: Returns the length of the list. If the list key does not exist, the key is interpreted as an empty list and returns 0. If key is not a list type, an error is returned.
Lpop: Remove and return the first element of the list.
Lrange: Returns the elements in the specified interval in the list, which is specified by offset START and END. Where 0 represents the first element of the list, 1 represents the second element of the list, and so on. You can also use negative subscripts to represent the last element of the list with - 1 and - 2 to represent the penultimate element of the list, and so on. (This command is always in use)

Lrem: Remove elements in the list that are equal to the parameter VALUE based on the value of the parameter COUNT.
COUNT values can be as follows:

  • Count > 0: Search from the header to the tail of the table to remove elements equal to VALUE in COUNT.
  • Count < 0: Search the header from the end of the table, removing elements equal to VALUE, the absolute number of COUNT.
  • count = 0: Remove all values in the table that are equal to VALUE.

It's more troublesome here. It's better to understand it through practical operation.

127.0.0.1:6379> lrange mylist 0 -1
1) "b"
2) "k2"
3) "pang"
4) "k2"
5) "k2"
6) "kun"
7) "k2"
8) "aa"
127.0.0.1:6379> lrem mylist 1 af
(integer) 0
127.0.0.1:6379> lrem mylist 1 k2
(integer) 1
127.0.0.1:6379> lrange mylist 0 -1
1) "b"
2) "pang"
3) "k2"
4) "k2"
5) "kun"
6) "k2"
7) "aa"
127.0.0.1:6379> lpush mylist k2 k2 k2 k2
(integer) 11
127.0.0.1:6379> lrange mylist 0 -1
 1) "k2"
 2) "k2"
 3) "k2"
 4) "k2"
 5) "b"
 6) "pang"
 7) "k2"
 8) "k2"
 9) "kun"
10) "k2"
11) "aa"
127.0.0.1:6379> lrem mylist  3 k2
(integer) 3
127.0.0.1:6379> lrange mylist 0 -1
1) "k2"
2) "b"
3) "pang"
4) "k2"
5) "k2"
6) "kun"
7) "k2"
8) "aa"
127.0.0.1:6379>

Ltrim: Trim a list, that is, keep only the elements in the specified interval in the list, and delete the elements that are not in the specified interval.
Subscript 0 represents the first element of the list, and 1 represents the second element of the list, and so on. You can also use negative subscripts to represent the last element of the list with - 1 and - 2 to represent the penultimate element of the list, and so on.

127.0.0.1:6379> lrange mylist 0 -1
1) "k2"
2) "b"
3) "pang"
4) "k2"
5) "k2"
6) "kun"
7) "k2"
8) "aa"
127.0.0.1:6379> ltrim mylist 2 -1
OK
127.0.0.1:6379> lrange mylist 0 -1
1) "pang"
2) "k2"
3) "k2"
4) "kun"
5) "k2"
6) "aa"
127.0.0.1:6379> 

Blpop: Remove and retrieve the first element of the list. If there are no elements in the list, the list will be blocked until waiting for a timeout or discovering a pop-up element.
If the list is empty, return a nil. Otherwise, return a list of two elements, the first being the key to which the pop-up element belongs, and the second being the value of the pop-up element.

127.0.0.1:6379> lrange mylist 0 -1
1) "pang"
2) "k2"
3) "k2"
4) "kun"
5) "k2"
6) "aa"
127.0.0.1:6379> blpop myl 10
(nil)
(10.01s)(No value was found to be removed. Actually, it waited.10.01Second)
127.0.0.1:6379> blpop mylist 5
1) "mylist"(List name: key)
2) "pang"(Removedvalue)
127.0.0.1:6379> lrange mylist 0 -1
1) "k2"
2) "k2"
3) "kun"
4) "k2"
5) "aa"
127.0.0.1:6379> 

Brpop: Remove and retrieve the last element of the list. If there are no elements in the list, the list will be blocked until a timeout or a pop-up element is found.
It is worth noting that both Blpop and Brpop can operate on multiple lists at the same time. First, the value of the first list is removed. Only when the first list is empty, can the next list be operated on once.

(This example is blpop )
127.0.0.1:6379> lpush list1 value1 valuea valueb
(integer) 3
127.0.0.1:6379> lpush list2 value2 valuea valueb
(integer) 3
127.0.0.1:6379> blpop list1 list2 value1 5
1) "list1"
2) "valueb"
127.0.0.1:6379> blpop list1 list2 valuea 5
1) "list1"
2) "valuea"
127.0.0.1:6379> lrange list2 0 -1
1) "valueb"
2) "valuea"
3) "value2"
127.0.0.1:6379> blpop list1 list2 value1 5
1) "list1"
2) "value1"
127.0.0.1:6379> lrange list1 0 -1
(empty list or set)
127.0.0.1:6379> lrange list2 0 -1
1) "valueb"
2) "valuea"
3) "value2"
127.0.0.1:6379> blpop list1 list2 value1 5
1) "list2"
2) "valueb"
127.0.0.1:6379> 

Brpoplpush: A value pops up from the list, inserts the pop-up element into another list and returns it; if there are no elements in the list, the list will block until waiting for a timeout or the pop-up element is found.
The value in the first list is removed and the last one is operated on.

127.0.0.1:6379> lrange list1 0 -1
1) "valueb"
2) "valuea"
127.0.0.1:6379> lrange list2 0 -1
1) "valuea"
2) "value2"
127.0.0.1:6379> brpoplpush list1 list2 5
"valuea"
127.0.0.1:6379> lrange list1 0 -1
1) "valueb"

Rpop: Remove and return the last element of the list.
Rpoplpush: Remove the last element of the list and add it to another list and return.
Rpush: Insert one or more values at the end of the list (rightmost).
If the list does not exist, an empty list is created and RPUSH is performed. When a list exists but is not a list type, an error is returned.
Rpushx: Insert one or more values at the end of an existing list (right-most). If the list does not exist, the operation is invalid.

The four instructions Rpush, Rpushx, Lpush and Lpushx can be compared and understood.

Redis Chinese Network Directive: http://www.redis.net.cn/order/3584.html

Posted by satal keto on Sun, 30 Jun 2019 12:18:37 -0700