Redis命令-List

lpush [key] [value1] [value2] … [valuen]

将一个或多个值插入给定key头部,如果key不存在,会创建再插入,当key存在且不是list类型时,会返回一个错误

Redis 2.4 版本以前只能插入单个值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 正常情况
127.0.0.1:6379> lpush lpush-key 1 2 3 4 5 6 7
(integer) 7
127.0.0.1:6379> lrange lpush-key 0 -1
1) "7"
2) "6"
3) "5"
4) "4"
5) "3"
6) "2"
7) "1"

# key存在且不是list类型
127.0.0.1:6379> set str-key 1
OK
127.0.0.1:6379> lpush str-key 2 3 4
(error) WRONGTYPE Operation against a key holding the wrong kind of value

lpushx [key] [value]

作用与lpush类似,区别在于插入的key必须存在,不存在时插入操作无效

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
# 正常情况
127.0.0.1:6379> lrange lpush-key 0 -1
1) "7"
2) "6"
3) "5"
4) "4"
5) "3"
6) "2"
7) "1"
127.0.0.1:6379> lpushx lpush-key 8
(integer) 8
127.0.0.1:6379> lrange lpush-key 0 -1
1) "8"
2) "7"
3) "6"
4) "5"
5) "4"
6) "3"
7) "2"
8) "1"

# key不存在
127.0.0.1:6379> exists lpush-key2
(integer) 0
127.0.0.1:6379> lpushx lpush-key2 1
(integer) 0
127.0.0.1:6379> lrange lpush-key2 0 -1
(empty list or set)

rpush [key] [value1] [value2] … [valuen]

作用与lpush类似,区别在于是从尾部插入

1
2
3
4
5
6
7
8
9
10
127.0.0.1:6379> rpush rpush-key 1 2 3 4 5 6 7
(integer) 7
127.0.0.1:6379> lrange rpush-key 0 -1
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
7) "7"

rpushx [key] [value]

作用与lpushx类似,区别在于是从尾部插入

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
127.0.0.1:6379> lrange rpush-key 0 -1
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
7) "7"
127.0.0.1:6379> rpushx rpush-key 8
(integer) 8
127.0.0.1:6379> lrange rpush-key 0 -1
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
7) "7"
8) "8"

# key不存在
127.0.0.1:6379> exists rpush-key2
(integer) 0
127.0.0.1:6379> rpushx rpush-key2 1
(integer) 0
127.0.0.1:6379> lrange rpush-key2 0 -1
(empty list or set)

llen [key]

获取列表长度

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

lpop [key]

移除并返回列表的第一个元素

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
127.0.0.1:6379> lpush lpop-key 1 2 3 4 5
(integer) 5
127.0.0.1:6379> lrange lpop-key 0 -1
1) "5"
2) "4"
3) "3"
4) "2"
5) "1"
127.0.0.1:6379> lpop lpop-key
"5"
127.0.0.1:6379> lrange lpop-key 0 -1
1) "4"
2) "3"
3) "2"
4) "1"

rpop [key]

移除并返回列表的最后一个元素

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
127.0.0.1:6379> lpush rpop-key 1 2 3 4 5
(integer) 5
127.0.0.1:6379> lrange rpop-key 0 -1
1) "5"
2) "4"
3) "3"
4) "2"
5) "1"
127.0.0.1:6379> rpop rpop-key
"1"
127.0.0.1:6379> lrange rpop-key 0 -1
1) "5"
2) "4"
3) "3"
4) "2"

lindex [key] [index]

通过索引获取列表中的元素,索引从0开始,可以是负数,-1表示最后一个元素,以此类推

如果索引超出列表范围,则返回nil

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
127.0.0.1:6379> lpush lindex-key 1 2 3 4 5
(integer) 5
127.0.0.1:6379> lrange lindex-key 0 -1
1) "5"
2) "4"
3) "3"
4) "2"
5) "1"
127.0.0.1:6379> lindex lindex-key 0
"5"
127.0.0.1:6379> lindex lindex-key -1
"1"
127.0.0.1:6379> lindex lindex-key -2
"2"
127.0.0.1:6379> lindex lindex-key 10
(nil)

lrange [key] [start] [stop]

获取给定key指定范围内的元素,索引从0开始,可以是负数,-1表示最后一个元素,以此类推

获取全部元素,start设定为0,stop设定为-1即可

所在范围内没有元素时返回(empty list or set)

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
127.0.0.1:6379> lpush lrange-key 1 2 3 4 5 6 7
(integer) 7
127.0.0.1:6379> lrange lrange-key 0 -1
1) "7"
2) "6"
3) "5"
4) "4"
5) "3"
6) "2"
7) "1"
127.0.0.1:6379> lrange lrange-key 1 3
1) "6"
2) "5"
3) "4"
127.0.0.1:6379> lrange lrange-key -6 1
1) "6"
127.0.0.1:6379> lrange lrange-key 0 10
1) "7"
2) "6"
3) "5"
4) "4"
5) "3"
6) "2"
7) "1"
127.0.0.1:6379> lrange lrange-key 8 10
(empty list or set)

