redis数据结构-List

2023/3/31 redisdatabase

🌙 List类型

List类型和双向链表类似,既可以支持正向检索也可以支持反向检索,特征如下:

  • 有序
  • 元素可以重复
  • 插入和删除快
  • 查询速度一般

基于这些特征,List常用来存储一个有序数据,例如: 朋友圈点赞列表、评论列表等。

List常见命令有:

  • LPUSH key element...: 向列表左侧插入一个或多个元素
  • LPOP key: 移除并返回列表左侧的第一个元素,没有则返回nil
  • RPUSH key element...: 向列表右侧插入一个或多个元素
  • RPOP key: 移除并返回列表右侧的第一个元素,没有则返回nil
  • LRANGE key start end: 返回一段脚标范围内的所有元素
  • BLPOP和BRPOP: 与LPOP和RPOP类似,只不过在没有元素时等待指定时间,而不是直接返回nil

这些命令是不是和js中的数组api很类似呢

🌙 FAQ

🌙 1.如何利用List结构模拟一个栈?

栈:入口和出口在同一边

1.栈的特点是后进先出,所以我们可以使用LPUSH和LPOP命令来模拟栈的操作

2.将元素插入到列表的左侧,相当于将元素压入栈中

3.从列表的左侧弹出元素,相当于从栈中弹出元素

例如,创建一个名为stack的列表,可以使用以下命令来模拟栈的操作:

LPUSH stack element1
LPUSH stack element2
LPUSH stack element3
LPOP stack # 返回element3
LPOP stack # 返回element2
LPOP stack # 返回element1
1
2
3
4
5
6

🌙 2.如何利用List结构模拟一个队列?

队列:入口和出口在两端

1.队列的特点是先进先出,所以我们可以使用LPUSH和RPOP命令来模拟队列的操作

2.将元素插入到列表的右侧,相当于将元素加入队列的尾部

3.从列表的左侧弹出元素,相当于从队列的头部弹出元素

例如,创建一个名为queue的列表,可以使用以下命令来模拟队列的操作:

LPUSH queue element1
LPUSH queue element2
LPUSH queue element3
RPOP queue # 返回element1
RPOP queue # 返回element2
RPOP queue # 返回element3
1
2
3
4
5
6

🌙 3.如何利用List结构模拟一个阻塞队列?

阻塞队列:入口和出口在两端。

1.在阻塞队列为空时,获取元素的线程会被阻塞,直到队列中有新的元素被加入;在队列已满时,插入元素的线程会被阻塞,直到队列中有空的位置。

2.利用BLPOP和RPUSH命令来模拟阻塞队列的操作

3.使用RPUSH命令将元素插入到列表的右侧,相当于将元素加入队列的尾部 4.使用BLPOP命令从列表的左侧弹出元素,如果列表为空,则阻塞等待指定时间,直到有新的元素被加入

例如,创建一个名为blocking_queue的列表,可以使用以下命令来模拟阻塞队列的操作:

RPUSH blocking_queue element1
RPUSH blocking_queue element2
BLPOP blocking_queue 10 # 等待10秒,如果队列为空则阻塞等待
RPUSH blocking_queue element3
BLPOP blocking_queue 10 # 返回element1
BLPOP blocking_queue 10 # 返回element2
BLPOP blocking_queue 10 # 返回element3
BLPOP blocking_queue 10 # 队列为空,等待10秒后返回nil
1
2
3
4
5
6
7
8

查看list命令:

$ redis-cli
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 a value 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 value
  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

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

  LPUSHX key value
  summary: Prepend a value 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 value
  summary: Remove elements from a list
  since: 1.0.0

  LSET key index value
  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 value [value ...]
  summary: Append one or multiple values to a list
  since: 1.0.0

  RPUSHX key value
  summary: Append a value to a list, only if the list exists
  since: 2.2.0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71