Redis从入门到放弃03——List 数据类型

154 阅读7分钟

Redis从入门到放弃03——List 数据类型

List 数据类型介绍

Introduction to Redis lists

Redis lists are linked lists of string values. Redis lists are frequently used to:

Implement stacks and queues. Build queue management for background worker systems


Redis列表是字符串值的链表。Redis列表经常用于:

实现堆栈和队列。 为后台工作系统构建队列管理

limits

The max length of a Redis list is 2^32 - 1 (4,294,967,295) elements


Redis列表的最大长度是2^32 - 1(4,294,967,295)个元素。

常用 List 命令

查看List数据类型下所有命令: help @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 key element [element ...] :将一个或多个值插入到列表头部 ,返回插入到list 中的元素个数

127.0.0.1:6379> lpush list1 1 2 3 4 5 6 7
(integer) 7

LRANGE key start stop:获取列表指定范围内的元素

# 获取list中所有的元素
127.0.0.1:6379> lrange list1 0 -1
1) "7"
2) "6"
3) "5"
4) "4"
5) "3"
6) "2"
7) "1"
# 获取前三个
127.0.0.1:6379> lrange list1 0 2
1) "7"
2) "6"
3) "5"
# 获取前6个
127.0.0.1:6379> lrange list1 0 5
1) "7"
2) "6"
3) "5"
4) "4"
5) "3"
6) "2"

LPUSHX key element [element ...]:将一个或多个值插入到列表头部 ,返回插入到列表中的元素个数,仅当key存在的时候才能成功执行

127.0.0.1:6379> lpushx list2 1 2 3 4
(integer) 0
127.0.0.1:6379> lrange list2 0 -1
(empty array)

LPOP key [count] :移出列表中最前的一个或者多个元素并且返回移出的元素

127.0.0.1:6379> lpop list1
"7"
127.0.0.1:6379> lpop list1 2
1) "6"
2) "5"

LINDEX key index :通过索引下标查找列表中的元素

127.0.0.1:6379> lindex list1 1
"6"
127.0.0.1:6379> lindex list1 6
"1"

LSET key index element:通过索引设置列表元素的值

127.0.0.1:6379> lrange list1 0 -1
1) "4"
2) "3"
3) "2"
127.0.0.1:6379> lset list1 0 1
OK
127.0.0.1:6379> lrange list1 0 -1
1) "1"
2) "3"
3) "2"

LTRIM key start stop :对一个列表进行修剪(trim),就是说,让列表只保留指定区间内的元素,不在指定区间之内的元素都将被删除。

127.0.0.1:6379> lrange list2 0 -1
1) "1"
2) "d"
3) "c"
4) "b"
5) "a"
127.0.0.1:6379> ltrim list2 0 3
OK
127.0.0.1:6379> lrange list2 0 -1
1) "1"
2) "d"
3) "c"
4) "b"

LLEN key :返回列表中的元素个数

127.0.0.1:6379> llen list1
(integer) 7

LREM key count element :移除列表中的元素 (count 表示要移除列表中此元素的个数,element 表示要移除的列表中的元素,如果count 大于列表中包含此元素的个数,也只会移除列表中所有为这个值的元素)

127.0.0.1:6379> lpush list4 1 1 1 2 2 3 4 4 5
(integer) 9
127.0.0.1:6379> lrem list4 1 2
(integer) 1
127.0.0.1:6379> lrem list4 7 2
(integer) 1
127.0.0.1:6379> lrange list4 0 -1
1) "5"
2) "4"
3) "4"
4) "3"
5) "1"
6) "1"
7) "1"
127.0.0.1:6379> lrem list4 2 3
(integer) 1
127.0.0.1:6379> lrem list4 10 5
(integer) 1
127.0.0.1:6379> lrange list4 0 -1
1) "4"
2) "4"
3) "1"
4) "1"
5) "1"

LPOS key element [RANK rank] [COUNT num-matches] [MAXLEN len] :返回第一个或第N个元素(rank参数值)的下标,或者前N个元素的的位置下标

127.0.0.1:6379> RPUSH list5 a b c 1 2 3 c c
(integer) 8
---返回第一个'c'出现的位置下标
127.0.0.1:6379> lpos list 5 c
(error) ERR syntax error
127.0.0.1:6379> lpos list5 c
(integer) 2
---返回第二个'c'出现的位置下标
127.0.0.1:6379> RPUSH list5 a b c 1 2 3 c c
(integer) 8
127.0.0.1:6379> lpos list 5 c
(error) ERR syntax error
127.0.0.1:6379> lpos list5 c
(integer) 2
---返回最后一个'c'出现的位置下标
127.0.0.1:6379> lpos list5 c rank -1
(integer) 7
---返回第N个匹配元素,还想返回所有前N个匹配元素的位置
127.0.0.1:6379> lpos list5 c count 2
1) (integer) 2
2) (integer) 6
--组合COUNTRANKCOUNT将尝试返回指定数量的匹配,但从n个匹配开始,由RANK选项指定。
--从最后一个位置开始数,返回2'c'的位置下标
127.0.0.1:6379> lpos list5 c  rank -1 count 2
1) (integer) 7
2) (integer) 6