lrem [key] [count] [value]

根据参数count的值,移除列表中与value相等的元素

count>0:从表头开始搜索移除,数量为count

count<0:从表尾开始搜索移除,数量为count绝对值

count=0:移除列表中所有与value值相等的元素

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
127.0.0.1:6379> lpush lrem-key 1 3 4 1 4 6 1 9 1 0 1 1 7
(integer) 13
127.0.0.1:6379> lrange lrem-key 0 -1
1) "7"
2) "1"
3) "1"
4) "0"
5) "1"
6) "9"
7) "1"
8) "6"
9) "4"
10) "1"
11) "4"
12) "3"
13) "1"
127.0.0.1:6379> lrem lrem-key 2 1
(integer) 2
127.0.0.1:6379> lrange lrem-key 0 -1
1) "7"
2) "0"
3) "1"
4) "9"
5) "1"
6) "6"
7) "4"
8) "1"
9) "4"
10) "3"
11) "1"
127.0.0.1:6379> lrem lrem-key -2 1
(integer) 2
127.0.0.1:6379> lrange lrem-key 0 -1
1) "7"
2) "0"
3) "1"
4) "9"
5) "1"
6) "6"
7) "4"
8) "4"
9) "3"
127.0.0.1:6379> lrem lrem-key 0 1
(integer) 2
127.0.0.1:6379> lrange lrem-key 0 -1
1) "7"
2) "0"
3) "9"
4) "6"
5) "4"
6) "4"
7) "3"

lset [key] [index] [value]

为指定key设置指定索引的值,如果索引超出范围或者key不存在则返回一个错误

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 正常情况
127.0.0.1:6379> lpush lset-key 1 2 3
(integer) 3
127.0.0.1:6379> lrange lset-key 0 -1
1) "3"
2) "2"
3) "1"
127.0.0.1:6379> lset lset-key 0 hello
OK
127.0.0.1:6379> lrange lset-key 0 -1
1) "hello"
2) "2"
3) "1"

# 索引超出范围
127.0.0.1:6379> lset lset-key 5 redis
(error) ERR index out of range

# key不存在
127.0.0.1:6379> exists lset-key2
(integer) 0
127.0.0.1:6379> lset lset-key2 0 hello
(error) ERR no such key

linsert [key] [before|after] pivot value

在给定key列表中指定元素前面|后面插入元素value

当列表中指定元素存在多个时,处理标准是第一个相同元素

指定元素不存在于列表时,不执行任何操作

列表不存在或列表为空时,不执行任何操作

给定key不是列表时,返回一个错误

执行成功返回列表的长度,如果没有找到指定的元素则返回-1,如果key不存在或是空列表则返回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
72
73
# 正常情况
127.0.0.1:6379> lpush linsert-key a b c d e a b c a
(integer) 9
127.0.0.1:6379> lrange linsert-key 0 -1
1) "a"
2) "c"
3) "b"
4) "a"
5) "e"
6) "d"
7) "c"
8) "b"
9) "a"
127.0.0.1:6379> linsert linsert-key a f
(error) ERR wrong number of arguments for 'linsert' command
127.0.0.1:6379> linsert linsert-key before a f
(integer) 10
127.0.0.1:6379> lrange linsert-key 0 -1
1) "f"
2) "a"
3) "c"
4) "b"
5) "a"
6) "e"
7) "d"
8) "c"
9) "b"
10) "a"
127.0.0.1:6379> linsert linsert-key after a f
(integer) 11
127.0.0.1:6379> lrange linsert-key 0 -1
1) "f"
2) "a"
3) "f"
4) "c"
5) "b"
6) "a"
7) "e"
8) "d"
9) "c"
10) "b"
11) "a"

# 没有相同元素
127.0.0.1:6379> linsert linsert-key before g f
(integer) -1
127.0.0.1:6379> linsert linsert-key after g f
(integer) -1

# key不存在
127.0.0.1:6379> exists linsert-key2
(integer) 0
127.0.0.1:6379> linsert linsert-key2 before a f
(integer) 0
127.0.0.1:6379> linsert linsert-key2 after a f
(integer) 0

# 列表为空
127.0.0.1:6379> ltrim linsert-key 15 20
OK
127.0.0.1:6379> lrange linsert-key 0 -1
(empty list or set)
127.0.0.1:6379> linsert linsert-key after g f
(integer) 0


# 给定key不是列表
127.0.0.1:6379> sadd linsert-key4 1
(integer) 1
127.0.0.1:6379> type linsert-key4
set
127.0.0.1:6379> linsert linsert-key4 after g f
(error) WRONGTYPE Operation against a key holding the wrong kind of value

ltrim [key] [start] [stop]

