lpush key values[value1 value2…]:在指定key所關聯的list頭部插入全部的valueredis
127.0.0.1:6379> lpush list1 a b c d (integer) 4 127.0.0.1:6379> lrange list1 0 3 1) "d" 2) "c" 3) "b" 4) "a"
rpush key values[value一、value2…]:在該list的尾部添加元素。bash
127.0.0.1:6379> rpush list2 a b c d (integer) 4 127.0.0.1:6379> lrange list2 0 3 1) "a" 2) "b" 3) "c" 4) "d"
lrange key start end:獲取鏈表中從start到end的元素的值,start、end從0開始計數;也可爲負數,若爲-1則表示鏈表尾部的元素,-2則表示倒數第二個,依次類推…spa
127.0.0.1:6379> lrange list1 0 3 1) "d" 2) "c" 3) "b" 4) "a" 127.0.0.1:6379> lrange list1 0 -1 1) "d" 2) "c" 3) "b" 4) "a"
lpop key:返回並彈出指定的key關聯的鏈表中的第一個元素,即頭部元素。若是該key不存在,返回nil;若key存在,則返回鏈表的頭部元素。code
rpop key:從尾部彈出元素索引
若是key中元素沒有了,在redis中key也會不存在。redis不會保留空list或空集合隊列
127.0.0.1:6379> lrange list1 0 -1 1) "d" 2) "c" 3) "b" 4) "a" 127.0.0.1:6379> lpop list1 "d" 127.0.0.1:6379> lrange list1 0 -1 1) "c" 2) "b" 3) "a" 127.0.0.1:6379> lpop list1 "c" 127.0.0.1:6379> lrange list1 0 -1 1) "b" 2) "a" 127.0.0.1:6379> keys * 1) "name" 2) "list2" 3) "list1" 127.0.0.1:6379> lpop list1 "b" 127.0.0.1:6379> lpop list1 "a" 127.0.0.1:6379> keys * 1) "name" 2) "list2" 127.0.0.1:6379>
127.0.0.1:6379> lrange list2 0 -1 1) "a" 2) "b" 3) "c" 4) "d" 127.0.0.1:6379> llen list2 (integer) 4
lrem key count value:刪除count個值爲value的元素。若是count大於0,從頭向尾遍歷並刪除count個值爲value的元素。若是count小於0,則從尾向頭遍歷並刪除。若是count等於0,則刪除鏈表中全部值爲value的元素。rem
效率較低,由於redis會給元素建立索引。io
127.0.0.1:6379> rpush list3 a b c a b c a b c (integer) 9 127.0.0.1:6379> lrem list3 0 a (integer) 3 127.0.0.1:6379> lrange list3 0 -1 1) "b" 2) "c" 3) "b" 4) "c" 5) "b" 6) "c" 127.0.0.1:6379> lrem list3 -2 b (integer) 2 127.0.0.1:6379> lrange list3 0 -1 1) "b" 2) "c" 3) "c" 4) "c" 127.0.0.1:6379>
127.0.0.1:6379> keys * 1) "name" 2) "list3" 3) "list2" 127.0.0.1:6379> del list2 (integer) 1 127.0.0.1:6379> del list3 (integer) 1 127.0.0.1:6379> keys * 1) "name"
lset key index value:設置鏈表中下標爲index的元素值。0表明鏈表的頭元素,-1表明鏈表的尾元素。操做鏈表不會拋異常。class
效率不過,由於是經過建索引操做。效率
127.0.0.1:6379> rpush list4 a b c a b c (integer) 6 127.0.0.1:6379> lset list4 2 ccc OK 127.0.0.1:6379> lrange list4 0 -1 1) "a" 2) "b" 3) "ccc" 4) "a" 5) "b" 6) "c" 127.0.0.1:6379> lset list4 -2 bbb OK 127.0.0.1:6379> lrange list4 0 -1 1) "a" 2) "b" 3) "ccc" 4) "a" 5) "bbb" 6) "c" 127.0.0.1:6379>
linsert key before/after pivot value:在pivot元素前或後插入value這個元素。
效率不高,由於是經過索引操做。
127.0.0.1:6379> rpush list5 a b c a b c (integer) 6 127.0.0.1:6379> linsert list5 before b 666 (integer) 7 127.0.0.1:6379> lrange list5 0 -1 1) "a" 2) "666" 3) "b" 4) "c" 5) "a" 6) "b" 7) "c" 127.0.0.1:6379> linsert list5 after b 777 (integer) 8 127.0.0.1:6379> lrange list5 0 -1 1) "a" 2) "666" 3) "b" 4) "777" 5) "c" 6) "a" 7) "b" 8) "c"
rpoplpush resource destination:將鏈表中的尾部元素彈出並添加到頭部
兩個隊列
127.0.0.1:6379> rpush list6 a b c d (integer) 4 127.0.0.1:6379> lrange list6 0 -1 1) "a" 2) "b" 3) "c" 4) "d" 127.0.0.1:6379> rpoplpush list6 list7 "d" 127.0.0.1:6379> lrange list6 0 -1 1) "a" 2) "b" 3) "c" 127.0.0.1:6379> lrange list7 0 -1 1) "d"
一個隊列
127.0.0.1:6379> rpush list8 a b c d e f (integer) 6 127.0.0.1:6379> rpoplpush list8 list8 "f" 127.0.0.1:6379> rpoplpush list8 list8 "e" 127.0.0.1:6379> lrange list8 0 -1 1) "e" 2) "f" 3) "a" 4) "b" 5) "c" 6) "d"