redis命令入门(lists)

334 阅读5分钟

redis-cli 连接好之后,执行 help @list 查看 list 下的常用命令,看命令首字母,大概分为三类,L开头的代表left,R开头的代表right,B开头的代表block

1. LPUSH

LPUSH key element [element ...]

summary: Prepend one or multiple elements to a list

since: 1.0.0

将所有指定的值插入到存于 key 的列表的头部。如果 key 不存在,那么在进行 push 操作前会创建一个空列表,举例:

执行 lpush k1 a b c d e,结果会得到一个列表:

搭配RPOP命令,可以作为FIFO队列

2.RPUSH

RPUSH key element [element ...]

summary: Append one or multiple elements to a list

since: 1.0.0

和LPUSH命令相反执行rpush k1 a b c d e,结果是:

搭配LPOP命令,可以作为栈

3.LPOP

LPOP key

summary: Remove and get the first element in a list

since: 1.0.0

移除并返回 key 对应的 list 的首个元素

例如,先执行 rpush k1 a b c d e,再执行 lpop k1,会返回 a,并且k1的值为 b c d e

4.RPOP

RPOP key

summary: Remove and get the last element in a list

since: 1.0.0

移除并返回 key 对应的 list 的最后一个元素,与LPOP相反

5.LRANGE

LRANGE key start stop

summary: Get a range of elements from a list 

since: 1.0.0

返回指定key对应的list的start,end区间内的元素列表,支持反向索引

例如,执行rpush k1 a b c d e f,再执行 lrange k1 0 -1,会返回k1中list的所有元素

6.LLEN

LLEN key

summary: Get the length of a list

since: 1.0.0

返回指定key对应的list的长度

7.LINDEX

LINDEX key index

summary: Get an element from a list by its index

since: 1.0.0

返回指定index索引位置的元素,list本身是有索引的,所以也可以作为数组使用

8.LSET

LSET key index element

summary: Set the value of an element in a list by its index

since: 1.0.0

设置 index 位置的list元素的值为 value

9.LREM

LREM key count element

summary: Remove elements from a list

since: 1.0.0

删除指定 key 的 list 的前 count 次出现的值为 element 的元素 

  • count > 0: 从头往尾移除值为 value 的元素
  • count < 0: 从尾往头移除值为 value 的元素
  • count = 0: 移除所有值为 value 的元素

10.LTRIM

LTRIM key start stop

summary: Trim a list to the specified range

since: 1.0.0

截取指定 key 的 list,start和end是范围值,支持正反向索引

11.LPUSHX

LPUSHX key element [element ...] 

summary: Prepend an element to a list, only if the list exists 

since: 2.2.0

当 key 已经存在并且存着一个 list 的时候,在这个 key 下面的 list 的头部插入 value,当 key 不存在的时候不会进行任何操作

12.RPUSHX

RPUSHX key element [element ...]

summary: Append an element to a list, only if the list exists

since: 2.2.0

当 key 已经存在并且存着一个 list 的时候,在这个 key 下面的 list 的尾部插入 value,当 key 不存在的时候不会进行任何操作

13.RPOPLPUSH

RPOPLPUSH source destination

summary: Remove the last element in a list, prepend it to another list and return it

since: 1.2.0

移除存储在 source 的列表的最后一个元素,并把该元素放入存储在 destination 的列表的第一个元素位置,如果 destination 不存在,则创建一个空的列表进行lpush操作(原子性)

例如:假设 source 存储着列表 a,b,c, destination存储着列表 x,y,z;执行 RPOPLPUSH 得到的结果是 source 保存着列表 a,b ,而 destination 保存着列表 c,x,y,z

如果 source 不存在,那么会返回 nil 值,并且不会执行任何操作。 如果 source 和 destination 是相同的,那么这个操作等同于移除列表最后一个元素并且把该元素放在列表头部, 所以这个命令也可以当作是一个旋转列表的命令

14.BRPOPLPUSH

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

BRPOPLPUSHRPOPLPUSH的阻塞版本, timeout 为 0 表示无限期阻塞客户端

15.BLPOP

BLPOP key [key ...] timeout

summary: Remove and get the first element in a list, or block until one is available

since: 2.0.0

BLPOPLPOP的阻塞版本,当给定多个 key 参数时,按参数 key 的先后顺序依次检查各个列表,弹出第一个非空列表的头元素,**timeout 参数表示的是一个指定阻塞的最大秒数的整型值,**当 timeout 为 0 是表示阻塞时间无限制

16.BRPOP

BRPOP key [key ...] timeout 

summary: Remove and get the last element in a list, or block until one is available 

since: 2.0.0

RPOP的阻塞版本,和BLPOP操作的方向相反

17.LINSERT

LINSERT key BEFORE|AFTER pivot element

summary: Insert an element before or after another element in a list

since: 2.2.0

把 element 插入存于 key 的列表中在 第一个pivot(具体list中的某个元素) 的前面或后面

当 key 不存在时,这个list会被看作是空list,任何操作都不会发生,当 key 存在,但保存的不是一个list的时候,会返回error

举例:先执行 rpush k1 hello world world world,再执行 linsert k1 after world hehe,返回结果 hello world hello world world

18.LPOS

LPOS key element [RANK rank] [COUNT num-matches] [MAXLEN len]

summary: Return the index of matching elements on a list

since: 6.0.6

返回指定key对应的list中元素element出现的索引位置

rank:element 在 list 中第 rank(最小值为1) 次出现,例如:先执行 rpush k1 hello hello world world world world,再执行 lpos k1 world rank 2,表示在k1的list中world出现第2次的索引位置,返回3

num-matches:element 在 list 中前 count 个索引的位置,返回值是个列表,例如:LPOS k1 world count 3,返回2,3,4

len:表示 list 的区间范围内查询,例如:先执行 RPUSH k2 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 10 3,再执行LPOS k2 3 rank 2 count 2 maxlen 20,意思是说获取k2对应的list前20个元素中元素3第2(rank)次出现开始,匹配2次元素的索引位置,返回11,19,如果把maxlen 20 改成 maxlen 19,返回值为11,所以len为开区间