Redis五大数据类型之 List(列表)

基本的数据类型,列表
在redis里面,我们可以把list玩成栈或者队列
这里的L代表是左边

127.0.0.1:6379[1]> LPUSH list one    # 将一个值或者多个值,插入到列表头部(左)
(integer) 1
127.0.0.1:6379[1]> LPUSH list two
(integer) 2
127.0.0.1:6379[1]> LPUSH list three
(integer) 3
127.0.0.1:6379[1]> LRANGE list 0 -1
1) "three"
2) "two"
3) "one"
127.0.0.1:6379[1]> LRANGE list 0 1    # 通过区间获取具体的值
1) "three"
2) "two"
127.0.0.1:6379[1]> RPUSH list right    # 将一个值或者多个值,插入到列表尾部(右)
(integer) 4
127.0.0.1:6379[1]> LRANGE list 0 -1
1) "three"
2) "two"
3) "one"
4) "right"
127.0.0.1:6379[1]> LRANGE list 0 -1
1) "three"
2) "two"
3) "one"
4) "right"
127.0.0.1:6379[1]> LPOP list    # 移除list的第一个元素
"three"
127.0.0.1:6379[1]> RPOP list    # 移除list的最后一个元素
"right"
127.0.0.1:6379[1]> LRANGE list 0 -1
1) "two"
2) "one"
127.0.0.1:6379[1]> LRANGE list 0 -1
1) "two"
2) "one"
127.0.0.1:6379[1]> LINDEX list 0
"two"
127.0.0.1:6379[1]> LINDEX list 1
"one"
  • 返回列表的长度
127.0.0.1:6379[1]> LPUSH list one two three
(integer) 3
127.0.0.1:6379[1]> LLEN list
(integer) 3
127.0.0.1:6379[1]> LRANGE list 0 -1
1) "three"
2) "two"
3) "one"
  • 移除指定的值
127.0.0.1:6379[1]> LRANGE list 0 -1
1) "three"
2) "three"
3) "two"
4) "one"
127.0.0.1:6379[1]> LREM list 1 one    # 移除list集合中指定个数的value,精确匹配
(integer) 1
127.0.0.1:6379[1]> LRANGE list 0 -1
1) "three"
2) "three"
3) "two"
127.0.0.1:6379[1]> LREM list 1 three
(integer) 1
127.0.0.1:6379[1]> LRANGE list 0 -1
1) "three"
2) "two"
127.0.0.1:6379[1]> LPUSH list three
(integer) 3
127.0.0.1:6379[1]> LREM list 2 three
(integer) 2
127.0.0.1:6379[1]> LRANGE list 0 -1
1) "two"
  • trim,修剪
127.0.0.1:6379[1]> RPUSH mylist hello hello1 hello2 hello3
(integer) 4
127.0.0.1:6379[1]> LRANGE mylist 0 -1
1) "hello"
2) "hello1"
3) "hello2"
4) "hello3"
127.0.0.1:6379[1]> LTRIM mylist 1 2    # 通过下标截取指定的长度,这个list已经被修改,阶段了只剩下截取的元素
OK
127.0.0.1:6379[1]> LRANGE mylist 0 -1
1) "hello1"
2) "hello2"
  • rpoplpush
127.0.0.1:6379[1]> RPUSH mylist hello hello1 hello2
(integer) 3
127.0.0.1:6379[1]> RPOPLPUSH mylist myotherlist    # 移除列表的最后一个元素,将他移动到新的列表中
"hello2"
127.0.0.1:6379[1]> LRANGE myotherlist 0 -1    # 查看新的列表
1) "hello2"
127.0.0.1:6379[1]> LRANGE mylist 0 -1    # 查看原来的列表
1) "hello"
2) "hello1"
  • lset 将列表中指定下标的值替换为另一个值,更新操作
127.0.0.1:6379[1]> EXISTS list    # 判断这个列表是否存在
(integer) 0
127.0.0.1:6379[1]> LSET list 0 item    # 如果不存在这个列我们去更新就会保存
(error) ERR no such key
127.0.0.1:6379[1]> LPUSH list value1
(integer) 1
127.0.0.1:6379[1]> LRANGE list 0 0
1) "value1"
127.0.0.1:6379[1]> LSET list 0 item    # 如果存在,更新当前下标的值
OK
127.0.0.1:6379[1]> LRANGE list 0 0
1) "item"
127.0.0.1:6379[1]> LRANGE list 1 other    # 如果不存在,则会报错
(error) ERR value is not an integer or out of range
  • linsert
127.0.0.1:6379[1]> RPUSH mylist hello world
(integer) 2
127.0.0.1:6379[1]> LINSERT mylist before world other
(integer) 3
127.0.0.1:6379[1]> LRANGE mylist 0 -1
1) "hello"
2) "other"
3) "world"
127.0.0.1:6379[1]> LINSERT mylist after world new
(integer) 4
127.0.0.1:6379[1]> LRANGE mylist 0 -1
1) "hello"
2) "other"
3) "world"
4) "new"
127.0.0.1:6379[1]> del mylist    # 删除列表中所有的元素

小结

  • 它实际上是一个链表,Before Node After,left,right都可以插入值
  • 如果key不存在,创建新的链表
  • 如果key存在,新增内容
  • 如果移除了所有值,空链表,也代表不存在
  • 两边插入或者改动值,效率最高!中间元素,相对来说效率会低一点

消息排队!消息队列(Lpush Rpop),栈(Lpush Lpop)

本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
未填写
文章
247
粉丝
17
喜欢
213
收藏
58
排名:732
访问:9674
私信
所有博文
社区赞助商