lpush 从头部压入数据
127.0.0.1:6379> lpush listname value1
(integer) 1//返回list的当前长度
127.0.0.1:6379> lpush listname value2
(integer) 2
(integer) 2
127.0.0.1:6379> lpush listname value2
(integer) 3//成功添加重复的元素,说明list是可重复的
lpushx 当给定Listname的list存在的时候,从上面插入数据,否则不做任何操作
127.0.0.1:6379> lpushx listname0 value
(integer) 0
rpushx 从给定Listname的list存在的时候,从下面插入数据,否则不做任何操作
127.0.0.1:6379> rpushx listname0 value
(integer) 0
lrange 从头部到尾部(栈)获取指定索引范围的数据
lrange listname 0 -1
127.0.0.1:6379> lrange listname 0 0
1) "value2"
127.0.0.1:6379> lrange listname 0 1
1) "value2"
1) "value2"
127.0.0.1:6379> lrange listname 0 1
1) "value2"
2) "value2"
127.0.0.1:6379> lrange listname 0 -1
1) "value2"
2) "value2"
1) "value2"
2) "value2"
3) "value1"
rpush 从尾部压入元素
127.0.0.1:6379> rpush listname value0
(integer) 4
127.0.0.1:6379> lrange listname 0 -1
1) "value2"
2) "value2"
3) "value1"
(integer) 4
127.0.0.1:6379> lrange listname 0 -1
1) "value2"
2) "value2"
3) "value1"
4) "value0"
linsert 中间插入
linsert listname before value1 value2在value1前面插入value2
127.0.0.1:6379> linsert listname before value0 value--
(integer) 5
127.0.0.1:6379> lrange listname 0 -1
1) "value2"
2) "value2"
(integer) 5
127.0.0.1:6379> lrange listname 0 -1
1) "value2"
2) "value2"
3) "value1"
4) “value--“ //成功插入
5) "value0"
127.0.0.1:6379> linsert listname before value2 value--
(integer) 6(成功插入)
127.0.0.1:6379> lrange listname 0 -1
1) “value--“ //插入到从上到下查找到的第一个匹配值的上面
2) "value2"
3) "value2"
4) "value1"
5) "value--"
3) "value2"
4) "value1"
5) "value--"
6) "value0"
127.0.0.1:6379> linsert listname before value-- value+
(integer) 7
(integer) 7
127.0.0.1:6379> lrange listname 0 -1
1) "value+”//插入到从上到下查找到的第一个匹配值的上面
2) "value--"
3) "value2"
4) "value2"
5) "value1"
6) "value--"
3) "value2"
4) "value2"
5) "value1"
6) "value--"
7) "value0"
127.0.0.1:6379> linsert listname before value7 value8
(integer) -1//找不到value7 插入不成功
127.0.0.1:6379> lrange listname 0 -1
1) "value+"
2) "valuenew"
3) "value2"
4) "value2"
5) "value1"
6) "value--"
1) "value+"
2) "valuenew"
3) "value2"
4) "value2"
5) "value1"
6) "value--"
7) "value0"
lset 更新指定脚标的元素的value
127.0.0.1:6379> lset listname 1 valuenew
OK
127.0.0.1:6379> lrange listname 1 1
1) "valuenew"
127.0.0.1:6379> lset listname 7 value7
(error) ERR index out of range//脚标越界的时候,会提示error
lrem 删除指定个数的值为指定value的元素
lrem listname x value
//先添加一个value2
127.0.0.1:6379> linsert listname before valuenew value2
(integer) 8
127.0.0.1:6379> lrange listname 0 -1
1) "value+"
2) "value2"
3) "valuenew"
4) "value2"
5) "value2"
6) "value1"
7) "value--"
(integer) 8
127.0.0.1:6379> lrange listname 0 -1
1) "value+"
2) "value2"
3) "valuenew"
4) "value2"
5) "value2"
6) "value1"
7) "value--"
8) "value0"
//移除1个value2
127.0.0.1:6379> lrem listname 1 value2
(integer) 1
//最上面的value2被移除了
127.0.0.1:6379> lrange listname 0 -1
1) "value+"
2) "valuenew"
3) "value2"
4) "value2"
5) "value1"
6) "value--"
1) "value+"
2) "valuenew"
3) "value2"
4) "value2"
5) "value1"
6) "value--"
7) "value0"
lindex 返回指定角标的元素
lindex listname x
127.0.0.1:6379> lindex listname 0
"value+"
"value+"
127.0.0.1:6379> lindex listname 7
(nil)//超出索引返回nill
127.0.0.1:6379> lindex listname -1
“value0”//脚标为负数的时候,从list底部开始找
127.0.0.1:6379> lindex listname -7
"value+"
127.0.0.1:6379> lindex listname -8
(nil)
lpop
从头部弹出一个元素,返回元素值并删除
127.0.0.1:6379> lpop listname
"value+"
127.0.0.1:6379> lpop listname
"valuenew"
127.0.0.1:6379> lrange listname 0 -1
1) "value2"
2) "value2"
3) "value1"
4) "value--"
"valuenew"
127.0.0.1:6379> lrange listname 0 -1
1) "value2"
2) "value2"
3) "value1"
4) "value--"
5) "value0"
rpop
从尾部弹出一个元素,返回元素值并删除
127.0.0.1:6379> rpop listname
"value0"
127.0.0.1:6379> lrange listname 0 -1
1) "value2"
2) "value2"
3) "value1"
"value0"
127.0.0.1:6379> lrange listname 0 -1
1) "value2"
2) "value2"
3) "value1"
4) "value--"
ltrim删除除指定范围外的脚标元素
ltrim listname x1 x2 只保留从x1~x2之间的元素
127.0.0.1:6379> ltrim listname 0 -1
OK
127.0.0.1:6379> lrange listname 0 -1
1) "value2"
2) "value2"
3) "value1"
OK
127.0.0.1:6379> lrange listname 0 -1
1) "value2"
2) "value2"
3) "value1"
4) "value--"
127.0.0.1:6379> ltrim listname 1 2
OK //成功删除脚标不在1-2之内的元素,这里删除了脚标为 0 3 的两个元素
127.0.0.1:6379> lrange listname 0 -1
1) "value2"
1) "value2"
2) "value1"
127.0.0.1:6379> ltrim listname 1 0
OK //删除1-0之外的元素,删除所有元素
127.0.0.1:6379> keys * //list被清除
(empty list or set)
//批量插入四条数据,注意版本问题,低版本的redis不支持批量插入
127.0.0.1:6379> lpush listname value-- value1 value2 value2
(integer) 4
127.0.0.1:6379> lrange listname 0 -1
1) "value2"
2) "value2"
3) "value1"
1) "value2"
2) "value2"
3) "value1"
4) "value--"
127.0.0.1:6379> lpush listname1 value value+ value-
(integer) 3
127.0.0.1:6379> lrange listname1 0 -1
1) "value-"
2) "value+"
1) "value-"
2) "value+"
3) "value"
rpoplpush从前一个list尾部弹出元素插入到第二个list头部
rpoplpush listname1 listname2
27.0.0.1:6379> RPOPLPUSH listname listname1
"value--"
127.0.0.1:6379> lrange listname 0 -1
1) "value2"
2) "value2"
3) "value1"
127.0.0.1:6379> lrange listname1 0 -1
1) "value--"
2) "value-"
3) "value+"
"value--"
127.0.0.1:6379> lrange listname 0 -1
1) "value2"
2) "value2"
3) "value1"
127.0.0.1:6379> lrange listname1 0 -1
1) "value--"
2) "value-"
3) "value+"
4) "value"
llen返回list的长度
127.0.0.1:6379> llen listname
(integer) 3
127.0.0.1:6379> llen listname1
(integer) 3
127.0.0.1:6379> llen listname1
(integer) 4