redis:list列表类型的操作

时间:2023-03-01 09:18:32

1. list列表类型的操作

1.1. lpush/rpush key value [value ...] 链表的头部(左侧)或尾部(右侧)插入值

语法:lpush key value [value ...]

作用:把值插入到列表左侧

语法:rpush key value [value ...]

作用:把值插入到列表右侧

返回值:返回列表的长度

例:

127.0.0.1:6379> lpush animal cat

(integer) 1

127.0.0.1:6379> lpush animal dog

(integer) 2

127.0.0.1:6379> rpush animal monkey

(integer) 3

1.2. lpop/rpop key

语法:lpop key

语法:rpop key

作用:返回并删除列表元素

返回值:删除的元素

例:

127.0.0.1:6379> rpush word apple banana cat dog

(integer) 4

127.0.0.1:6379> lrange word 0 -1

1) "apple"

2) "banana"

3) "cat"

4) "dog"

127.0.0.1:6379> lpop word

"apple"

127.0.0.1:6379> lrange word 0 -1

1) "banana"

2) "cat"

3) "dog"

127.0.0.1:6379> rpop word

"dog"

127.0.0.1:6379> lrange word 0 -1

1) "banana"

2) "cat"

1.3. lrange key start stop

语法:lrange key start stop

作用:返回列表中[start,stop]中的元素

注:左侧从0开始;右侧从-1开始

例:查看列表中所有的元素

127.0.0.1:6379> lrange animal 0 -1

1) "dog"

2) "cat"

3) "monkey"

127.0.0.1:6379> lpush num 1 2 3 4

(integer) 4

127.0.0.1:6379> lrange num 0 -1

1) "4"

2) "3"

3) "2"

4) "1"

127.0.0.1:6379> lrange num 2 3

1) "2"

2) "1"

127.0.0.1:6379> lrange num -2 3

1) "2"

2) "1"

127.0.0.1:6379> lrange num -1 -3   #

(empty list or set)

127.0.0.1:6379> lrange num -3 -1

1) "3"

2) "2"

3) "1"

1.4. lrem key count value

语法:lrem key count value

作用:从key列表中删除value值(从左到右)

注:删除count的绝对值个value后结束

Count>0 从左侧删除

Count<0 从右侧删除

例:

127.0.0.1:6379> lpush answer a b c a b d a

(integer) 7

127.0.0.1:6379> lrange answer 0 -1

1) "a"

2) "b"

3) "c"

4) "a"

5) "b"

6) "d"

7) "a"

127.0.0.1:6379> lrem answer 1 b  #删除第1个b

(integer) 1

127.0.0.1:6379> lrange answer 0 -1

1) "a"

2) "c"

3) "a"

4) "b"

5) "d"

6) "a"

127.0.0.1:6379> lrem answer -2 a  #删除最后2个a

(integer) 2

127.0.0.1:6379> lrange answer 0 -1

1) "a"

2) "c"

3) "b"

4) "d"

1.5. Itrim key start stop

语法:ltrim key start stop

作用:剪切key对应的列表,切[start,stop]一段,并把该段重新赋给key

例:

127.0.0.1:6379> rpush word apple banana cat dog

(integer) 4

127.0.0.1:6379> lrange word 0 -1

1) "apple"

2) "banana"

3) "cat"

4) "dog"

127.0.0.1:6379> ltrim word 1 3

OK

127.0.0.1:6379> lrange word 0 -1

1) "banana"

2) "cat"

3) "dog"

127.0.0.1:6379> rpush word a b c d e f

(integer) 6

127.0.0.1:6379> ltrim word 1 -2

OK

127.0.0.1:6379> lrange word 0 -1

1) "b"

2) "c"

3) "d"

4) "e"

127.0.0.1:6379> ltrim word -1 2

OK

127.0.0.1:6379> lrange word 0 -1   #

(empty list or set)

1.6. lindex key index

语法:lindex key index

作用:返回Index索引上的值

例:

127.0.0.1:6379> rpush fruit apple banana ogange pear peach

(integer) 5

127.0.0.1:6379> lindex fruit 0

"apple"

127.0.0.1:6379> lindex fruit 1

"banana"

127.0.0.1:6379> lindex fruit -1

"peach"

1.7. llen key

语法:llen key

作用:计算列表的元素个数

例:

127.0.0.1:6379> rpush fruit apple banana ogange pear peach

(integer) 5

127.0.0.1:6379> llen fruit

(integer) 5

1.8. linsert key BEFORE|AFTER pivot value

语法:linsert key BEFORE|AFTER pivot value

作用:在key列表中寻找pivot,并在pivot值之前/之后插入value

插入成功返回列表的长度

如果找不到pivot,插入失败,返回-1

注:一旦找到一个pivot后,命令就结束了,因此不会插入多个value

例:

127.0.0.1:6379> rpush num 1 3 5 7

(integer) 4

127.0.0.1:6379> lrange num 0 -1

1) "1"

2) "3"

3) "5"

4) "7"

127.0.0.1:6379> linsert num before 3 2

(integer) 5

127.0.0.1:6379> lrange num 0 -1

1) "1"

2) "2"

3) "3"

4) "5"

5) "7"

127.0.0.1:6379> linsert num after 7 8

(integer) 6

127.0.0.1:6379> lrange num 0 -1

1) "1"

2) "2"

3) "3"

4) "5"

5) "7"

6) "8"

127.0.0.1:6379> linsert num after 20 21

(integer) -1

127.0.0.1:6379> linsert num after 8 8

(integer) 7

127.0.0.1:6379> lrange num 0 -1

1) "1"

2) "2"

3) "3"

4) "5"

5) "7"

6) "8"

7) "8"

127.0.0.1:6379> linsert num after 8 10

(integer) 8

127.0.0.1:6379> lrange num 0 -1

1) "1"

2) "2"

3) "3"

4) "5"

5) "7"

6) "8"

7) "10"

8) "8"

1.9. rpoplpush source destination

语法:rpoplpush source destination

作用:把source的尾部(右侧)拿出来,放在desinationt的头部(左侧),并返回该单元值

场景:task+bak 双列表完成安全队列

业务逻辑:

rpoplpush task bak

接收返回值,并做业务处理

如果成功,rpop bak清除任务,如果不成功,下次从bak表里取任务

注:此命令实现的功能,用rpop、lpush两个命令也能实现,但是此命令是一个原子操作,如果用两个命令来实现不够原子性(如:rpop成功,lpush失败就没有rpoplpush更安全)

127.0.0.1:6379> rpush task a b c d

(integer) 4

127.0.0.1:6379> rpoplpush task job

"d"

127.0.0.1:6379> lrange task 0 -1

1) "a"

2) "b"

3) "c"

127.0.0.1:6379> lrange job 0 -1

1) "d"