【redis】list类型命令简述

redis的list类型,可以存储双向链表作为value,key保留有head和tail指针可以指向双向链表的头和尾,因此可以直接从头或尾对list进行操作。

全部命令如下:

复制代码
127.0.0.1:6379> help @list

  BLMOVE source destination LEFT|RIGHT LEFT|RIGHT timeout
  summary: Pop an element from a list, push it to another list and return it; or block until one is available
  since: 6.2.0

  BLMPOP timeout numkeys key [key ...] LEFT|RIGHT [COUNT count]
  summary: Pop elements from a list, or block until one is available
  since: 7.0.0

  BLPOP key [key ...] timeout
  summary: Remove and get the first element in a list, or block until one is available
  since: 2.0.0

  BRPOP key [key ...] timeout
  summary: Remove and get the last element in a list, or block until one is available
  since: 2.0.0

  BRPOPLPUSH source destination timeout
  summary: Pop an element from a list, push it to another list and return it; or block until one is available
  since: 2.2.0

  LINDEX key index
  summary: Get an element from a list by its index
  since: 1.0.0

  LINSERT key BEFORE|AFTER pivot element
  summary: Insert an element before or after another element in a list
  since: 2.2.0

  LLEN key
  summary: Get the length of a list
  since: 1.0.0

  LMOVE source destination LEFT|RIGHT LEFT|RIGHT
  summary: Pop an element from a list, push it to another list and return it
  since: 6.2.0

  LMPOP numkeys key [key ...] LEFT|RIGHT [COUNT count]
  summary: Pop elements from a list
  since: 7.0.0

  LPOP key [count]
  summary: Remove and get the first elements in a list
  since: 1.0.0

  LPOS key element [RANK rank] [COUNT num-matches] [MAXLEN len]
  summary: Return the index of matching elements on a list
  since: 6.0.6

  LPUSH key element [element ...]
  summary: Prepend one or multiple elements to a list
  since: 1.0.0

  LPUSHX key element [element ...]
  summary: Prepend an element to a list, only if the list exists
  since: 2.2.0

  LRANGE key start stop
  summary: Get a range of elements from a list
  since: 1.0.0

  LREM key count element
  summary: Remove elements from a list
  since: 1.0.0

  LSET key index element
  summary: Set the value of an element in a list by its index
  since: 1.0.0

  LTRIM key start stop
  summary: Trim a list to the specified range
  since: 1.0.0

  RPOP key [count]
  summary: Remove and get the last elements in a list
  since: 1.0.0

  RPOPLPUSH source destination
  summary: Remove the last element in a list, prepend it to another list and return it
  since: 1.2.0

  RPUSH key element [element ...]
  summary: Append one or multiple elements to a list
  since: 1.0.0

  RPUSHX key element [element ...]
  summary: Append an element to a list, only if the list exists
  since: 2.2.0

下面示例如下:

lpush :lpush key e1 e2 e3...将数据从头那里推入list
lpop :lpop key,将数据从head弹出
这样2个同向的命令组合起来,可以实现一个队列

反向的命令组合起来,可以实现一个栈。

复制代码
127.0.0.1:6379> lpush k1 a b c d e
(integer) 5
127.0.0.1:6379> lpop k1
"e"
127.0.0.1:6379> lpop k1
"d"

lrange :lrange key start end,展示key对应的从下标start到end的所有数据

复制代码
127.0.0.1:6379> lrange k1 0 -1
1) "c"
2) "b"
3) "a"

lindex:lindex key index ,返回key对应的List指定index位置的值

复制代码
127.0.0.1:6379> lrange k1 0 -1
1) "f"
2) "e"
3) "d"
4) "c"
5) "b"
6) "a"
127.0.0.1:6379> lindex k1 1
"e"

lset:lset key index value,在key对应的list中的指定下标处替换为value

