# 向 list 中第 1 次出现 a 的前面添加 hello 127.0.0.1:6379> LINSERT list BEFORE a hello (integer) 5 127.0.0.1:6379> LRANGE list 0 -1 # 遍历 list 1) "hello" 2) "a" 3) "d" 4) "c" 5) "b"
127.0.0.1:6379> LPUSH mylist hello world gg yy hehe haha (integer) 6 127.0.0.1:6379> LTRIM mylist - + (error) ERR value is not an integer or out of range 127.0.0.1:6379> LTRIM mylist -inf +inf (error) ERR value is not an integer or out of range 127.0.0.1:6379> LTRIM mylist [1 [4 (error) ERR value is not an integer or out of range 127.0.0.1:6379> LTRIM mylist 1 4.5 (error) ERR value is not an integer or out of range 127.0.0.1:6379> LTRIM mylist 1 4 OK 127.0.0.1:6379> LRANGE mylist 0 -1 1) "hehe" 2) "yy" 3) "gg" 4) "world"
批量移除相同元素
LREM key count element 移除列表指定数量的元素并返回移除元素的数量, 列表为空或者不存在返回 0
count > 0 从列表头部开始向尾部搜索, 移除与 element 相等的元素, 数量为 count
count < 0 从列表尾部开始向头部搜索, 移除与 element 相等的元素, 数量为 count 的绝对值
count = 0 移除列表中与 element 相等的所有的元素
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
127.0.0.1:6379> KEYS * # 获取当前数据库的 key 1) "sex" 2) "addr" 3) "name" 4) "runoob" 5) "age" 127.0.0.1:6379> LPUSH list a b a d a c d a b d # 创建列表 list (integer) 10 127.0.0.1:6379> LREM list 3 a # 移除列表中 3 个元素 a (integer) 3 127.0.0.1:6379> LRANGE list 0 7 1) "d" 2) "b" 3) "d" 4) "c" 5) "d" 6) "b" 7) "a"
# 向 list 尾部添加元素 127.0.0.1:6379> RPUSH list c d e (integer) 3 # newlist 为空或者不存在, 会删除 list 的尾部的元素 127.0.0.1:6379> BRPOP newlist list 0 1) "list" 2) "e" # 向 newlist 尾部添加元素 127.0.0.1:6379> RPUSH newlist g a b f (integer) 4 # list 和 newlist 都是非空的列表, 会删除返回 list 的头部的元素 127.0.0.1:6379> BLPOP list newlist 0 1) "list" 2) "c" 127.0.0.1:6379> del list # 删除 list (integer) 1 # list 为空或者不存在, 会删除返回 newlist 的头部的元素 127.0.0.1:6379> BLPOP list newlist 0 1) "newlist" 2) "g"
127.0.0.1:6379> RPUSH list a b c d e f # 创建列表 list (integer) 6 127.0.0.1:6379> RPUSH newlist 1 2 3 4 5 6 # 创建列表 newlist (integer) 6 # list 不为空, 从 list 的头部删除返回 3 个 元素 127.0.0.1:6379> LMPOP 2 list newlist LEFT COUNT 3 1) "list" 2) 1) "a" 2) "b" 3) "c" 127.0.0.1:6379> del list # 删除列表 list (integer) 1 # list 为空, 从 newlist 的尾部删除返回 3 个元素 127.0.0.1:6379> LMPOP 2 list newlist right COUNT 3 1) "newlist" 2) 1) "6" 2) "5" 3) "4"
# 向 list 尾部添加元素 127.0.0.1:6379> RPUSH list a b c (integer) 3 # 向 newlist 尾部添加元素 127.0.0.1:6379> RPUSH newlist 1 2 3 (integer) 3 # 从 list 头部移除一个元素添加到 newlist 的尾部 127.0.0.1:6379> BLMOVE list newlist LEFT RIGHT 0 "a" # 遍历 newlist 127.0.0.1:6379> LRANGE newlist 0 -1 1) "1" 2) "2" 3) "3" 4) "a"
# 阻塞执行等待超时或者有可操作元素 127.0.0.1:6379> BRPOPLPUSH list newlist 10 (nil) (10.05s) # 向 list 尾部添加元素 127.0.0.1:6379> RPUSH list a b c d (integer) 4 # 从 list 尾部移除一个元素添加到 newlist 的头部 127.0.0.1:6379> BRPOPLPUSH list newlist "d"
# 向 list 尾部添加元素 127.0.0.1:6379> RPUSH list a b c d e f g (integer) 7 # 从 list 头部移除元素 a 添加到 newlist 的头部 127.0.0.1:6379> LMOVE list newlist LEFT LEFT "a" 127.0.0.1:6379> LRANGE newlist 0 -1 # 遍历 newlist 1) "a" # 从 list 头部移除元素 b 添加到 newlist 的尾部 127.0.0.1:6379> LMOVE list newlist LEFT RIGHT "b" 127.0.0.1:6379> LRANGE newlist 0 -1 # 遍历 newlist 1) "a" 2) "b" # 从 list 尾部移除元素 g 添加到 newlist 的头部 127.0.0.1:6379> LMOVE list newlist RIGHT LEFT "g" 127.0.0.1:6379> LRANGE newlist 0 -1 # 遍历 newlist 1) "g" 2) "a" 3) "b" # 从 list 尾部移除元素 f 添加到 newlist 的头部 127.0.0.1:6379> LMOVE list newlist RIGHT RIGHT "f" 127.0.0.1:6379> LRANGE newlist 0 -1 # 遍历 newlist 1) "g" 2) "a" 3) "b" 4) "f"
127.0.0.1:6379> LPUSH mylist hello world gg yy hehe haha (integer) 6 127.0.0.1:6379> LRANGE mylist - + (error) ERR value is not an integer or out of range 127.0.0.1:6379> LRANGE mylist -inf +inf (error) ERR value is not an integer or out of range 127.0.0.1:6379> LRANGE mylist 1 4.5 (error) ERR value is not an integer or out of range 127.0.0.1:6379> LRANGE mylist (1 (4 (error) ERR value is not an integer or out of range 127.0.0.1:6379> LRANGE mylist [1 [4 (error) ERR value is not an integer or out of range 127.0.0.1:6379> LRANGE mylist 1 4 1) "hehe" 2) "yy" 3) "gg" 4) "world"
查找指定索引元素
LINDEX key index 获取列表中指定索引的元素, 如果列表或者索引不存在返回 <nil>
# 创建列表 list 127.0.0.1:6379> RPUSH list a b c a b c b d c a b a c d b c a d (integer) 18 # 查找元素 b 第 1 次出现的下标 127.0.0.1:6379> LPOS list b (integer) 1 # 查找元素 b 第 3 次出现的下标 127.0.0.1:6379> LPOS list b RANK 3 (integer) 6 # 查找元素 b 出现 3 次的下标 127.0.0.1:6379> LPOS list b COUNT 3 1) (integer) 1 2) (integer) 4 3) (integer) 6 # 查找元素 b 第 2 次开始出现 3 次的下标 127.0.0.1:6379> LPOS list b RANK 2 COUNT 3 1) (integer) 4 2) (integer) 6 3) (integer) 10 # 查找元素 b 第 3 次开始出现 4 次的下标 127.0.0.1:6379> LPOS list b RANK 3 COUNT 4 1) (integer) 6 2) (integer) 10 3) (integer) 14 # 查找 10 次元素 b 第 2 次开始出现 4 次的下标 127.0.0.1:6379> LPOS list b RANK 2 COUNT 4 MAXLEN 10 1) (integer) 4 2) (integer) 6 # 查找 15 次元素 b 第 3 次开始出现 2 次的下标 127.0.0.1:6379> LPOS list b RANK 3 COUNT 2 MAXLEN 15 1) (integer) 6 2) (integer) 10