Type and operation of a lists
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. The Redis list type is actually a doubly linked list where each child element is a sring type. We can add delete elements from the head or tail of the list through the Push,pop operation, so that the list can be used as a stack and as a queue.
Use of two lists types
1. Lpush
Adds a string to the key corresponding to the list's head.
Redis127.0.0.1:6379>lpush mylist1 World (integer)1Redis127.0.0.1:6379>lpush mylist1 Hello (integer)2Redis127.0.0.1:6379> Lrange mylist10-11)"Hello"2)" World"
2. Rpush
Adds a string at the end of the key corresponding list.
Redis 127.0.0.1:6379> Rpush mylist2 BBB
(integer) 1
Redis 127.0.0.1:6379> Rpush mylist2 AAA
(integer) 2
Redis 127.0.0.1:6379> lrange mylist2 0-1
1) "BBB"
2) "AAA"
3. Linsert
Adds a string before or after a specific position in the list for which the key corresponds.
Redis 127.0.0.1:6379> Rpush Mylist3 World
(integer) 1
Redis 127.0.0.1:6379> Linsert mylist3 before World Hello
(integer) 2
Redis 127.0.0.1:6379> lrange mylist3 0-1
1) "Hello"
2) "World"
4. LSet
Sets the element value for the specified subscript in the list.
127.0. 0.1:6379>1127.0. 0.1:63790127.0. 0.1:63790 -11"hello"
5 Lrem
Removes N and value elements from the key corresponding list. (N < 0 never deleted, n =0 all deleted)
127.0. 0.1:6379>1127.0. 0.1:6379>2127.0. 0.1:637911
6 LTrim
Retains the data within the range of the specified key.
Redis 127.0.0.1:6379> Rpush Mylist8 One
(integer) 1
Redis 127.0.0.1:6379> Rpush Mylist8
(integer) 2
Redis 127.0.0.1:6379> LTrim mylist8 1-1
Ok
Redis 127.0.0.1:6379> lrange mylist8 0-1
1) "Both"
7 Lpop
Removes the element from the head of the list and returns the deleted element.
Redis 127.0.0.1:6379> Rpush mylist11 One
(integer) 1
Redis 127.0.0.1:6379> Rpush mylist11
(integer) 2
Redis 127.0.0.1:6379> Rpush mylist11 Three
(integer) 3
Redis 127.0.0.1:6379> Lpop mylist11
"One"
Redis 127.0.0.1:6379> lrange mylist11 0-1
1) "Both"
2) "Three"
8.rpoplpush
Removes the element from the end of the first list and adds it to the head of the second list.
Redis127.0.0.1:6379>rpush mylist12 One (integer)1Redis127.0.0.1:6379>rpush mylist12 (integer)2Redis127.0.0.1:6379>rpush mylist13 aaa (integer)1Redis127.0.0.1:6379>rpush mylist13 bbb (integer)2Redis127.0.0.1:6379>rpush mylist13 CCC (integer)3Redis127.0.0.1:6379>Rpoplpush mylist12 mylist13" Both"Redis127.0.0.1:6379> Lrange mylist120-11)" One"Redis127.0.0.1:6379> Lrange mylist130-11)" Both"2)"AAA"3)"BBB"4)"CCC"
9 Lindex
Returns the element in the list with the name key as the index position.
Redis 127.0.0.1:6379> lrange mylist13 0-1
1) "Both"
2) "AAA"
3) "BBB"
4) "CCC"
Redis 127.0.0.1:6379> lindex mylist13 0
"Both"
Redis 127.0.0.1:6379> lindex mylist13 1
"AAA"
Redis 127.0.0.1:6379> lindex mylist13 2
"BBB"
Ten Llen
Returns the length of the key corresponding to the list.
Redis 127.0.0.1:6379> Llen mylist13
(integer) 4
Redis 127.0.0.1:6379> lrange mylist13 0-1
1) "Both"
2) "AAA"
3) "BBB"
4) "CCC"
Redis04 using Redis database (lists type)