Redis笔记(5)redis五大数据类型2----list(列表)

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 左近左出栈

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值