list(列表)
应用场景:栈、队列、阻塞队列
lpush key element ··· 从左侧(头部)插入一个值,或多个值
lrange key start end 查看list列表或查看区间
rpush key element ··· 从右侧(尾部)插入一个值,或多个值
127.0.0.1:6379> lpush list one
(integer) 1
127.0.0.1:6379> lpush list two
(integer) 2
127.0.0.1:6379> lpush list three
(integer) 3
127.0.0.1:6379> lrange list 0 -1
1) "three"
2) "two"
3) "one"
127.0.0.1:6379> lrange list 0 1
1) "three"
2) "two"
127.0.0.1:6379> rpush list 01 02
(integer) 5
127.0.0.1:6379> lrange list 0 -1
1) "three"
2) "two"
3) "one"
4) "01"
5) "02"
lpop key 从左侧移除一个元素
rpop key 从右侧移除一个元素
127.0.0.1:6379> lrange list 0 -1
1) "three"
2) "two"
3) "one"
4) "01"
5) "02"
127.0.0.1:6379> lpop list
"three"
127.0.0.1:6379> rpop list
"02"
127.0.0.1:6379> lrange list 0 -1
1) "two"
2) "one"
3) "01"
lindex key index 根据下标获取值
127.0.0.1:6379> lrange list 0 -1
1) "two"
2) "one"
3) "01"
127.0.0.1:6379> lindex list 0
"two"
llen key 获取list长度
127.0.0.1:6379> lrange list 0 -1
1) "two"
2) "one"
3) "01"
127.0.0.1:6379> llen list
(integer) 3
lrem key count element 移除指定个数的值,精确匹配
127.0.0.1:6379> lrange list 0 -1
1) "three"
2) "two"
3) "one"
4) "01"
127.0.0.1:6379> lrem list 1 01
(integer) 1
127.0.0.1:6379> lrange list 0 -1
1) "three"
2) "two"
3) "one"
ltrim key start end 根据下标修剪list
127.0.0.1:6379> lpush list one two three four
(integer) 4
127.0.0.1:6379> lrange list 0 -1
1) "four"
2) "three"
3) "two"
4) "one"
127.0.0.1:6379> ltrim list 1 2
OK
127.0.0.1:6379> lrange list 0 -1
1) "three"
2) "two"
rpoplpush source destination 从右侧拿取放到另外list中
127.0.0.1:6379> lpush list one two three
(integer) 3
127.0.0.1:6379> lrange list 0 -1
1) "three"
2) "two"
3) "one"
127.0.0.1:6379> rpoplpush list othlist
"one"
127.0.0.1:6379> lrange list 0 -1
1) "three"
2) "two"
127.0.0.1:6379> lrange othlist 0 -1
1) "one"
lset key index element 通过下表改变值,不存在会报错
127.0.0.1:6379> exists list //检查列表是否存在
(integer) 1
127.0.0.1:6379> lrange list 0 -1
1) "three"
2) "two"
127.0.0.1:6379> lset list 0 xxx
OK
127.0.0.1:6379> lrange list 0 -1
1) "xxx"
2) "two"
127.0.0.1:6379> lset list 2 aaa
(error) ERR index out of range
linsert key before|after pivot element 在某个值的前/后插入值
127.0.0.1:6379> lrange list 0 -1
1) "xxx"
2) "two"
127.0.0.1:6379> linsert list before two three
(integer) 3
127.0.0.1:6379> lrange list 0 -1
1) "xxx"
2) "three"
3) "two"
127.0.0.1:6379> linsert list after two one
(integer) 4
127.0.0.1:6379> lrange list 0 -1
1) "xxx"
2) "three"
3) "two"
4) "one"
小结:lpush rpop 左进右出消息队列,lpush lpop 左近左出栈