Redis version
Copy Code code as follows:
[Root@localhost ~]# Redis-server--version
Redis server v=2.8.19 sha=00000000:0 malloc=jemalloc-3.6.0 bits=32 build=e2559761bd460ca0
List is a list structure, the main function is push (similar to PHP Array_push () method), pop (similar to PHP Array_pop () method), get a range of all values, etc., operation
The key is understood as the name of the list. The list type of Redis is actually a two-way list of string types for each child element.
The maximum length of the list is (2 of 32). We can add deletion elements from the head or tail of a linked list by push, pop operation. This allows the list to be used either as a stack or as a queue.
Interestingly, the list's pop operation also has a blocking version, and when we [Lr]pop a list object, if the list is empty or does not exist, it returns nil immediately. However, blocked version of the B[lr]pop can be blocked, of course, can add timeout, after the timeout will return nil. Why do you want to block the version of the pop, mainly to avoid polling. As a simple example, if we use list to implement a task queue. Thread that performs a task can invoke a blocked version of the pop to get a task so that polling can be avoided to check for the existence of a task. When the task comes, the worker thread can return immediately, or it can avoid the delay caused by polling.
①lpush Method (Stack)
Adds (presses) A string element to the head of the key corresponding to the list.
②lrange method
Lrange List1 0-1 represents the first element from the head of the list List1 to the tail first element (-1 represents the first element of the tail).
Cases
Copy Code code 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 (push) The string element at the end of the key corresponding list.
Cases
Copy Code code 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 pressed from both ends of the list.
④linsert method
Adds a string before or after the key corresponds to a specific location in the list.
Cases
Copy Code code 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"
Attention:
| | Head (front)
| | ↑
| | Tail
⑤lset method
Sets the element value of the specified subscript in the list (the element that replaces the specified subscript, similar to an array in PHP: $arr = Array (' A ', ' B ', ' C '); $arr [0] = ' d '; )。
Cases
Copy Code code 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 and value elements from the key corresponding list. (N < 0 deleted from tail, n = 0 all removed). Returns the number of deletes.
Cases
Copy Code code 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 Code code 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
Preserves data within the value range of the specified key.
Cases
Copy Code code 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: Leave the subscript (key) at 1 beginning to the subscript 2 element, and all the other ends of the element are deleted.
"Example 2"
Copy Code code 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
Deletes the element from the head of the list and returns the deleted element (similar to the Array_pop () method in PHP: POPs the last cell of the array (delete)).
(Rpop: Represents the deletion of elements from the tail)
Cases
Copy Code code 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"
⑨rpoplpush method
Removes the element from the tail of the first list and adds it to the head of the second list.
Copy Code code 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 an element of the index position in the list named key.
Cases
Copy Code code 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"
⑪llen method (similar to count ($arr) in PHP)
Returns the number of elements in the list chain list.
Cases
Copy Code code 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