前俩篇,介绍了string与hashes,本章介绍list的操作方法
首先我们使用redis-cli进入redis
[[email protected] ~]# redis-cli 127.0.0.1:6379>
1、添加或插入
a>lpush
方法:lpush key value
描述:从头压入一个元素
127.0.0.1:6379> lpush list1 one (integer) 1 127.0.0.1:6379> lpush list1 two (integer) 2 127.0.0.1:6379> lpush list1 three (integer) 3 127.0.0.1:6379> lrange list1 0 -1 1) "three" 2) "two" 3) "one"
b>lpushx
方法:pushx key value
描述:只有当key存在,才会从表头插入一个元素,否则不会插入
127.0.0.1:6379> lrange list1 0 -1 1) "three" 2) "two" 3) "one" 127.0.0.1:6379> lpushx list1 hello (integer) 4 127.0.0.1:6379> lrange list1 0 -1 1) "hello" 2) "three" 3) "two" 4) "one"
d>rpush
方法:rpush key value
描述:从尾压入一个元素
127.0.0.1:6379> rpush list4 one (integer) 1 127.0.0.1:6379> rpush list4 two (integer) 2 127.0.0.1:6379> rpush list4 three (integer) 3 127.0.0.1:6379> lrange list4 0 -1 1) "one" 2) "two" 3) "three"
e>rpushx
方法:rpush key value
描述:只有当key存在,才会从尾插入一个元素,否则不会插入
127.0.0.1:6379> lrange list4 0 -1 1) "one" 2) "two" 3) "three" 127.0.0.1:6379> rpushx list4 hello (integer) 4 127.0.0.1:6379> lrange list4 0 -1 1) "one" 2) "two" 3) "three" 4) "hello"
f>linsert
方法:linsert before|after pivot value
描述:before插入pivot前,after插入pivot后
127.0.0.1:6379> lrange list5 0 -1 1) "1" 2) "2" 3) "3" 4) "4" 5) "5" 127.0.0.1:6379> linsert list5 before 3 hello \\使用before插入3的前面hello (integer) 6 127.0.0.1:6379> lrange list5 0 -1 1) "1" 2) "2" 3) "hello" 4) "3" 5) "4" 6) "5"
127.0.0.1:6379> linsert list5 after 3 hello \\使用after插入3的后面hello
(integer) 7
127.0.0.1:6379> lrange list5 0 -1
1) "1"
2) "2"
3) "hello"
4) "3"
5) "hello"
6) "4"
7) "5"
2、查看
a>lindex
方法: lindex key index
描述: 以index下标查看key的value
127.0.0.1:6379> lrange list5 0 -1 1) "1" 2) "2" 3) "hello" 4) "3" 5) "hello" 6) "4" 7) "5" 127.0.0.1:6379> lindex list5 0 "1" 127.0.0.1:6379> lindex list5 1 "2" 127.0.0.1:6379> lindex list5 2 "hello" 127.0.0.1:6379> lindex list5 3 "3" 127.0.0.1:6379> lindex list5 4 "hello" 127.0.0.1:6379> lindex list5 5 "4" 127.0.0.1:6379> lindex list5 6 "5" 127.0.0.1:6379> lindex list5 7 (nil)
b>lrange
方法:lrange key start end
127.0.0.1:6379> lrange list5 0 -1 1) "1" 2) "2" 3) "hello" 4) "3" 5) "hello" 6) "4" 7) "5" 127.0.0.1:6379> lrange list5 0 4 1) "1" 2) "2" 3) "hello" 4) "3" 5) "hello" 127.0.0.1:6379> lrange list5 0 3 1) "1" 2) "2" 3) "hello" 4) "3"
c>llen
方法:llen key
描述:查看key元素的个数
127.0.0.1:6379> llen list9 (integer) 5 127.0.0.1:6379> lrange list9 0 -1 1) "0" 2) "1" 3) "2" 4) "3" 5) "4" 127.0.0.1:6379> llen list8 (integer) 0 127.0.0.1:6379> lrange list8 0 -1 (empty list or set)
3、修改
a>lset
方法:lset key index value
描述:如果key的index下标有值则修改,否则报错
127.0.0.1:6379> lrange list5 0 -1 1) "1" 2) "2" 3) "hello" 4) "3" 5) "hello" 6) "4" 7) "5" 127.0.0.1:6379> lset list5 3 three OK 127.0.0.1:6379> lrange list5 0 -1 1) "1" 2) "2" 3) "hello" 4) "three" 5) "hello" 6) "4" 7) "5" 127.0.0.1:6379> lset list5 10 three (error) ERR index out of range 127.0.0.1:6379> lrange list5 0 -1 1) "1" 2) "2" 3) "hello" 4) "three" 5) "hello" 6) "4" 7) "5"
4、删除
a>ltrim
方法:ltrim key start end
描述:保留指定key下标start end 范围的value
127.0.0.1:6379> lrange list5 0 -1 1) "1" 2) "2" 3) "hello" 4) "three" 5) "hello" 6) "4" 7) "5" 127.0.0.1:6379> ltrim list5 2 6 OK 127.0.0.1:6379> lrange list5 0 -1 1) "hello" 2) "three" 3) "hello" 4) "4" 5) "5"
b>lrem
方法:lrem key count value
描述:删除指定count的value元素,并返回count,如果value的count为0,从表头开始删除所有等于value,如果count<0则从尾开始删除count个数
127.0.0.1:6379> lpush list6 6 6 6 6 6 7 6 6 6 6 6 6 (integer) 12 127.0.0.1:6379> lrange list6 0 -1 1) "6" 2) "6" 3) "6" 4) "6" 5) "6" 6) "6" 7) "7" 8) "6" 9) "6" 10) "6" 11) "6" 12) "6" 127.0.0.1:6379> lrem list6 3 6 \\删除3个value等于6的,该删除是从表头开始 (integer) 3 127.0.0.1:6379> lrange list6 0 -1 1) "6" 2) "6" 3) "6" 4) "7" 5) "6" 6) "6" 7) "6" 8) "6" 9) "6" 127.0.0.1:6379> lrem list6 -3 6 \\删除3个value等于6的,该删除是从表尾开始 (integer) 3 127.0.0.1:6379> lrange list6 0 -1 1) "6" 2) "6" 3) "6" 4) "7" 5) "6" 6) "6"
c>rpop
方法:rpop key
描述:从尾移除并返回值
127.0.0.1:6379> lrange list6 0 -1 1) "6" 2) "6" 3) "6" 4) "7" 5) "6" 6) "6" 127.0.0.1:6379> rpop list6 "6" 127.0.0.1:6379> lrange list6 0 -1 1) "6" 2) "6" 3) "6" 4) "7" 5) "6"
d>lpop
方法:lpop key
描述:从头移除并返回值
127.0.0.1:6379> lpop list6 "6" 127.0.0.1:6379> lrange list6 0 -1 1) "6" 2) "6" 3) "7" 4) "6"
e>rpoplpush
方法:rpoplpush key1 key2
描述:从key1尾移除一个value压入到key2头
127.0.0.1:6379> rpush list8 1 2 (integer) 2 127.0.0.1:6379> rpush list9 3 4 (integer) 2 127.0.0.1:6379> lrange list8 0 -1 1) "1" 2) "2" 127.0.0.1:6379> lrange list9 0 -1 1) "3" 2) "4" 127.0.0.1:6379> rpoplpush list8 list9 "2" 127.0.0.1:6379> lrange list8 0 -1 1) "1" 127.0.0.1:6379> lrange list9 0 -1 1) "2" 2) "3" 3) "4"
f>blpop
方法:blpop key [key...] timeout
描述:从key头取出,第一个元素,如果没有值则执行timeout设定的阻塞时长,期间如果有值则立马取出
127.0.0.1:6379> lpush list8 1 2 (integer) 2 127.0.0.1:6379> blpop list8 20 1) "list8" 2) "2" 127.0.0.1:6379> blpop list8 20 1) "list8" 2) "1" 127.0.0.1:6379> blpop list8 20 \\在另外一台执行127.0.0.1:6379> lpush list8 3 在20秒的timeout期间,有元素压入,则立马取出并显示值 1) "list8" 2) "3" (2.69s)
g>brpop
方法:blpop key [key...] timeout
描述:从key尾取出,第一个元素,如果没有值则执行timeout设定的阻塞时长,期间如果有值则立马取出
127.0.0.1:6379> lrange list8 0 -1 1) "2" 2) "1" 127.0.0.1:6379> brpop list8 20 1) "list8" 2) "1" 127.0.0.1:6379> brpop list8 20 1) "list8" 2) "2" 127.0.0.1:6379> brpop list8 20 \\在另外一台执行127.0.0.1:6379> rpush list8 3 在20秒的timeout期间,有元素压入,则立马取出并显示值 1) "list8" 2) "3" (3.34s)
h>brpoplpush
方法:brpoplpush key1 key2 timeout
描述:从key1尾移除一个value压入到key2,如果没有执行timeout设定的阻塞时长,期间如果有值则立马取出。
127.0.0.1:6379> rpush list8 1 2 (integer) 2 127.0.0.1:6379> rpush list9 3 4 (integer) 2 127.0.0.1:6379> lrange list8 0 -1 1) "1" 2) "2" 127.0.0.1:6379> lrange list9 0 -1 1) "3" 2) "4" 127.0.0.1:6379> brpoplpush list8 list9 20 "2" 127.0.0.1:6379> brpoplpush list8 list9 20 "1" 127.0.0.1:6379> brpoplpush list8 list9 20 \\在另外一台执行127.0.0.1:6379> rpush list8 0 在20秒的timeout期间,有元素压入,则立马取出并显示值 "0" (2.70s)
至此Redis list就介绍完毕la
欢迎大家关注 459479177QQ群