list(数组)
队列,列表的子成员类型为string
lpush key value
rpush key value
linsert key after|before 指定元素 value
lindex key index
lrange key start stop
lset key index value
lrem key count value
127.0.0.1:6379> lpush names ww zs ls (integer) 3 127.0.0.1:6379> type names list 127.0.0.1:6379> lrange names 0 -1 1) "ls" 2) "zs" 3) "ww" 127.0.0.1:6379> rpush names a b c (integer) 6 127.0.0.1:6379> lrange names 0 -1 1) "ls" 2) "zs" 3) "ww" 4) "a" 5) "b" 6) "c" 127.0.0.1:6379> lpush names x y (integer) 8 127.0.0.1:6379> lrange names 0 -1 1) "y" 2) "x" 3) "ls" 4) "zs" 5) "ww" 6) "a" 7) "b" 8) "c"
(1)添加子成员
# 在左侧(前)添加一条或多条数据 lpush key value1 value2 ... # 在右侧(后)添加一条或多条数据 rpush key value1 value2 ... # 在指定元素的左边(前)/右边(后)插入一个或多个数据 linsert key before 指定元素 value1 value2 .... linsert key after 指定元素 value1 value2 ....
从键为brother
的列表左侧添加一个或多个数据liubei、guanyu、zhangfei
lpush brother liubei # [liubei] lpush brother guanyu zhangfei xiaoming # [xiaoming,zhangfei,guanyu,liubei]
从键为brother的列表右侧添加一个或多个数据,xiaohong,xiaobai,xiaohui
rpush brother xiaohong # [xiaoming,zhangfei,guanyu,liubei,xiaohong] rpush brother xiaobai xiaohui # [xiaoming,zhangfei,guanyu,liubei,xiaohong,xiaobai,xiaohui]
从key=brother的xiaohong的列表位置左侧添加一个数据,xiaoA,xiaoB
linsert brother before xiaohong xiaoA # [xiaoming,zhangfei,guanyu,liubei,xiaoA,xiaohong,xiaobai,xiaohui] linsert brother before xiaohong xiaoB # [xiaoming,zhangfei,guanyu,liubei,xiaoA,xiaoB,xiaohong,xiaobai,xiaohui]
从key=brother,key=xiaohong的列表位置右侧添加一个数据,xiaoC,xiaoD
linsert brother after xiaohong xiaoC # [xiaoming,zhangfei,guanyu,liubei,xiaoA,xiaohong,xiaoC,xiaobai,xiaohui] linsert brother after xiaohong xiaoD # [xiaoming,zhangfei,guanyu,liubei,xiaoA,xiaohong,xiaoD,xiaoC,xiaobai,xiaohui]
注意:当列表如果存在多个成员值一致的情况下,默认只识别第一个。
127.0.0.1:6379> linsert brother before xiaoA xiaohong # [xiaoming,zhangfei,guanyu,liubei,xiaohong,xiaoA,xiaohong,xiaoD,xiaoC,xiaobai,xiaohui] 127.0.0.1:6379> linsert brother before xiaohong xiaoE # [xiaoming,zhangfei,guanyu,liubei,xiaoE,xiaohong,xiaoA,xiaohong,xiaoD,xiaoC,xiaobai,xiaohui] 127.0.0.1:6379> linsert brother after xiaohong xiaoF # [xiaoming,zhangfei,guanyu,liubei,xiaoE,xiaohong,xiaoF,xiaoA,xiaohong,xiaoD,xiaoC,xiaobai,xiaohui]
(2)基于索引获取列表成员
根据指定的索引(下标)获取成员的值,负数下标从右边-1开始,逐个递减
lindex key index
获取brother下标为2以及-2的成员
del brother lpush brother guanyu zhangfei xiaoming lindex brother 2 # "guanyu" lindex brother -2 # "zhangfei"
(3)获取列表的切片
闭区间[包括stop]
lrange key start stop
操作:
del brother rpush brother liubei guanyu zhangfei xiaoming xaiohong # 获取btother的全部成员 lrange brother 0 -1 # 获取brother的前2个成员 lrange brother 0 1 # 获取brother的后2个成员 lrange brother -2 -1
127.0.0.1:6379> linsert names after "y" 10 (integer) 9 127.0.0.1:6379> lrange names 0 -1 1) "y" 2) "10" 3) "x" 4) "ls" 5) "zs" 6) "ww" 7) "a" 8) "b" 9) "c" 127.0.0.1:6379> linsert names before "y" 20 (integer) 10 127.0.0.1:6379> lrange names 0 -1 1) "20" 2) "y" 3) "10" 4) "x" 5) "ls" 6) "zs" 7) "ww" 8) "a" 9) "b" 10) "c" 127.0.0.1:6379> lindex names 3 "x" 127.0.0.1:6379> lindex names 0 "20" 127.0.0.1:6379> lindex names -1 "c" 127.0.0.1:6379> lrange names 0 1 1) "20" 2) "y" 127.0.0.1:6379> lrange names -2 -1 1) "b" 2) "c" 127.0.0.1:6379>
(4)获取列表的长度
llen key
获取brother列表的成员个数
llen brother
(5)按索引设置值
lset key index value # 注意: # redis的列表也有索引,从左往右,从0开始,逐一递增,第1个元素下标为0 # 索引可以是负数,表示尾部开始计数,如`-1`表示最后1个元素
修改键为brother
的列表中下标为4
的元素值为xiaohongmao
lset brother 4 xiaohonghong
(6)删除指定成员
移除并获取列表的第一个成员或最后一个成员
lpop key # 第一个成员出列 rpop key # 最后一个成员出列
获取并移除brother中的第一个成员
lpop brother # 开发中往往使用rpush和lpop实现队列的数据结构->实现入列和出列
lrem key count value # 注意: # count表示删除的数量,value表示要删除的成员。该命令默认表示将列表从左侧前count个value的元素移除 # count==0,表示删除列表所有值为value的成员 # count >0,表示删除列表左侧开始的前count个value成员 # count <0,表示删除列表右侧开始的前count个value成员 del brother rpush brother A B A C A lrem brother 0 A ["B","C"] del brother rpush brother A B A C A lrem brother -2 A ["A","B","C"] del brother rpush brother A B A C A lrem brother 2 A ["B","C","A"]
127.0.0.1:6379> llen names (integer) 10 127.0.0.1:6379> lset names 9 cc OK 127.0.0.1:6379> lrange names 0 -1 1) "20" 2) "y" 3) "10" 4) "x" 5) "ls" 6) "zs" 7) "ww" 8) "a" 9) "b" 10) "cc" 127.0.0.1:6379> lpop names "20" 127.0.0.1:6379> lrange names 0 -1 1) "y" 2) "10" 3) "x" 4) "ls" 5) "zs" 6) "ww" 7) "a" 8) "b" 9) "cc" 127.0.0.1:6379> rpop names "cc" 127.0.0.1:6379> lrange names 0 -1 1) "y" 2) "10" 3) "x" 4) "ls" 5) "zs" 6) "ww" 7) "a" 8) "b" 127.0.0.1:6379> rpush namaes x (integer) 1 127.0.0.1:6379> lrange names 0 -1 1) "y" 2) "10" 3) "x" 4) "ls" 5) "zs" 6) "ww" 7) "a" 8) "b" 127.0.0.1:6379> rpush names x (integer) 9 127.0.0.1:6379> lrange namaes 0 -1 1) "x" 127.0.0.1:6379> lrem names -1 x (integer) 1 127.0.0.1:6379> lrange names 0 -1 1) "y" 2) "10" 3) "x" 4) "ls" 5) "zs" 6) "ww" 7) "a" 8) "b" 127.0.0.1:6379>