Redis数据结构之列表

Redis数据结构之列表

Redis列表是简单的字符串列表,按照插入顺序排序。你可以添加一个元素到列表的头部(左边)或者尾部(右边)一个列表最多可以包含 2^32 - 1 个元素 (4294967295, 每个列表超过40亿个元素)。

查看命令帮助

127.0.0.1:6379> help @list

  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

  LPOP key
  summary: Remove and get the first element 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
  summary: Remove and get the last element 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

127.0.0.1:6379> LPUSH mylist ddfff 21 nb
(integer) 3
127.0.0.1:6379> LRANGE mylist 0 -1
1) "nb"
2) "21"
3) "ddfff"

从右边插入数据

RPUSH

127.0.0.1:6379> RPUSH mylist qq ll nn
(integer) 6
127.0.0.1:6379> LRANGE mylist 0 -1
1) "nb"
2) "21"
3) "ddfff"
4) "qq"
5) "ll"
6) "nn"

若list存在,则从左边依次追加元素,不存在则忽略

LPUSHX

127.0.0.1:6379> LPUSHX mylist ww ee
(integer) 8
127.0.0.1:6379> LRANGE mylist 0 -1
1) "ee"
2) "ww"
3) "nb"
4) "21"
5) "ddfff"
6) "qq"
7) "ll"
8) "nn"
127.0.0.1:6379> LPUSHX youlist ww ee
(integer) 0
127.0.0.1:6379> LRANGE youlist 0 -1
(empty array)

若list存在,则从右边依次追加元素,不存在则忽略

RPUSHX

127.0.0.1:6379> RPUSHX mylist rr nn
(integer) 10
127.0.0.1:6379> LRANGE mylist 0 -1
 1) "ee"
 2) "ww"
 3) "nb"
 4) "21"
 5) "ddfff"
 6) "qq"
 7) "ll"
 8) "nn"
 9) "rr"
10) "nn"
127.0.0.1:6379> RPUSHX youlist rr nn
(integer) 0
127.0.0.1:6379> LRANGE youlist 0 -1
(empty array)

从list中指定的元素前/后插入一个新元素

LINSERT

127.0.0.1:6379> LRANGE mylist 0 -1
 1) "ee"
 2) "ww"
 3) "nb"
 4) "21"
 5) "ddfff"
 6) "qq"
 7) "ll"
 
 #在指定元素前插入新元素
127.0.0.1:6379> LINSERT mylist before ddfff www
(integer) 8
127.0.0.1:6379> LRANGE mylist 0 -1
 1) "ee"
 2) "ww"
 3) "nb"
 4) "21"
 5) "www"
 6) "ddfff"
 7) "qq"
 8) "ll"
 
 #在指定元素后插入新元素
 127.0.0.1:6379> LINSERT mylist after ddfff eee
(integer) 9
127.0.0.1:6379> LRANGE mylist 0 -1
 1) "ee"
 2) "ww"
 3) "nb"
 4) "21"
 5) "www"
 6) "ddfff"
 7) "eee"
 8) "qq"
 9) "ll"

删除数据

从列表左侧开始删除

LREM

127.0.0.1:6379> LRANGE mylist 0 -1
 1) "we"
 2) "fp"
 3) "we"
 4) "ed"
 5) "we"
 6) "om"
 7) "we"
 8) "ig"
 9) "we"
10) "rn"
11) "we"
12) "df"
127.0.0.1:6379> LREM mylist 2 we		#从列表左侧开始,删除2个we元素
(integer) 2
127.0.0.1:6379> LRANGE mylist 0 -1
 1) "fp"
 2) "ed"
 3) "we"
 4) "om"
 5) "we"
 6) "ig"
 7) "we"
 8) "rn"
 9) "we"
10) "df"

修改数据

LSET

127.0.0.1:6379> LRANGE mylist 0 -1
 1) "fp"		#第0个元素
 2) "ed"		#第1个元素
 3) "we"		#第2个元素
 4) "om"
 5) "we"
 6) "ig"
 7) "we"
 8) "rn"
 9) "we"
