List
所有的list命令都是以l开头,Redis不区分大小写
lpush key value 将一个值插入到列表头部
rpush key value 直接将数据添加到列表末尾
lrange key start pop 根据索引[start,stop]获取列表的元素 ,lrange key 0 -1 获取列表所有的元素
lpop key 移除列表中左边的值
rpop key 移除列表中右边的值
lindex key index 根据索引查看value
llen key 查看key的长度
lrem key count value 移除key列表中count个value
ltrim key start stop 截断key,索引从start开始到stop结束 [start,stop]
rpoplpush key destination 截取key右边的元素然后将此元素lpush(从左边push进去)到新的列表destination
lset key index value 列表根据索引设置value ,如果key不存在则报错,索引不存在报索引越界。如果key存在,并且索引也存在则进行更新操作
linsert key before|after pivot value :key列表插入 在pivot 的前面或者(before|after)插入value,如果pivot不存在则插入失败
127.0.0.1:6379> LPUSH list one
(integer) 1
127.0.0.1:6379> LPUSH list two
(integer) 2
127.0.0.1:6379> LPUSH list three
(integer) 3
127.0.0.1:6379> LRANGE list 0 -1
1) "three"
2) "two"
3) "one"
127.0.0.1:6379> LRANGE list 0 2
1) "three"
2) "two"
3) "one"
127.0.0.1:6379> RPUSH list right
(integer) 4
127.0.0.1:6379> LRANGE list 0 -1
1) "three"
2) "two"
3) "one"
4) "right"
127.0.0.1:6379> LRANGE list 0 5
1) "three"
2) "two"
3) "one"
4) "right"
127.0.0.1:6379> lpop list
"three"
127.0.0.1:6379> rpop list
"right"
127.0.0.1:6379> lrange list 0 -1
1) "two"
2) "one"
127.0.0.1:6379> lindex list 1
"one"
127.0.0.1:6379> lindex list 0
"two"
127.0.0.1:6379> lpush list one
(integer) 1
127.0.0.1:6379> lpush list two
(integer) 2
127.0.0.1:6379> lpush list three
(integer) 3
127.0.0.1:6379> LLEN list
(integer) 3
127.0.0.1:6379> lpush list three
(integer) 4
127.0.0.1:6379> lrange list 0 -1
1) "three"
2) "three"
3) "two"
4) "one"
127.0.0.1:6379> lrem list 1 one
(integer) 1
127.0.0.1:6379> lrange list 0 -1
1) "three"
2) "three"
3) "two"
127.0.0.1:6379> lrem list 2 three
(integer) 2
127.0.0.1:6379> lrange list 0 2
1) "two"
127.0.0.1:6379> rpush list hello
(integer) 1
127.0.0.1:6379> rpush list hello1
(integer) 2
127.0.0.1:6379> rpush list hello2
(integer) 3
127.0.0.1:6379> rpush list hello3
(integer) 4
127.0.0.1:6379> ltrim list 0 2
OK
127.0.0.1:6379> lrange list 0 -1
1) "hello"
2) "hello1"
3) "hello2"
127.0.0.1:6379> lrange list 0 -1
1) "hello"
2) "hello1"
3) "hello2"
4) "hello3"
127.0.0.1:6379> rpoplpush list mylist
"hello3"
127.0.0.1:6379> LRANGE list 0 -1
1) "hello"
2) "hello1"
3) "hello2"
127.0.0.1:6379> lrange mylist 0 1
1) "hello3"
127.0.0.1:6379> exists list
(integer) 0
127.0.0.1:6379> lset list 0 hello
(error) ERR no such key
127.0.0.1:6379> lpush list value1
(integer) 1
127.0.0.1:6379> lrange list 0 -1
1) "value1"
127.0.0.1:6379> lset list 0 hello
OK
127.0.0.1:6379> lrange list 0 -1
1) "hello"
127.0.0.1:6379> lset list 1 hello1
(error) ERR index out of range
127.0.0.1:6379> rpush list hello
(integer) 1
127.0.0.1:6379> rpush list world
(integer) 2
127.0.0.1:6379> linsert list before hello gun
(integer) 3
127.0.0.1:6379> lrange list 0 -1
1) "gun"
2) "hello"
3) "world"
127.0.0.1:6379> linsert list after world gg
(integer) 4
127.0.0.1:6379> linsert list after g gg
(integer) -1
127.0.0.1:6379> lrange list 0 -1
1) "gun"
2) "hello"
3) "world"
4) "gg"
小结 :
- 他实际上是一个列表,before node after ,left ,right 都可以插入值
- 如果key不存在,lpush 创建新的列表
- 如果key存在,lpush在后面添加value
- 如果移除所有的值,空链表,就不存在列表
个人感觉这就是栈和队列!
|