Redis列表是简单的字符串列表,按照插入顺序排序。你可以添加一个元素到列表的头部(左边)或者尾部(右边)
一个列表最多可以包含 232 - 1 个元素 (4294967295, 每个列表超过40亿个元素)。
一、将一个或多个值插入到列表头部
命令:lpush
格式:lpush key value1 [value2] ...
127.0.0.1:6379> lpush list_key 1 (integer) 1 127.0.0.1:6379> lpush list_key 2 3 4 (integer) 4
二、获取列表指定范围内的元素
命令:lrange
格式:lrange key begin_index end_index
127.0.0.1:6379> lpush list_key 1 (integer) 1 127.0.0.1:6379> lpush list_key 2 3 4 (integer) 4 127.0.0.1:6379> lrange list_key 0 1 1) "4" 2) "3" 127.0.0.1:6379> lrange list_key 0 0 1) "4" 127.0.0.1:6379> lrange list_key 2 3 1) "2" 2) "1" 127.0.0.1:6379> lrange list_key 0 10 1) "4" 2) "3" 3) "2" 4) "1"
三、将一个值插入到已存在的列表头部
命令:lpushx
格式:lpushx key value
127.0.0.1:6379> del list_key (integer) 1 127.0.0.1:6379> lrange list_key 0 10 (empty list or set) 127.0.0.1:6379> lpushx list_key 1 (integer) 0 127.0.0.1:6379> lrange list_key 0 10 (empty list or set) 127.0.0.1:6379> lpush list_key 1 (integer) 1 127.0.0.1:6379> lrange list_key 0 10 1) "1" 127.0.0.1:6379> lpushx list_key 2 (integer) 2 127.0.0.1:6379> lrange list_key 0 10 1) "2" 2) "1"
四、将一个或多个值插入到列表尾部
命令:rpush
格式:rpush key value1 [value2] ...
127.0.0.1:6379> lrange list_key 0 10 1) "2" 2) "1" 127.0.0.1:6379> lpush list_key 3 (integer) 3 127.0.0.1:6379> lrange list_key 0 10 1) "3" 2) "2" 3) "1" 127.0.0.1:6379> rpush list_key 4 (integer) 4 127.0.0.1:6379> lrange list_key 0 10 1) "3" 2) "2" 3) "1" 4) "4"
五、将一个值插入到已存在的列表尾部
命令:rpushx
格式:rpushx key value
127.0.0.1:6379> del list_key (integer) 1 127.0.0.1:6379> lrange list_key 0 10 (empty list or set) 127.0.0.1:6379> rpushx list_key 1 (integer) 0 127.0.0.1:6379> lrange list_key 0 10 (empty list or set) 127.0.0.1:6379> rpush list_key 1 (integer) 1 127.0.0.1:6379> lrange list_key 0 10 1) "1" 127.0.0.1:6379> rpushx list_key 2 (integer) 2 127.0.0.1:6379> lrange list_key 0 10 1) "1" 2) "2"
六、移出并获取列表的第一个元素
命令:lpop
格式:lpop key
127.0.0.1:6379> del list_key (integer) 1 127.0.0.1:6379> lpush list_key 1 2 3 4 5 (integer) 5 127.0.0.1:6379> lrange list_key 0 10 1) "5" 2) "4" 3) "3" 4) "2" 5) "1" 127.0.0.1:6379> lpop list_key "5" 127.0.0.1:6379> lrange list_key 0 10 1) "4" 2) "3" 3) "2" 4) "1"
七、移出并获取列表的最后一个元素
命令:rpop
格式:rpop key
127.0.0.1:6379> lrange list_key 0 10 1) "4" 2) "3" 3) "2" 4) "1" 127.0.0.1:6379> rpop list_key "1" 127.0.0.1:6379> lrange list_key 0 10 1) "4" 2) "3" 3) "2"
八、获取列表长度
命令:llen
格式:llen key
127.0.0.1:6379> lrange list_key 0 10 1) "4" 2) "3" 3) "2" 127.0.0.1:6379> llen list_key (integer) 3 127.0.0.1:6379> lpush list_key 5 6 7 (integer) 6 127.0.0.1:6379> lrange list_key 0 10 1) "7" 2) "6" 3) "5" 4) "4" 5) "3" 6) "2" 127.0.0.1:6379> llen list_key (integer) 6
九、通过索引获取列表中的元素
命令:lindex
格式:lindex key index
127.0.0.1:6379> lrange list_key 0 10 1) "7" 2) "6" 3) "5" 4) "4" 5) "3" 6) "2" 127.0.0.1:6379> lindex list_key 4 "3" 127.0.0.1:6379> lindex list_key 0 "7" 127.0.0.1:6379> lindex list_key 7 (nil) 127.0.0.1:6379> lindex list_key 6 (nil) 127.0.0.1:6379> lindex list_key 5 "2"
十、移出并获取列表的第一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止
命令:blpop
格式:blpop key1 [key2] timeout
对于有元素的list:
127.0.0.1:6379> lpush l1 1 2 3 4 (integer) 4 127.0.0.1:6379> lpush l2 a b c d (integer) 4 127.0.0.1:6379> lrange l1 0 10 1) "4" 2) "3" 3) "2" 4) "1" 127.0.0.1:6379> lrange l2 0 10 1) "d" 2) "c" 3) "b" 4) "a" 127.0.0.1:6379> blpop l1 l2 1 1) "l1" 2) "4" 127.0.0.1:6379> blpop l1 l2 1 1) "l1" 2) "3" 127.0.0.1:6379> blpop l1 l2 1 1) "l1" 2) "2" 127.0.0.1:6379> blpop l1 l2 1 1) "l1" 2) "1" 127.0.0.1:6379> blpop l1 l2 1 1) "l2" 2) "d" 127.0.0.1:6379> blpop l1 l2 1 1) "l2" 2) "c" 127.0.0.1:6379> blpop l1 l2 1 1) "l2" 2) "b" 127.0.0.1:6379> blpop l1 l2 1 1) "l2" 2) "a" 127.0.0.1:6379> blpop l1 l2 1 //1s后 (nil) (1.05s)
对于无元素的list:
127.0.0.1:6379> blpop l1 l2 1 //1s后 (nil) (1.06s)
127.0.0.1:6379> blpop l1 l2 10 //另一客户端调用 lpush l1 1 1) "l1" 2) "1" (1.54s)
127.0.0.1:6379> blpop l1 l2 0 //一直等待
十一、移出并获取列表的最后一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止
命令:brpop
格式:brpop key1 [key2] timeout
操作与blpop相同,此处略
十二、移除列表的最后一个元素,并将该元素添加到另一个列表并返回
命令:rpoplpush
格式:rpoplpush source target
127.0.0.1:6379> del l1 l2 (integer) 1 127.0.0.1:6379> lpush l1 1 2 3 (integer) 3 127.0.0.1:6379> lrange l1 0 10 1) "3" 2) "2" 3) "1" 127.0.0.1:6379> lrange l2 0 10 (empty list or set) 127.0.0.1:6379> rpoplpush l1 l2 "1" 127.0.0.1:6379> lrange l1 0 10 1) "3" 2) "2" 127.0.0.1:6379> lrange l2 0 10 1) "1" 127.0.0.1:6379> rpoplpush l1 l2 "2" 127.0.0.1:6379> rpoplpush l1 l2 "3" 127.0.0.1:6379> rpoplpush l1 l2 (nil) 127.0.0.1:6379> lrange l1 0 10 (empty list or set) 127.0.0.1:6379> lrange l2 0 10 1) "3" 2) "2" 3) "1"
十三、从列表中弹出一个值,将弹出的元素插入到另外一个列表中并返回它; 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止
命令:brpoplpush
格式:brpoplpush source target timeout
source有元素时:
127.0.0.1:6379> del l1 l2 (integer) 0 127.0.0.1:6379> lpush l1 1 2 3 (integer) 3 127.0.0.1:6379> lrange l1 0 10 1) "3" 2) "2" 3) "1" 127.0.0.1:6379> lrange l2 0 10 (empty list or set) 127.0.0.1:6379> brpoplpush l1 l2 1 "1" 127.0.0.1:6379> lrange l1 0 10 1) "3" 2) "2" 127.0.0.1:6379> lrange l2 0 10 1) "1" 127.0.0.1:6379> brpoplpush l1 l2 1 "2" 127.0.0.1:6379> brpoplpush l1 l2 1 "3" 127.0.0.1:6379> brpoplpush l1 l2 1 //1s后 (nil) (1.06s) 127.0.0.1:6379> lrange l1 0 10 (empty list or set) 127.0.0.1:6379> lrange l2 0 10 1) "3" 2) "2" 3) "1"
source无元素时,与blpop brpop类似
十四、在列表的第一个指定元素pivot前或者后插入元素value
命令:linsert
格式:linsert key before|after pivot value
127.0.0.1:6379> lpush list_key 1 2 1 3 1 4 (integer) 6 127.0.0.1:6379> lrange list_key 0 10 1) "4" 2) "1" //第一个指定元素"1" 3) "3" 4) "1" 5) "2" 6) "1" 127.0.0.1:6379> linsert list_key after 1 5 (integer) 7 127.0.0.1:6379> lrange list_key 0 10 1) "4" 2) "1" //第一个指定元素"1" 3) "5" 4) "3" 5) "1" 6) "2" 7) "1" 127.0.0.1:6379> linsert list_key before 1 6 (integer) 8 127.0.0.1:6379> lrange list_key 0 10 1) "4" 2) "6" 3) "1" //第一个指定元素"1" 4) "5" 5) "3" 6) "1" 7) "2" 8) "1"
十五、从列表头部开始,移除count个指定元素value
命令:lrem
格式:lrem key count value
127.0.0.1:6379> lrange list_key 0 10 1) "4" 2) "6" 3) "1" 4) "5" 5) "3" 6) "1" 7) "2" 8) "1" 127.0.0.1:6379> lrem list_key 1 1 (integer) 1 127.0.0.1:6379> lrange list_key 0 10 1) "4" 2) "6" 3) "5" 4) "3" 5) "1" 6) "2" 7) "1" 127.0.0.1:6379> lrem list_key 2 1 (integer) 2 127.0.0.1:6379> lrange list_key 0 10 1) "4" 2) "6" 3) "5" 4) "3" 5) "2" 127.0.0.1:6379> lrem list_key 1 1 (integer) 0 127.0.0.1:6379> lrange list_key 0 10 1) "4" 2) "6" 3) "5" 4) "3" 5) "2"
十六、通过索引设置列表元素的值
命令:lset
格式:lset key index value
127.0.0.1:6379> rpush list_key 1 2 3 4 5 (integer) 5 127.0.0.1:6379> lrange list_key 0 10 1) "1" 2) "2" 3) "3" 4) "4" 5) "5" 127.0.0.1:6379> lset list_key 0 10 OK 127.0.0.1:6379> lrange list_key 0 10 1) "10" 2) "2" 3) "3" 4) "4" 5) "5" 127.0.0.1:6379> lset list_key 3 10 OK 127.0.0.1:6379> lrange list_key 0 10 1) "10" 2) "2" 3) "3" 4) "10" 5) "5"
十七、对一个列表进行修剪,删除指定下标区间[start,stop]之外的元素
命令:ltrim
格式:ltrim key start stop
127.0.0.1:6379> del list_key (integer) 1 127.0.0.1:6379> rpush list_key 1 2 3 4 5 6 7 8 9 (integer) 9 127.0.0.1:6379> lrange list_key 0 10 1) "1" 2) "2" 3) "3" 4) "4" 5) "5" 6) "6" 7) "7" 8) "8" 9) "9" 127.0.0.1:6379> ltrim list_key 3 6 OK 127.0.0.1:6379> lrange list_key 0 10 1) "4" 2) "5" 3) "6" 4) "7"
参考链接:https://www.runoob.com/redis/redis-strings.html