复制代码
127.0.0.1:6379> lset k1 3 x
OK
127.0.0.1:6379> lrange k1 0 -1
1) "f"
2) "e"
3) "d"
4) "x"
5) "b"
6) "a"

lrem:lrem key num target,删除key对应的list中的target元素,如果num大于0,从head开始删num个。如果num小于0,从tail开始删abs(num)个

复制代码
127.0.0.1:6379> lrange k3 0 -1
 1) "d"
 2) "6"
 3) "a"
 4) "5"
 5) "c"
 6) "4"
 7) "a"
 8) "3"
 9) "b"
10) "2"
11) "a"
12) "1"
127.0.0.1:6379> lrem k3 2 a
(integer) 2
127.0.0.1:6379> lrange k3 0 -1
 1) "d"
 2) "6"
 3) "5"
 4) "c"
 5) "4"
 6) "3"
 7) "b"
 8) "2"
 9) "a"
10) "1"

linsert:linsert key before/after element value,在key对应的list中,在元素element(不是下标)之前或者之后,添加value

复制代码
127.0.0.1:6379> lrange k3 0 -1
 1) "d"
 2) "6"
 3) "5"
 4) "c"
 5) "4"
 6) "3"
 7) "b"
 8) "2"
 9) "a"
10) "1"
127.0.0.1:6379> linsert k3 after 6 a
(integer) 11
127.0.0.1:6379> lrange k3 0 -1
 1) "d"
 2) "6"
 3) "a"
 4) "5"
 5) "c"
 6) "4"
 7) "3"
 8) "b"
 9) "2"
10) "a"
11) "1"

llen :llen key,返回长度
redis的List类型有很多关于下标的操作,也可以将其抽象为一个数组来使用

复制代码
127.0.0.1:6379> llen k3
(integer) 10

blpop:blpop key time,弹出指定key对应的list中的一个元素,如果list没有元素或者不存在key对应的这个list,则阻塞等待time指定的时间,0表示一直等待,单位是s。

复制代码
127.0.0.1:6379> blpop k1 0

阻塞期间如果list有了元素,则会中断阻塞并弹出

复制代码
127.0.0.1:6379> blpop k1 0
1) "k1"
2) "a"
(35.77s)

ltrim:ltrim key start end,删除key对应的list start和end之外的两端的元素

复制代码
127.0.0.1:6379> lrange k4 0 -1
1) "f"
2) "e"
3) "d"
4) "c"
5) "b"
6) "a"
127.0.0.1:6379> ltrim k4 1 -2
OK
127.0.0.1:6379> lrange k4 0 -1
1) "e"
2) "d"
3) "c"
4) "b"
相关推荐
leeyi2 天前
Checkpoint 机制:Agent 怎么在断电后接着跑
redis·aigc·agent
云技纵横3 天前
一个 @Async 让循环依赖暴雷:Spring 代理的暗坑
redis
犯困蛋挞yy4 天前
用Claude快速解决Redis代码报错反复无解的问题
redis
用户31693538118310 天前
Java连接Redis
redis
qq_3692243312 天前
Windows全系通用!ntdll.dll文件丢失、报错、闪退问题的完整排查与修复教程
windows·dll·dll修复·dll丢失·dll错误
小小工匠12 天前
Redis - 事务机制:能实现 ACID 属性吗
数据结构·redis·性能优化·并发·持久化
阿米亚波12 天前
【Windows】QEMU 启动 openEuler aarch64/arm64 架构系统 + 离线软件源
linux·windows·经验分享·笔记·架构·arm
caimouse12 天前
Reactos 第 10 章 网络操作 — 10.3.1 NIC驱动
网络·windows
taocarts_bidfans12 天前
反向海淘跨境缓存架构优化:taocarts Redis分层缓存实战技术
redis·缓存·架构·反向海淘·taocarts
初圣魔门首席弟子12 天前
Node.js 详细介绍(知识库版)
windows·qt·node.js·知识库