List是一个链表结构,主要功能是push、pop、获取一个范围之内的所有值等等,操作中key理解为链表的名称。Redis的list类型其实就是一个每个子元素都是string类型的双向链表。我们可以铜驼push、pop操作从链表的头部或者尾部添加删除元素,这样list既可以作为栈,又可以作为队列。
eg:redis 127.0.0.1:6379>lpush mylist “world”
(integer)1
redis 127.0.0.1:6379>lpush mylist “hello”
(integer)2
redis 127.0.0.1:6379>lrange mylist 0 -1 //从头第一个元素到尾第一个元素。
“hello”
“world”
不再举例,和2.1类似,只是元素压入方向不一样而已。
弥补了lpush,rpush方法只能在一端插入的不足。
eg:redis 127.0.0.1:6379>lpush list3 “one”
redis127.0.0.1:6379>lpush list3 “two”
redis127.0.0.1:6379>linsert list3 before one three
redis127.0.0.1:6379>lrange list3 0 -1
“two”
“three”
“one”
redis127.0.0.1:6379>rpush mylist4 “hello”
redis127.0.0.1:6379>lset mylist4 0 “world”
redis127.0.0.1:6379>lrange mylist4 0 -1
“world”
redis127.0.0.1:6379>lpush list5 “one”
redis 127.0.0.1:6379>lpush list5 “one”
redis 127.0.0.1:6379>lpush list5 “two”
redis 127.0.0.1:6379>lpush list5 “three”
redis 127.0.0.1:6379>lpush list5 “one”
redis 127.0.0.1:6379>lrem list5 2 “one”
(integer)2
redis 127.0.0.1:6379>lrange list5 0 -1
“two”
“three”
“one”
redis 127.0.0.1:6379>rpush mylist6 “one”
redis 127.0.0.1:6379>rpush mylist6 “two”
redis 127.0.0.1:6379>rpush mylist6“three”
redis 127.0.0.1:6379>rpush mylist6“four”
redis 127.0.0.1:6379>lrim mylist6 1 2
(integer)2
redis 127.0.0.1:6379>lrange mylist6 0 -1
“two”
“three”
redis 127.0.0.1:6379>rpush mylist10 “one”
redis 127.0.0.1:6379>rpush mylist10“two”
redis 127.0.0.1:6379>rpush mylist10“three”
redis 127.0.0.1:6379>lpop mylist10
“one”
eg:redis 127.0.0.1:6379>rupshmylist11 “one”
redis 127.0.0.1:6379>rpush mylist11“two”
redis 127.0.0.1:6379>rpush mylist12“three”
redis 127.0.0.1:6379>rpush mylist13“four”
redis 127.0.0.1:6379>rpoplpush mylist11mylist12
redis 127.0.0.1:6379>lrange mylist11 0-1
“one”
redis 127.0.0.1:6379>lrange mylist11 0-1
“two”
“three”
“four”
eg: redis 127.0.0.1:6379>lpush mylist12“one”
redis 127.0.0.1:6379>lpush mylist12“two”
redis 127.0.0.1:6379>lindex mylist12 0
“one”
eg:redis 127.0.0.1:6379>rpushmylist13 “one”
redis 127.0.0.1:6379>rpush mylist13“two”
redis 127.0.0.1:6379>rpush mylist13“three”
redis 127.0.0.1:6379>llen mylist13
(integer)3