Redis list learning notes and summary

Time:2020-2-8

Redis version

Copy codeThe code is as follows:
[[email protected] ~]# redis-server –version
Redis server v=2.8.19 sha=00000000:0 malloc=jemalloc-3.6.0 bits=32 build=e2559761bd460ca0

List is a linked list structure. Its main functions are to push (like PHP’s array_push() method), pop (like PHP’s array_pop() method), get all the values of a range, and so on

Key is understood as the name of the linked list. Redis’s list type is actually a two-way linked list with each child element of string type.

The maximum length of a linked list is (32 power of 2). We can add and delete elements from the head or tail of the list through the push and pop operations. This allows the list to be used as both a stack and a queue.

Interestingly, there is a blocking version of the list pop operation. When we [LR] pop a list object, if the list is empty or does not exist, nil will be returned immediately. But the blocking version of B [LR] pop can be blocked, of course, the timeout can be added, and nil will be returned after the timeout. Why to block the version of pop? It is mainly to avoid polling. For a simple example, if we use list to implement a work queue. The thread executing the task can call the blocking version of pop to get the task, which can avoid polling to check whether there is a task. When the task comes, the worker thread can return immediately and avoid the delay caused by polling.

① Lpush method (stack)

Add (press in) string elements to the header of the list corresponding to the key.

② Lrange method

Lrange LIST1 0 – 1 represents the first element taken from the first element in the head of LIST1 to the first element in the tail (- 1 represents the first element in the tail).

[example]

Copy codeThe code is as follows:
127.0.0.1:6379> lpush list1 hello
(integer) 1

127.0.0.1:6379> lpush list1 world
(integer) 2

127.0.0.1:6379> lrange list1 0 -1
1) “world”
2) “hello”

③ Rpush method (queue)

Add (press in) string elements at the end of the list corresponding to the key.

[example]

Copy codeThe code is as follows:
127.0.0.1:6379> rpush list2 hello
(integer) 1

127.0.0.1:6379> rpush list2 world
(integer) 2

127.0.0.1:6379> lrange list2 0 -1
1) “hello”
2) “world”

Both lpush and rpush methods are pushed in from both ends of the list.

④ Linsert method

Add a string before or after the specific location of the key corresponding to the list.

[example]

Copy codeThe code is as follows:
127.0.0.1:6379> rpush list3 one
(integer) 1

127.0.0.1:6379> linsert list3 before one two
(integer) 2

127.0.0.1:6379> lrange list3 0 -1
1) “two”
2) “one”

127.0.0.1:6379> linsert list3 before one three
(integer) 3

127.0.0.1:6379> lrange list3 0 -1
1) “two”
2) “three”
3) “one”

Be careful:

|Scrotum head (front)

|   |  ↑

|Scrofula tail

⑤ Lset method

Set the element value of the specified subscript in the list (replace the element of the specified subscript, similar to the array in PHP: $arr = array (‘a ‘,’b’,’c ‘); $arr [0] =’d’;).

[example]

Copy codeThe code is as follows:
127.0.0.1:6379> lrange list3 0 -1
1) “two”
2) “three”
3) “one”

127.0.0.1:6379> lset list3 1 tmp
OK

127.0.0.1:6379> lrange list3 0 -1
1) “two”
2) “tmp”
3) “one”

127.0.0.1:6379> lset list3 0 reset
OK

127.0.0.1:6379> lrange list3 0 -1
1) “reset”
2) “tmp”
3) “one”

⑥ Lrem method

Remove n elements with the same value from the key corresponding list. (n < 0 deleted from tail, n = 0 deleted all). Returns the number of deletions.

[example]

Copy codeThe code is as follows:
127.0.0.1:6379> lrange list3 0 -1
1) “reset”
2) “tmp”
3) “one”

127.0.0.1:6379> lpush list3 two
(integer) 4
127.0.0.1:6379> lpush list3 two
(integer) 5
127.0.0.1:6379> lpush list3 two
(integer) 6

127.0.0.1:6379> lrange list3 0 -1
1) “two”
2) “two”
3) “two”
4) “reset”
5) “tmp”
6) “one”

127.0.0.1:6379> lrem list3 1 two
(integer) 1