10) "df"
#将第1个元素修改为clearlove7
127.0.0.1:6379> LSET mylist 1 clearlove7
OK
127.0.0.1:6379> LRANGE mylist 0 -1
 1) "fp"
 2) "clearlove7"
 3) "we"
 4) "om"
 5) "we"
 6) "ig"
 7) "we"
 8) "rn"
 9) "we"
10) "df"

截取数据

LTRIM

127.0.0.1:6379> LRANGE mylist 0 -1
 1) "fp"
 2) "clearlove7"
 3) "we"
 4) "om"
 5) "we"
 6) "ig"
 7) "we"
 8) "rn"
 9) "we"
10) "df"
#截取列表第1到第4个元素,替换源列表
127.0.0.1:6379> LTRIM mylist 1 4
OK
127.0.0.1:6379> LRANGE mylist 0 -1
1) "clearlove7"
2) "we"
3) "om"
4) "we"

查看列表

查看数据

LRANGE

#查看全部元素
127.0.0.1:6379> LRANGE mylist 0 -1
1) "clearlove7"
2) "we"
3) "om"
4) "we"
#查看第1到第2个元素
127.0.0.1:6379> LRANGE mylist 1 2
1) "we"
2) "om"

根据下标查看某个元素

LINDE

127.0.0.1:6379> LRANGE mylist 0 -1
1) "clearlove7"
2) "we"
3) "om"
4) "we"
127.0.0.1:6379> LINDEX mylist 0
"clearlove7"

查看列表长度

LLEN

127.0.0.1:6379> LRANGE mylist 0 -1
1) "clearlove7"
2) "we"
3) "om"
4) "we"
127.0.0.1:6379> LLEN mylist
(integer) 4

从左边查看并删除元素

LPOP

127.0.0.1:6379> LRANGE mylist 0 -1
1) "clearlove7"
2) "we"
3) "om"
4) "we"
127.0.0.1:6379> LPOP mylist
"clearlove7"
127.0.0.1:6379> LRANGE mylist 0 -1
1) "we"
2) "om"
3) "we"

从右边查看并删除元素

RPOP

127.0.0.1:6379> LRANGE mylist 0 -1
1) "we"
2) "om"
3) "we"
127.0.0.1:6379> RPOP mylist
"we"
127.0.0.1:6379> LRANGE mylist 0 -1
1) "we"
2) "om"

查看一个元素并移动到另一个列表中

RPOPLPUSH

127.0.0.1:6379> LRANGE mylist 0 -1
1) "11"
2) "10"
3) "01"
4) "00"
5) "we"
6) "om"
127.0.0.1:6379> LRANGE testlist 0 -1
1) "ff"
2) "dd"
127.0.0.1:6379> RPOPLPUSH mylist testlist
"om"
127.0.0.1:6379> RPOPLPUSH mylist testlist
"we"
127.0.0.1:6379> LRANGE mylist 0 -1
1) "11"
2) "10"
3) "01"
4) "00"
127.0.0.1:6379> LRANGE testlist 0 -1
1) "we"
2) "om"
3) "ff"
4) "dd"

BLPOP

从列表左侧开始查询,返回列表的key和左侧第一个元素

若查询的列表中没有元素,则原地阻塞至设置的timeout过后,返回空

若在阻塞状态时,列表新增了元素,则返回列表的key和左侧第一个元素,并删除该元素

#服务器A
#查询列表,此时列表为空,阻塞并等待100秒
127.0.0.1:6379> BLPOP testlist 100

#服务器B
#此时有人向列表插入新元素
127.0.0.1:6379> LPUSH testlist we
(integer) 1

#服务器A
#检测到新增元素,则返回列表的key和左侧第一个元素,并删除该元素
127.0.0.1:6379> BLPOP testlist 100
1) "testlist"
2) "we"
(22.24s)

#服务器B
#新增元素后查看,发现元素被删除
127.0.0.1:6379> LRANGE testlist 0 -1
(empty array)
posted @ 2022-10-16 21:17  大胡萝卜没有须  阅读(59)  评论(0编辑  收藏  举报