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) lpushxSyntax: 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) RpushSyntax: 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) rpushxSyntax: 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) LinsertSyntax: 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 #参照点不存在, not plugged in
- (integer)-1
- Redis 127.0.0.1:6379> Linsert lst.user after Zhangsan Zhangsan01 #列表中有多个pivot, whichever is first from left to right
- (integer) 6
2) queryA) 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) LrangeSyntax: 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 #返回所有
- 1) "Wangwu"
- 2) "Lisi"
- 3) "Zhangsan"
- 4) "Zhangsan01"
- 5) "Zhangsan"
- 6) "ls005"
- 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 #返回多个
- 1) "Lisi"
- 2) "Zhangsan"
- Redis 127.0.0.1:6379> lrange lst.user 1 1 #返回一个元素
- 1) "Lisi"
3) ModifyLSet
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"
- 3) "Zhangsan1"
- 4) "Zhangsan01"
- 5) "Zhangsan"
- 6) "ls005"
- Redis 127.0.0.1:6379> LSet lst.user 6 ls006 #指定索引不存在
- (Error) ERR index out of range
4) DeleteA) 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) LremSyntax: 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) RpopSyntax: Rpop key
Explanation: Removing and returning a footer element
[Plain]View Plaincopy
- Redis 127.0.0.1:6379> Rpop Lst.user
- "Zhangsan"
D) LpopSyntax: Lpop key
Explanation: Removing and returning a footer element
[Plain]View Plaincopy
- Redis 127.0.0.1:6379> Lpop Lst.user
- "Wangwu"
5) OtherA) 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