对列表进行修剪,只保留索引范围内的元素,其余元素都会被清除,索引从0开始,可以是负数,-1表示最后一个元素,以此类推,如果范围内没有元素,则会清除所有元素.

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
127.0.0.1:6379> lpush ltrim-key 1 2 3 4 5 6 7
(integer) 7
127.0.0.1:6379> lrange ltrim-key 0 -1
1) "7"
2) "6"
3) "5"
4) "4"
5) "3"
6) "2"
7) "1"
127.0.0.1:6379> ltrim ltrim-key 1 5
OK
127.0.0.1:6379> lrange ltrim-key 0 -1
1) "6"
2) "5"
3) "4"
4) "3"
5) "2"
127.0.0.1:6379> ltrim ltrim-key 5 8
OK
127.0.0.1:6379> lrange ltrim-key 0 -1
(empty list or set)
127.0.0.1:6379> lpush ltrim-key2 1 2 3 4 5 6 7
(integer) 7
127.0.0.1:6379> ltrim ltrim-key2 -5 5
OK
127.0.0.1:6379> lrange ltrim-key2 0 -1
1) "5"
2) "4"
3) "3"
4) "2"

rpoplpush [source-key] [destination-key]

移除源key的最后一个元素,将元素添加到目标key并返回

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
127.0.0.1:6379> lpush rpoplpush-key 1 2 3 4 5
(integer) 5
127.0.0.1:6379> lrange rpoplpush-key 0 -1
1) "5"
2) "4"
3) "3"
4) "2"
5) "1"
127.0.0.1:6379> exists rpoplpush-key2
(integer) 0
127.0.0.1:6379> rpoplpush rpoplpush-key rpoplpush-key2
"1"
127.0.0.1:6379> lrange rpoplpush-key 0 -1
1) "5"
2) "4"
3) "3"
4) "2"
127.0.0.1:6379> lrange rpoplpush-key2 0 -1
1) "1"

blpop [key1] [key2] … [keyn] [timeout]

移除并获取列表的第一个元素,如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素

如果列表为空,返回一个 nil 。 否则,返回一个含有两个元素的列表,第一个元素是被弹出元素所属的 key ,第二个元素是被弹出元素的值。

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
127.0.0.1:6379> lpush blpop-key 1 2 3 4 5
(integer) 5
127.0.0.1:6379> lpush blpop-key2 a b c d e
(integer) 5
127.0.0.1:6379> lrange blpop-key 0 -1
1) "5"
2) "4"
3) "3"
4) "2"
5) "1"
127.0.0.1:6379> lrange blpop-key2 0 -1
1) "e"
2) "d"
3) "c"
4) "b"
5) "a"
127.0.0.1:6379> blpop blpop-key blpop-key2 20
1) "blpop-key"
2) "5"
127.0.0.1:6379> blpop blpop-key2 blpop-key 20
1) "blpop-key2"
2) "e"
127.0.0.1:6379> lrange blpop-key 0 -1
1) "4"
2) "3"
3) "2"
4) "1"
127.0.0.1:6379> lrange blpop-key2 0 -1
1) "d"
2) "c"
3) "b"
4) "a"
127.0.0.1:6379> blpop blpop-key2 20
1) "blpop-key2"
2) "d"
# 列表没有元素阻塞了列表,过了超时时间后返回nil
127.0.0.1:6379> blpop blpop-key3 20
(nil)
(20.09s)
# 列表没有元素阻塞了列表,用另外一个连接lpush元素后弹出了第一个元素
127.0.0.1:6379> blpop blpop-key3 20
1) "blpop-key3"
2) "11"
(9.03s)

brpop [key1] [key2] … [keyn] [timeout]

作用与blpop类似,区别在于是弹出最后一个元素

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
127.0.0.1:6379> lpush brpop-key 1 2 3 4 5
(integer) 5
127.0.0.1:6379> lrange brpop-key 0 -1
1) "5"
2) "4"
3) "3"
4) "2"
5) "1"
127.0.0.1:6379> brpop brpop-key 20
1) "brpop-key"
2) "1"
127.0.0.1:6379> lrange brpop-key 0 -1
1) "5"
2) "4"
3) "3"
4) "2"

brpoplpush [source-key] [destination-key] [timeout]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
127.0.0.1:6379> lpush brpoplpush-key 1 2 3 4 5
(integer) 5
127.0.0.1:6379> lrange brpoplpush-key 0 -1
1) "5"
2) "4"
3) "3"
4) "2"
5) "1"
127.0.0.1:6379> brpoplpush brpoplpush-key brpoplpush-key2 20
"1"
127.0.0.1:6379> lrange brpoplpush-key 0 -1
1) "5"
2) "4"
3) "3"
4) "2"
127.0.0.1:6379> lrange brpoplpush-key2 0 -1
1) "1"

本文作者:Jormin
本文地址https://blog.lerzen.com/2018/01/16/redis命令-list/
版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 CN 许可协议。转载请注明出处!

----- 到这结束咯 感谢您的阅读 -----