--COUNT可以指定0作为匹配的数目,找到的所有匹配作为索引数组返回,此处返回所有 'c'的位置下标
127.0.0.1:6379> lpos list5 c count 0
1) (integer) 2
2) (integer) 6
3) (integer) 7

LMOVE source destination LEFT|RIGHT LEFT|RIGHT:自动返回并删除存储在源的列表的第一个/最后一个元素(头/尾取决于wherefrom参数),并将该元素压入存储在目标的列表的第一个/最后一个元素(头/尾取决于whereto参数);现在替代了RPOPLPUSH

127.0.0.1:6379> rpush mylist one two three
(integer) 3
127.0.0.1:6379> lmove mylist myotherlist right left
"three"
127.0.0.1:6379> lrange myotherlist 0 -1
1) "three"
127.0.0.1:6379> lmove mylist myotherlist left right
"one"
127.0.0.1:6379> lrange myotherlist 0 -1
1) "three"
2) "one"
127.0.0.1:6379> lpos myotherlist one
(integer) 1
127.0.0.1:6379> lpos myotherlist three
(integer) 0
127.0.0.1:6379> lmove mylist myotherlist left left
"two"
127.0.0.1:6379> lrange myotherlist 0 -1
1) "two"
2) "three"
3) "one"

BLPOP key [key ...] timeout:是LPOP的阻塞版本,因为当没有元素可以从任何给定列表中弹出时,它会阻塞连接;参数 timeout是阻塞的超时时间, 为0时代表一直阻塞不会超时,当BLPOP阻塞并且指定了非零超时时,当指定的超时过期时,客户端将解除阻塞并返回nil多批量值,而无需对至少一个指定的键进行推送操作

127.0.0.1:6379> lpush blist a b c
(integer) 3
127.0.0.1:6379> blpop blist 0
1) "blist"
2) "c"
127.0.0.1:6379> blpop blist 0
1) "blist"
2) "b"
127.0.0.1:6379> blpop blist 0
1) "blist"
2) "a"
---阻塞中,直到下一个元素push进blist
127.0.0.1:6379> blpop blist 0

---另一个客户端登录后执行lpush命令
127.0.0.1:6379> lpush blist d
(integer) 1
127.0.0.1:6379> blpop blist 0
1) "blist"
2) "d"
(26.66s)

RPUSH key element [element ...]:在列表中添加一个或多个值到列表尾部,返回插入到列表中的元素个数

127.0.0.1:6379> rpush list2  1 2 3 4 5 6 7 
(integer) 7

RPUSHX key element [element ...]:在列表中添加一个或多个值到列表尾部,返回插入到列表中的元素个数,仅当key存在的时候才能执行成功

127.0.0.1:6379> rpushx list3 1 2 3 4
(integer) 0
127.0.0.1:6379> lrange list3 0 -1
(empty array)

RPOP key [count] :移除列表的最后一个元素,返回值为移除的元素

127.0.0.1:6379> rpush list4 1 2 3 4 5
(integer) 5
127.0.0.1:6379> rpop list4 
"5"

BRPOP key [key ...] timeout:是RPOP的阻塞版本,因为当没有元素可以从任何给定列表中弹出时,它会阻塞连接;参数 timeout是阻塞的超时时间, 为0时代表一直阻塞不会超时,当BRPOP阻塞并且指定了非零超时时,当指定的超时过期时,客户端将解除阻塞并返回nil多批量值,而无需对至少一个指定的键进行推送操作

127.0.0.1:6379> rpush brlist a b c
(integer) 3
127.0.0.1:6379> brpop brlist 0
1) "brlist"
2) "c"
127.0.0.1:6379> brpop brlist 0
1) "brlist"
2) "b"
127.0.0.1:6379> brpop brlist 0
1) "brlist"
2) "a"
---阻塞中,直到下一个元素push进blist
127.0.0.1:6379> brpop blist 0

---另一个客户端登录后执行lpush命令
127.0.0.1:6379> rpush blist d
(integer) 1
127.0.0.1:6379> brpop blist 0
1) "blist"
2) "d"
(19.06s)

RPOPLPUSH source destination :移除列表的最后一个元素,并将该元素添加到另一个列表并返回

127.0.0.1:6379> lrange list1 0 -1
1) "4"
2) "3"
3) "2"
4) "1"
127.0.0.1:6379> lrange list2 0 -1
1) "d"
2) "c"
3) "b"
4) "a"
127.0.0.1:6379> rpoplpush list1 list2
"1"
127.0.0.1:6379> lrange list2 0 -1
1) "1"
2) "d"
3) "c"
4) "b"
5) "a"

应用场景

  • 大V作者A和B发布了文章分别是 11 和 22我关注了他们两个,只要他们发布了新文章,就会推送进我的List: lpush likearticle:myid 11 22
  • 查看自己的号订阅的全部文章,类似分页,下面 0~9 就是一次显示10条:lrange likearticle:myid 0 9