127.0.0.1:6379> lrange list3 0 -1
1) “two”
2) “two”
3) “reset”
4) “tmp”
5) “one”

127.0.0.1:6379> rpush list3 two
(integer) 6

127.0.0.1:6379> lrange list3 0 -1
1) “two”
2) “two”
3) “reset”
4) “tmp”
5) “one”
6) “two”

127.0.0.1:6379> lrem list3 -2 two
(integer) 2

127.0.0.1:6379> lrange list3 0 -1
1) “two”
2) “reset”
3) “tmp”
4) “one”

[example 2]

Copy codeThe code is as follows:
127.0.0.1:6379> lrange list3 0 -1
1) “two”
2) “reset”
3) “tmp”
4) “one”

127.0.0.1:6379> lrem list3 -2 two
(integer) 1

127.0.0.1:6379> lrange list3 0 -1
1) “reset”
2) “tmp”
3) “one”

127.0.0.1:6379> lrem list3 -2 two
(integer) 0

127.0.0.1:6379> lrange list3 0 -1
1) “reset”
2) “tmp”
3) “one”

⑦ Ltrm method

Keep the data within the value range of the specified key.

[example]

Copy codeThe code is as follows:
127.0.0.1:6379> lpush list4 one
(integer) 1
127.0.0.1:6379> lpush list4 two
(integer) 2
127.0.0.1:6379> lpush list4 three
(integer) 3
127.0.0.1:6379> lpush list4 four
(integer) 4

127.0.0.1:6379> lrange list4 0 -1
1) “four”
2) “three”
3) “two”
4) “one”

127.0.0.1:6379> ltrim list4 1 2
OK

127.0.0.1:6379> lrange list4 0 -1
1) “three”
2) “two”

Note: keep the elements with the subscript (key) from 1 to 2, and delete all other elements at both ends.

[example 2]

Copy codeThe code is as follows:
127.0.0.1:6379> lrange list4 0 -1
1) “seven”
2) “six”
3) “five”
4) “three”
5) “two”

127.0.0.1:6379> ltrim list4 2 -1
OK

127.0.0.1:6379> lrange list4 0 -1
1) “five”
2) “three”
3) “two”

⑧ Lpop method

Delete the element from the head of the list and return the deleted element (similar to the array? Pop () method in PHP: Pop (delete) the last cell of the array).

(rpop: for removing elements from the tail)

[example]

Copy codeThe code is as follows:
127.0.0.1:6379> lrange list4 0 -1
1) “five”
2) “three”
3) “two”

127.0.0.1:6379> lpop list4
“five”

127.0.0.1:6379> lrange list4 0 -1
1) “three”
2) “two”

127.0.0.1:6379> rpop list4
“two”

127.0.0.1:6379> lrange list4 0 -1
1) “three”

⑨ Rpopplbush method

Remove the element from the end of the first list and add it to the head of the second list.

Copy codeThe code is as follows:
127.0.0.1:6379> lrange list4 0 -1
1) “eight”
2) “seven”
3) “three”

127.0.0.1:6379> lrange list5 0 -1
1) “redis”
2) “nginx”
3) “mysql”
4) “php”

127.0.0.1:6379> rpoplpush list4 list5
“three”

127.0.0.1:6379> lrange list4 0 -1
1) “eight”
2) “seven”

127.0.0.1:6379> lrange list5 0 -1
1) “three”
2) “redis”
3) “nginx”
4) “mysql”
5) “php”

⑩ Lindex method

Returns the element in the index position of the list named key.

[example]

Copy codeThe code is as follows:
127.0.0.1:6379> lrange list5 0 -1
1) “three”
2) “redis”
3) “nginx”
4) “mysql”
5) “php”

127.0.0.1:6379> lindex list5 1
“redis”

127.0.0.1:6379> lindex list5 -1
“php”

⑪ Len method (similar to count ($ARR) in PHP)

Returns the number of elements in the list list.

[example]

Copy codeThe code is as follows:
127.0.0.1:6379> lrange list5 0 -1
1) “three”
2) “redis”
3) “nginx”
4) “mysql”
5) “php”
 
127.0.0.1:6379> llen list5
(integer) 5