1、 将1个或者多个的value压入key的表头:LPUSH key value [value ...]
127.0.0.1:6379> LPUSH list abc
(integer) 1
127.0.0.1:6379> LGET list
(error) ERR unknown command 'LGET'
127.0.0.1:6379> LRANGE 0 -1
(error) ERR wrong number of arguments for 'lrange' command
127.0.0.1:6379> LRANGE list 0 -1
1) "abc"
127.0.0.1:6379> LPUSH list abc def
(integer) 3
127.0.0.1:6379> LRANGE list 0 -1
1) "def"
2) "abc"
3) "abc"
2、将1个value压入到key表头,当不存在key的时候失败: LPUSHX key value
127.0.0.1:6379> KEYS *
1) "list"
127.0.0.1:6379> LRANGE list 0 -1
1) "def"
2) "abc"
3) "abc"
127.0.0.1:6379> LPUSHX list woaini
(integer) 4
127.0.0.1:6379> LRANGE list 0 -1
1) "woaini"
2) "def"
3) "abc"
4) "abc"
127.0.0.1:6379> LPUSHx mylist aaaa
(integer) 0
127.0.0.1:6379> EXISTS mylist
(integer) 0
127.0.0.1:6379>
3、返回list的长度:HLEN key ; 如果key不存在就解释成一个空表返回0, 如果key不是表类型,返回一个错误。
127.0.0.1:6379> LRANGE list 0 -1
1) "woaini"
2) "def"
3) "abc"
4) "abc"
127.0.0.1:6379> LLEN list
(integer) 4
127.0.0.1:6379> EXISTS mylist
(integer) 0
127.0.0.1:6379> LLEN mylist
(integer) 0
3、返回list中指定的元素:LRANGE key start end ; 区间偏移是是start和end决定的,start最小从0开始,end可以是负数,-1代表最后一个 -2代表倒数第二个 等等,
127.0.0.1:6379> keys *
1) "list"
127.0.0.1:6379>
127.0.0.1:6379> type list
list
127.0.0.1:6379> LRANGE list 0 -1
1) "woaini"
2) "def"
3) "abc"
4) "abc"
127.0.0.1:6379> LRANGE list 0 1
1) "woaini"
2) "def"
127.0.0.1:6379> LRANGE list 0 -2
1) "woaini"
2) "def"
3) "abc"
127.0.0.1:6379> LRANGE list 0 -3
1) "woaini"
2) "def"
4、移除并返回list被移除的头元素:LPOP key
127.0.0.1:6379> LRANGE list 0 -1
1) "woaini"
2) "def"
3) "abc"
4) "abc"
127.0.0.1:6379> LPOP list
"woaini"
127.0.0.1:6379> LRANGE list 0 -1
1) "def"
2) "abc"
3) "abc"
127.0.0.1:6379> LPOP list
"def"
127.0.0.1:6379> LRANGE list 0 -1
1) "abc"
2) "abc"
5、将列表key下标为index的元素设置为value: LSET key index value ;如果key不存在或者index超出范围就返回一个错误
127.0.0.1:6379> LRANGE list 0 -1
1) "abc"
2) "abc"
127.0.0.1:6379> LSET list 0 def
OK
127.0.0.1:6379> LRANGE list 0 -1
1) "def"
2) "abc"
127.0.0.1:6379> LSET list 1 woaini1314
OK
127.0.0.1:6379> LRANGE list 0 -1
1) "def"
2) "woaini1314"
127.0.0.1:6379> LSET list 123 woaini1314
(error) ERR index out of range
6、根据count的值,移除列表中值为value的元素:
count可以为: cout>0 从表头到表尾,移除与value相等的元素,数量为count
count可以为:count=0 移除表中所有的与value相等的元素
count可以为:count<0 从表尾到表头,移除与value相等的元素,数量为count的绝对值
如果value为空或者list彼此在的时候总返回0:
LREM key count value
127.0.0.1:6379> LPUSH list def
(integer) 4
127.0.0.1:6379> LRANGE list 0 -1
1) "def"
2) "woaixiaofang"
3) "def"
4) "woaini1314"
127.0.0.1:6379>
127.0.0.1:6379> LREM list 3 def
(integer) 2
127.0.0.1:6379> LRANGE list 0 -1
1) "woaixiaofang"
2) "woaini1314"
7、将一个或者多个value插入list的尾部:RPUSH key value [value ..] ;如果不存在将会创建,如果key类型不是list那么会返回错误
127.0.0.1:6379> LRANGE list 0 -1
1) "def"
2) "abc"
3) "woaixiaofang"
4) "woaini1314"
127.0.0.1:6379> RPUSH list aaaaaaaa
(integer) 5
127.0.0.1:6379> LRANGE list 0 -1
1) "def"
2) "abc"
3) "woaixiaofang"
4) "woaini1314"
5) "aaaaaaaa"
127.0.0.1:6379> RPUSH list bbbb
(integer) 6
127.0.0.1:6379> LRANGE list 0 -1
1) "def"
2) "abc"
3) "woaixiaofang"
4) "woaini1314"
5) "aaaaaaaa"
6) "bbbb"
127.0.0.1:6379> RPUSH list bbbb mmmm
(integer) 8
127.0.0.1:6379> LRANGE list 0 -1
1) "def"
2) "abc"
3) "woaixiaofang"
4) "woaini1314"
5) "aaaaaaaa"
6) "bbbb"
7) "bbbb"
8) "mmmm"
8、将value插入list的尾部,当且仅当list存在的时候: RPUSHX key value
127.0.0.1:6379> keys *
1) "list"
127.0.0.1:6379> type list
list
127.0.0.1:6379> RPUSHX mm 12354
(integer) 0
127.0.0.1:6379> EXISTS mm
(integer) 0
127.0.0.1:6379> keys *
1) "list"
127.0.0.1:6379> LRANGE list 0 -1
1) "def"
2) "abc"
3) "woaixiaofang"
4) "woaini1314"
5) "aaaaaaaa"
6) "bbbb"
7) "bbbb"
8) "mmmm"
127.0.0.1:6379> RPUSHX list tianxia
(integer) 9
127.0.0.1:6379> LRANGE list 0 -1
1) "def"
2) "abc"
3) "woaixiaofang"
4) "woaini1314"
5) "aaaaaaaa"
6) "bbbb"
7) "bbbb"
8) "mmmm"
9) "tianxia"
9、弹出并且返回key的尾部:RPOP key
127.0.0.1:6379> LRANGE list 0 -1
1) "def"
2) "abc"
3) "woaixiaofang"
4) "woaini1314"
5) "aaaaaaaa"
6) "bbbb"
7) "bbbb"
8) "mmmm"
9) "tianxia"
127.0.0.1:6379> RPOP list
"tianxia"
127.0.0.1:6379> LRANGE list 0 -1
1) "def"
2) "abc"
3) "woaixiaofang"
4) "woaini1314"
5) "aaaaaaaa"
6) "bbbb"
7) "bbbb"
8) "mmmm"
10、RPOPLPUSH source dest :
命令在一个原子时间内,执行一下动作:
1)、将source尾部第一个元素弹出,返回给客户端;
2)、将source弹出的内容插入到列表,座位dest列表的头元素;
如果source不存在,就不执行操作,如果source和dest是同一个list,则执行旋转操作;
127.0.0.1:6379> LPUSH source 111 222 333 444 555
(integer) 5
127.0.0.1:6379> LRANGE source 0 -1
1) "555"
2) "444"
3) "333"
4) "222"
5) "111"
127.0.0.1:6379> LPUSH dest aaa bbb ccc ddd eee fff
(integer) 6
127.0.0.1:6379> LRANGE dest 0 -1
1) "fff"
2) "eee"
3) "ddd"
4) "ccc"
5) "bbb"
6) "aaa"
127.0.0.1:6379> RPOPLPUSH source dest
"111"
127.0.0.1:6379> LRANGE source 0 -1
1) "555"
2) "444"
3) "333"
4) "222"
127.0.0.1:6379> LRANGE dest 0 -1
1) "111"
2) "fff"
3) "eee"
4) "ddd"
5) "ccc"
6) "bbb"
7) "aaa"
127.0.0.1:6379> RPOPLPUSH source source
"222"
127.0.0.1:6379> LRANGE source 0 -1
1) "222"
2) "555"
3) "444"
4) "333"