Before summing up the list, you need to understand several concepts related to list:
List: A left-to-right queue, a personal understanding is more like a stack, a regular mode, an advanced list of elements, and a post-out.
Header element: The first element of the leftmost list.
Footer element: The last element at the right end of the list. A list that does not contain any elements becomes an empty list.
1) New
A) Lpush
Syntax: Lpush key Value[value]
Explanation: Inserting one or more elements into a table header. If more than one value is inserted, follow the left-to-right order. Returns the number of inserted elements
[Plain]View Plaincopy
- [Email protected] ~]# REDIS-CLI
- Redis 127.0.0.1:6379> lpush lst.user Zhangsan #插入一个元素
- (integer) 1
- Redis 127.0.0.1:6379> lpush lst.user zhangsan Lisi #插入多个元素, List allows duplicate elements to be inserted
- (integer) 3
b) lpushx
Syntax: Lpushx key value
Explanation: Inserting a header element can only be inserted if and only if the list key exists. Returns the number of elements in a list
[Plain]View Plaincopy
- Redis 127.0.0.1:6379> lpushx lst.user Wangwu #列表存在
- (integer) 4
- Redis 127.0.0.1:6379> lpushx Lst.tech Wangwu #列表不存在, not inserted
- (integer) 0
c) Rpush
Syntax: Rpush key [value]
Explanation: Inserts one or more values into the queue's tail. Multiple values are added from left to right. Returns the number of elements in a list
[Plain]View Plaincopy
- Redis 127.0.0.1:6379> rpush lst.user ls005 #列表存在
- (integer) 5
- Redis 127.0.0.1:6379> rpush lst.tech tec01 tec02 tec03 #列表不存在
- (integer) 3
D) rpushx
Syntax: Rpushx key value
Explanation: The value is inserted at the end of the list, and is added only if the list key exists. Returns the number of list elements
[Plain]View Plaincopy
- Redis 127.0.0.1:6379> rpushx Lst.tech tec04 #key exist
- (integer) 4
- Redis 127.0.0.1:6379> rpushx lst.sub englist #key not present
- (integer) 0
e) Linsert
Syntax: Linsert key Before|after Pivot value
Explanation: Inserts a value into the front or back of the pivot. Returns the number of list elements. If the reference point pivot does not exist, do not insert. If there are multiple pivot, whichever is closest to the table header
[Plain]View Plaincopy
- REDIS 127.0.0.1:6379> LINSERT LST.TECH AFTER TEC04 tec06 #后面插
- (integer) 5
- redis 127.0.0.1:6379> linsert lst.tech before tec06 tec05 #前面插
- (integer) 6
- redis 127.0.0.1:6379> linsert lst.tech before tec08 tec07 #参照点不存在,
- (integer) -1
- redis 127.0.0.1:6379> linsert lst.user after zhangsan zhangsan01 #列表中有多个pivot, the first one from left to right is the
- (integer) 6
2) query
A) lindex
Syntax: Lindex key index
Explanation: Gets the elements of the list by indexing index. index>=0, 0 table header, 1 second element, and so on, index<0, 1, footer, 2 the penultimate element, and so on
[Plain]View Plaincopy
- Redis 127.0.0.1:6379> lindex lst.user 0 #表头
- "Wangwu"
- Redis 127.0.0.1:6379> lindex lst.user-1 #表尾
- "Ls005"
- Redis 127.0.0.1:6379> lindex lst.user 2 #第三个元素
- "Zhangsan"
- Redis 127.0.0.1:6379> lindex lst.user-2 #倒数第二个元素
- "Zhangsan"
b) Lrange
Syntax: Lrange key start stop
Explanation: Gets some column elements that specify the start and end ranges. 0: Table header,-1: Footer. Returns an empty list if the stop specified element is to the left of start
[Plain]View Plaincopy
- redis 127.0.0.1:6379> lrange lst.user 0 -1 # return all
- 1) "Wangwu" &NBSP;&NBSP;
- 2) "Lisi" &NBSP;&NBSP;
- 3 ) "Zhangsan" &NBSP;&NBSP;
- 4) "Zhangsan01" &NBSP;&NBSP;
- 5) "Zhangsan"
- 6) "ls005" &NBSP;&NBSP;
- Redis 127.0.0.1:6379> lrange lst.user -1 0 #返回空
- (empty list or Set)
- redis 127.0.0.1:6379> lrange lst.user 1 2 # Return multiple ,
- 1) "Lisi" &NBSP;&NBSP;
- 2) "Zhangsan" &NBSP;&NBSP;
- redis 127.0.0.1:6379> lrange lst.user 1 1 #返回一个元素
- 1) "Lisi" &NBSP;&NBSP;
3) Modify
LSet
Syntax: LSet key index value
Explanation: Sets the value of the specified index in the list, and if the specified index does not exist, an error
[Plain]View Plaincopy
- redis 127.0.0.1:6379> lset lst.user 2 zhangsan1 #设置第三个元素为zhangsan1
- ok
- redis 127.0.0.1:6379> lrange lst.user 0 -1
- 1) "Wangwu"
- 2) "Lisi" &NBSP;&NBSP;
- 3) "Zhangsan1" &NBSP;&NBSP;
- 4) "Zhangsan01" &NBSP;&NBSP;
- 5) "Zhangsan" &NBSP;&NBSP;
- 6) "ls005"
- redis 127.0.0.1:6379> lset lst.user 6 ls006 # The specified index does not exist
-
4) Delete
A) LTrim
Syntax: LTrim key start stop
Explanation: Preserves the elements of the specified range and removes all other elements
[Plain]View Plaincopy
- Redis 127.0.0.1:6379> LTrim Lst.user 0-2
- Ok
- Redis 127.0.0.1:6379> lrange Lst.user 0-1
- 1) "Wangwu"
- 2) "Lisi"
- 3) "Zhangsan1"
- 4) "Zhangsan01"
- 5) "Zhangsan"
b) Lrem
Syntax: Lrem key Count value
Explanation: Remove the element equal to value, when count>0, search from the table header, remove count, and when count=0, search from the table header, remove all equals value, and when count<0, start looking at the end of the table, remove |count| A.
[Plain]View Plaincopy
- Redis 127.0.0.1:6379> lrange Lst.user 0-1
- 1) "Zhangsan"
- 2) "Wangwu"
- 3) "Lisi"
- 4) "Zhangsan1"
- 5) "Zhangsan01"
- 6) "Zhangsan"
- 7) "Lisi"
- 8) "Zhangsan"
- 9) "Lisi"
- "Zhangsan"
- Redis 127.0.0.1:6379> lrem lst.user 2 Zhangsan #移除前两个zhangsan
- (integer) 2
- Redis 127.0.0.1:6379> lrange Lst.user 0-1
- 1) "Wangwu"
- 2) "Lisi"
- 3) "Zhangsan1"
- 4) "Zhangsan01"
- 5) "Lisi"
- 6) "Zhangsan"
- 7) "Lisi"
- 8) "Zhangsan"
- Redis 127.0.0.1:6379> lrange lst.user-1 Zhangsan #移除最后一个zhangsan
- (empty list or set)
- Redis 127.0.0.1:6379> lrange Lst.user 0-1
- 1) "Wangwu"
- 2) "Lisi"
- 3) "Zhangsan1"
- 4) "Zhangsan01"
- 5) "Lisi"
- 6) "Zhangsan"
- 7) "Lisi"
- Redis 127.0.0.1:6379> lrem lst.user 0 Lisi #移除所有lisi
- (integer) 3
- Redis 127.0.0.1:6379> lrange Lst.user 0-1
- 1) "Wangwu"
- 2) "Zhangsan1"
- 3) "Zhangsan01"
- 4) "Zhangsan"
- Redis 127.0.0.1:6379>
c) Rpop
Syntax: Rpop key
Explanation: Removing and returning a footer element
[Plain]View Plaincopy
- Redis 127.0.0.1:6379> Rpop Lst.user
- "Zhangsan"
D) Lpop
Syntax: Lpop key
Explanation: Removing and returning a footer element
[Plain]View Plaincopy
- Redis 127.0.0.1:6379> Lpop Lst.user
- "Wangwu"
5) Other
A) Llen
Syntax: Llen key
Explanation: Get list length
[Plain]View Plaincopy
- Redis 127.0.0.1:6379> Llen Lst.user
- (integer) 2
Reference: Http://redis.io/commands#list
Redis Series-Storage list main Operation function summary (GO)