redis

Redis学习笔记系列(四)——基本数据类型之列表

2020-10-07  本文已影响0人  复旦猿

5.2 列表(list)

5.2.1 LPUSH / RPUSH / LPUSHX / RPUSHX / LINSERT
# 将a b c 依次插入队首
127.0.0.1:6379> LPUSH list1 a b c
(integer) 3
127.0.0.1:6379> LRANGE list1 0 -1
1) "c"
2) "b"
3) "a"
# 将d e f 依次插入队尾
127.0.0.1:6379> RPUSH list2 d e f
(integer) 3
127.0.0.1:6379> LRANGE list2 0 -1
1) "d"
2) "e"
3) "f"
# list1 存在,因此插入成功,返回6
127.0.0.1:6379> LPUSHX list1 d e f
(integer) 6
127.0.0.1:6379> LRANGE list1 0 -1
1) "f"
2) "e"
3) "d"
4) "c"
5) "b"
6) "a"
# list 不存在,因此插入失败,返回0
127.0.0.1:6379> RPUSHX list d e f
(integer) 0
127.0.0.1:6379> LPUSH list a b c
(integer) 3
127.0.0.1:6379> LINSERT list BEFORE a 1
(integer) 4
127.0.0.1:6379> LRANGE list 0 -1
1) "c"
2) "b"
3) "1"
4) "a"
5.2.2 LPOP / RPOP / BLPOP / BRPOP
127.0.0.1:6379> LPUSH list a b c
(integer) 3
127.0.0.1:6379> LRANGE list 0 -1
1) "c"
2) "b"
3) "a"
127.0.0.1:6379> LPOP list
"c"
127.0.0.1:6379> RPOP list
"a"
127.0.0.1:6379> LPOP list
"b"
127.0.0.1:6379> LRANGE list 0 -1
(empty array)
127.0.0.1:6379> LPUSH list a b c
(integer) 3
127.0.0.1:6379> BLPOP list 1
1) "list"
2) "c"
127.0.0.1:6379> BLPOP list 1
1) "list"
2) "b"
127.0.0.1:6379> BLPOP list 1
1) "list"
2) "a"
127.0.0.1:6379> BLPOP list 1
(nil)
(1.10s)
# 打开一个新的redis-cli,插入元素后,返回
127.0.0.1:6379> BLPOP list 0
1) "list"
2) "c"
(27.70s)
5.2.3 RPOPLPUSH / BRPOPLPUSH
127.0.0.1:6379> LPUSH list1 a b c
(integer) 3
127.0.0.1:6379> RPOPLPUSH list1 list2
"a"
127.0.0.1:6379> LRANGE list1 0 -1
1) "c"
2) "b"
127.0.0.1:6379> LRANGE list2 0 -1
1) "a"
127.0.0.1:6379> RPOPLPUSH list1 list1
"b"
127.0.0.1:6379> LRANGE list1 0 -1
1) "b"
2) "c"
127.0.0.1:6379> BRPOPLPUSH list1 list2 5
"c"
127.0.0.1:6379> BRPOPLPUSH list1 list2 5
"b"
127.0.0.1:6379> BRPOPLPUSH list1 list2 5
(nil)
(5.00s)
127.0.0.1:6379> BRPOPLPUSH list1 list2 0
"a"
(4.70s)
5.2.4 LRANGE / LPOS / LINDEX / LLEN
127.0.0.1:6379> LPUSH list a b c
(integer) 3
# 从第0个到倒数第1个,即所有元素
127.0.0.1:6379> LRANGE list 0 -1
1) "c"
2) "b"
3) "a"
127.0.0.1:6379> LRANGE list 0 1000
1) "c"
2) "b"
3) "a"
127.0.0.1:6379> LRANGE list -2 -1
1) "b"
2) "a"
127.0.0.1:6379> LRANGE list -2 0
(empty array)
127.0.0.1:6379> LPUSH list a b c a d e f
(integer) 7
127.0.0.1:6379> LPOS list a RANK 1
(integer) 3
127.0.0.1:6379> LPOS list a RANK 2
(integer) 6
127.0.0.1:6379> LPOS list a COUNT 2
1) (integer) 3
2) (integer) 6
# 从第2个a开始,输出3个a的位置。
127.0.0.1:6379> LPOS list a RANK 2 COUNT 3
1) (integer) 6
# 获取前7个元素中的3个a的位置列表,只有2个a,因此,返回 3 和 6
127.0.0.1:6379> LPOS list a COUNT 3 MAXLEN 7
1) (integer) 3
2) (integer) 6
# 获取前4个元素中的3个a的位置列表,只有1个a,因此,返回 3
127.0.0.1:6379> LPOS list a COUNT 3 MAXLEN 4
1) (integer) 3
127.0.0.1:6379> LPUSH list a b c
(integer) 3
127.0.0.1:6379> LINDEX list 0
"c"
127.0.0.1:6379> LINDEX list 1
"b"
127.0.0.1:6379> LINDEX list -1
"a"
127.0.0.1:6379> LINDEX list 5
(nil)
127.0.0.1:6379> LPUSH list a b c
(integer) 3
127.0.0.1:6379> LLEN list
(integer) 3
127.0.0.1:6379> LLEN list1
(integer) 0
127.0.0.1:6379> SET k1 v1
OK
127.0.0.1:6379> LLEN k1
(error) WRONGTYPE Operation against a key holding the wrong kind of value
5.2.5 LTRIM
127.0.0.1:6379> LPUSH list a b c d
(integer) 4
127.0.0.1:6379> LPUSH list e f g
(integer) 7
127.0.0.1:6379> LTRIM list 0 5
OK
127.0.0.1:6379> LRANGE list 0 -1
1) "g"
2) "f"
3) "e"
4) "d"
5) "c"
6) "b"
127.0.0.1:6379> LPUSH list h i j
(integer) 9
127.0.0.1:6379>  LTRIM list 0 5
OK
127.0.0.1:6379> LRANGE list 0 -1
1) "j"
2) "i"
3) "h"
4) "g"
5) "f"
6) "e"
127.0.0.1:6379> LTRIM list 100 10000
OK
127.0.0.1:6379> LRANGE list 0 -1
(empty array)
5.2.6 LSET

*LSET
LSET命令用于设置列表中指定位置的元素,当索引不在列表的范围内时将返回错误。与LINDEX相同,索引从0开始,且负数值代表相对于队尾的位置。
LSET key index element

127.0.0.1:6379> LPUSH list a b c
(integer) 3
127.0.0.1:6379> LSET list 0 d
OK
127.0.0.1:6379> LSET list -1 f
OK
127.0.0.1:6379> LRANGE list 0 -1
1) "d"
2) "b"
3) "f"
5.2.7 LREM
127.0.0.1:6379> LPUSH list a b a a c d e d
(integer) 8
# 移除所有a
127.0.0.1:6379> LREM list 0 a
(integer) 3
127.0.0.1:6379> LRANGE list 0 -1
1) "d"
2) "e"
3) "d"
4) "c"
5) "b"
# 从队首开始移除1个d
127.0.0.1:6379> LREM list 1 d
(integer) 1
127.0.0.1:6379> LRANGE list 1 -1
1) "d"
2) "c"
3) "b"

写在最后

如果你觉得我写的文章帮到了你,欢迎点赞、评论、分享、赞赏哦,你们的鼓励是我不断创作的动力~

上一篇 下一篇

猜你喜欢

热点阅读