List is a linked list structure, the main function is push, pop, get all the values of a range and so on, the operation of key is understood as the name of the list.
Series Articles:
Redis Detailed: Strings data types and operations
Redis Detailed: Hashes data types and operations
The Redis list type is actually a doubly linked list in which each child element is a string type. The maximum length of a linked list is (2 of 32). We can add delete elements from the head or tail of the list by Push,pop operation. This allows the list to be used as either a stack or as a queue.
The interesting thing is that the pop operation of list is also blocked version, when we [Lr]pop a list object, if the list is empty, or does not exist, it will return nil immediately. However, the blocked version of B[lr]pop can be blocked, of course, can be added to the timeout period, after the timeout will also return nil. Why block versions of Pop, primarily to avoid polling. As a simple example, if we use list to implement a Task Force column. The thread that performs the task can invoke the blocked version of the pop to get the task so that the polling can be avoided to check for the presence of a task. The worker thread can return immediately when the task comes, or it can avoid the delay caused by polling. Having said so much, let's look at the practical way to do it:
1, Lpush
Add a string element to the head of the key corresponding to the list:
Redis 127.0.0.1:6379> lpush Mylist "World" (integer) 1 Redis 127.0.0.1:6379> Lpush mylist "Hello" (integer) 2 redis 127.0.0.1:6379> lrange mylist 0 -1 1) Span class= "apple-converted-space" > "Hello" 2) "World" Redis 127.0.0.1:6379>
Here we insert a world first and then insert a hello in the world's head. Where Lrange is used to fetch mylist content.
2, Rpush
Add a string element at the end of the key corresponding list:
Redis127.0.0.1:6379> rpush mylist2 "Hello" (integer) 1 redis 127.0.0.1:6379> rpush mylist2 "World" (integer) 2 redis 127.0.0.1:6379> lrange mylist2 0 -1 1) "Hello" 2) "World" red is 127.0.0.1:6379>
Here we first insert a Hello, and then insert a world at the end of Hello.
3, Linsert
Add a string element before or after the key corresponds to a specific position in the list:
Redis127.0.0.1:6379>Rpush Mylist3"Hello" (integer)1 Redis127.0.0.1:6379> rpush mylist3 "World" ( Integer) 2 redis 127.0.0.1:6379> linsert mylist3 before "World" "There" (integer) Span class= "Apple-converted-space" > 3 redis 127.0.0.1:6379>< Span class= "Apple-converted-space" > lrange mylist3 0 -1 1) " Hello "2) " there "3) " World "Redis 127.0.0.1:6379>
Here we first insert a Hello, then insert a world at the end of Hello, and then insert the there in front of world.
4, LSet
Sets the element value for the specified subscript in the list (subscript starting from 0):
Redis127.0.0.1:6379>Rpush Mylist4"One" (integer)1 Redis127.0.0.1:6379>Rpush Mylist4"Both" (integer)2 Redis127.0.0.1:6379>Rpush Mylist4"Three" (integer)3 Redis 127.0.0.1:6379> lset Mylist4 0 "four" OK redis 127.0.0.1:6379> lset mylist4< Span class= "Apple-converted-space" > -2 "five" OK Redis 127.0.0.1:6379> lrange Mylist4 0 -1 1) "four" 2) "five" 3) "three" Redis 127.0.0.1:6379 >
Here we insert the One,two,three in turn, set the value of 0 to four, and then set the value of the subscript-2 to five.
5, Lrem
Removes the same count and value elements from the key corresponding list.
Count>0, delete them in order from beginning to end, as follows:
Redis127.0.0.1:6379>Rpush MYLIST5"Hello" (integer)1 Redis127.0.0.1:6379>Rpush MYLIST5"Hello" (integer)2 Redis127.0.0.1:6379>Rpush MYLIST5"foo" (integer)3 Redis 127.0.0.1:6379> rpush Mylist5 "Hello" (integer) 4 Redis 127.0.0.1:6379> Lrem mylist5 2 "Hello" ( Integer) 2 redis 127.0.0.1:6379> lrange mylist5 0 -1 1) "foo" 2) "Hello" Redis 127.0.0.1:6379>
Count<0, delete from the end of the head, as follows:
Redis127.0.0.1:6379>Rpush Mylist6"Hello" (integer)1 Redis127.0.0.1:6379>Rpush Mylist6"Hello" (integer)2 Redis127.0.0.1:6379>Rpush Mylist6"foo" (integer)3 Redis 127.0.0.1:6379> rpush Mylist6 "Hello" (integer) 4 Redis 127.0.0.1:6379> Lrem mylist6 -2 "Hello" ( Integer) 2 redis 127.0.0.1:6379> lrange mylist6 0 -1 1) "Hello" 2) "foo" Redis 127.0.0.1:6379>
Count=0, delete all, specifically as follows:
Redis127.0.0.1:6379>Rpush Mylist7"Hello" (integer)1 Redis127.0.0.1:6379>Rpush Mylist7"Hello" (integer)2 Redis127.0.0.1:6379>Rpush Mylist7"foo" (integer) 3 redis 127.0.0.1:6379>< Span class= "Apple-converted-space" > rpush mylist7 "Hello" (integer) 4 redis 127.0.0.1:6379>< Span class= "Apple-converted-space" > lrem mylist7 0 " Hello "(integer) 3 redis 127.0.0.1:6379> lrange Mylist7 0 -1 1) "foo" Redis 127.0.0.1:6379
6, LTrim
Keep data within the value range of the specified key:
Redis127.0.0.1:6379>Rpush Mylist8"One" (integer)1 Redis127.0.0.1:6379>Rpush Mylist8"Both" (integer)2 Redis127.0.0.1:6379>Rpush Mylist8"Three" (integer)3 Redis127.0.0.1:6379>Rpush mylist8 "four" (integer) 4 redis 127.0.0.1:6379> ltrim mylist8 1 -1 OK redis 127.0.0.1:6379> lrange mylist8 0 -1 1) "Double" 2) "three" 3) "Four" Redis 127.0.0.1:63 79>
7, Lpop
Removes the element from the head of the list and returns the Delete element:
Redis127.0.0.1:6379> lrange mylist 0 -1 1) "Hello" 2) "World" Redis 127.0.0.1:6379> lpop MyList "Hello" Redis 127.0.0.1:6379> lrange mylist 0 -1 1) "World" Redis 127.0.0.1:6379> ;
8, Rpop
Removes the element from the tail of the list and returns the element to delete:
Redis127.0.0.1:6379> lrange mylist2 0 -1 1) "World " Redis 127.0.0.1:6379> RPO P mylist2 "World" Redis 127.0.0.1:6379> lrange mylist2 0 -1 1) "Hello" Redis 127.0.0.1:6379 >
9, Rpoplpush
Removes the element from the end of the first list and adds it to the head of the second list, and finally returns the value of the removed element, the entire operation being atomic. If the first list is empty or does not exist return nil:
Redis127.0.0.1:6379>Lrange MYLIST50-1 1)"Three" 2)"Foo" 3)"Hello" Redis127.0.0.1:6379>Lrange Mylist60-1 1)"Hello" 2)"Foo" Redis 127.0.0.1:6379> Rpoplpush mylist5 mylist6 "Hello" Redis 127.0.0.1:6379> lrange mylist5 0 -1 1) " three "2) " foo "Redis 127.0.0.1:6379> lrange mylist6 0 -1 1) " Hello "2) " Hello "3) " foo "Redis 127.0.0.1:6379>
10, Lindex
Returns the element of the index position in the list named key:
Redis127.0.0.1:6379> lrange mylist5 0 -1 1) "three" 2) "foo" Redis 127.0.0.1:6379> Linde X mylist5 0 "three" Redis 127.0.0.1:6379> lindex mylist5 1 "foo" Redis 127.0.0.1:6379>
11, Llen
Returns the length of the key corresponding to the list:
Redis127.0.0.1:6379> llen mylist5 (integer) 2 redis 127.0.0.1:6379>
Redis Detailed: Lists data types and operations