在Redis里面,我们可以把list玩成栈、队列、阻塞队列
它实际上是一个链表,before Node after, left, right都可以插入值
所有的list命令都是以L开头的
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> RPUSH list four(若此时往右边插入)
(integer) 4
127.0.0.1:6379> LRANGE list 0 -1
1) "three"
2) "two"
3) "one"
4) "four"
(看到的顺序如上)
从左边移除列表第一个元素
127.0.0.1:6379> LPOP list
"three"
127.0.0.1:6379> LRANGE list 0 -1
1) "two"
2) "one"
3) "four"
从右边移除列表第一个元素
127.0.0.1:6379> RPOP list
"four"
127.0.0.1:6379> LRANGE list 0 -1
1) "two"
2) "one"
获取下标为0的key对应的值
127.0.0.1:6379> LINDEX list 0
"two"
LLEN list(返回列表的长度)
移除list中指定个数的value
127.0.0.1:6379> LREM list 1 one(移除1个one,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 1 three (移除1个three ,one是指定的值)
(integer) 1
127.0.0.1:6379> LRANGE list 0 -1
1) "three"
2) "two"
截取指定范围的list
127.0.0.1:6379> RPUSH mylist "hello1"
(integer) 1
127.0.0.1:6379> RPUSH mylist "hello2"
(integer) 2
127.0.0.1:6379> RPUSH mylist "hello3"
(integer) 3
127.0.0.1:6379> LRANGE mylist 0 -1
1) "hello1"
2) "hello2"
3) "hello3"
127.0.0.1:6379> ltrim mylist 1 2
OK
127.0.0.1:6379> LRANGE mylist 0 -1
1) "hello2"
2) "hello3"
将mylist右边元素第一个移动到另外新的list
127.0.0.1:6379> RPUSH mylist one
(integer) 1
127.0.0.1:6379> RPUSH mylist two
(integer) 2
127.0.0.1:6379> RPUSH mylist three
(integer) 3
127.0.0.1:6379> LRANGE mylist 1 -1
1) "two"
2) "three"
127.0.0.1:6379> LRANGE mylist 0 -1
1) "one"
2) "two"
3) "three"
127.0.0.1:6379> RPOPLPUSH mylist myotherlist
"three"
127.0.0.1:6379> LRANGE mylist 0 -1
1) "one"
2) "two"
127.0.0.1:6379> LRANGE myotherlist 0 -1
1) "three"
将下标为0的元素替换成item
127.0.0.1:6379> LPUSH list one
(integer) 1
127.0.0.1:6379> LRANGE list 0 -1
1) "one"
127.0.0.1:6379> lset list 0 item
OK
127.0.0.1:6379> LRANGE list 0 -1
1) "item"
在list指定元素one的前面插入新值aaa
127.0.0.1:6379> LRANGE list 0 -1
1) "two"
2) "one"
127.0.0.1:6379> LINSERT list before one aaa
(integer) 3
127.0.0.1:6379> LRANGE list 0 -1
1) "two"
2) "aaa"
3) "one"
原文:https://www.cnblogs.com/whs123/p/14154328.html