Redis(8)—— Redis列表数据类型

List(列表)

在这里插入图片描述
在redis里面,我们可以把list玩成 ,栈、队列、阻塞队列! 所有的list命令都是用l开头的,Redis不区分大小命令

##########################################################################
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	# 获取list中值!
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 righr	# 将一个值或者多个值,插入到列表位部 (右)
(integer) 4
127.0.0.1:6379> LRANGE list 0 -1
1)"three"
2)"two"
3)"one"
4)"righr"


########################################################################## LPOP
RPOP
127.0.0.1:6379> LRANGE list 0 -1
1)"three"
2)"two"
3)"one"
4)"righr"
127.0.0.1:6379> Lpop list	# 移除list的第一个元素
"three"
127.0.0.1:6379> Rpop list	# 移除list的最后一个元素
"righr"
127.0.0.1:6379> LRANGE list 0 -1
1)"two"
2)"one" ########################################################################## Lindex

127.0.0.1:6379> LRANGE list 0 -1
1)"two"
2)"one"
127.0.0.1:6379> lindex list 1	# 通过下标获得 list 中的某一个值!
"one"
127.0.0.1:6379> lindex list 0 "two"

##########################################################################
Llen


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> Llen list	# 返回列表的长度
(integer) 3


##########################################################################
移除指定的值! 取关	uid

Lrem
127.0.0.1:6379> LRANGE list 0 -1
1)"three"
2)"three"
3)"two"
4)"one"
127.0.0.1:6379> lrem list 1 one	# 移除list集合中指定个数的value,精确匹配
(integer) 1
127.0.0.1:6379> LRANGE list 0 -1
1)"three"
2)"three"
3)"two"
127.0.0.1:6379> lrem list 1 three
(integer) 1
127.0.0.1:6379> LRANGE list 0 -1
1)"three"
2)"two"
127.0.0.1:6379> Lpush list three (integer) 3
127.0.0.1:6379> lrem list 2 three
(integer) 2
127.0.0.1:6379> LRANGE list 0 -1
1) "two"


##########################################################################
trim 修剪。; list 截断!


127.0.0.1:6379> keys
(empty list or set)
127.0.0.1:6379> Rpush mylist "hello" (integer) 1
127.0.0.1:6379> Rpush mylist "hello1" (integer) 2
127.0.0.1:6379> Rpush mylist "hello2" (integer) 3
127.0.0.1:6379> Rpush mylist "hello3" (integer) 4
127.0.0.1:6379> ltrim mylist 1 2	# 通过下标截取指定的长度,这个list已经被改变了,截断了
只剩下截取的元素!
OK
127.0.0.1:6379> LRANGE mylist 0 -1
1)"hello1"
2)"hello2"


##########################################################################
rpoplpush # 移除列表的最后一个元素,将他移动到新的列表中!

127.0.0.1:6379> rpush mylist "hello"

(integer) 1
127.0.0.1:6379> rpush mylist "hello1" (integer) 2
127.0.0.1:6379> rpush mylist "hello2" (integer) 3
127.0.0.1:6379> rpoplpush mylist myotherlist	# 移除列表的最后一个元素,将他移动到新的
列表中!
"hello2"
127.0.0.1:6379> lrange mylist 0 -1 # 查看原来的列表
1)"hello"
2)"hello1"
127.0.0.1:6379> lrange myotherlist 0 -1	# 查看目标列表中,确实存在改值!
1) "hello2"


##########################################################################
lset	将列表中指定下标的值替换为另外一个值,更新操作127.0.0.1:6379> EXISTS list	# 判断这个列表是否存在(integer) 0
127.0.0.1:6379> lset list 0 item	# 如果不存在列表我们去更新就会报错
(error) ERR no such key 127.0.0.1:6379> lpush list value1 (integer) 1
127.0.0.1:6379> LRANGE list 0 0
1) "value1"
127.0.0.1:6379> lset list 0 item	# 如果存在,更新当前下标的值
OK
127.0.0.1:6379> LRANGE list 0 0
1) "item"
127.0.0.1:6379> lset list 1 other	# 如果不存在,则会报错!
(error) ERR index out of range ##########################################################################
linsert # 将某个具体的value插入到列把你中某个元素的前面或者后面!

127.0.0.1:6379> Rpush mylist "hello" (integer) 1
127.0.0.1:6379> Rpush mylist "world" (integer) 2
127.0.0.1:6379> LINSERT mylist before "world" "other" (integer) 3
127.0.0.1:6379> LRANGE mylist 0 -1
1)"hello"
2)"other"
3)"world"
127.0.0.1:6379> LINSERT mylist after world new (integer) 4
127.0.0.1:6379> LRANGE mylist 0 -1
1)"hello"
2)"other"
3)"world"
4)"new"

小结

  • 他实际上是一个链表,before Node after , left,right 都可以插入值
  • 如果key 不存在,创建新的链表
    如果key存在,新增内容
  • 如果移除了所有值,空链表,也代表不存在!
  • 在两边插入或者改动值,效率最高! 中间元素,相对来说效率会低一点~
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值