這篇文章跟大家介紹Redis中的list指令,透過程式碼範例了解list指令的用法,希望對大家有幫助!
#List類型是一個雙端鍊錶
的結構,容量是2的32次方減1個元素,即40多億個;其主要功能有push、pop、獲取元素等;一般應用在棧、隊列、訊息隊列
等場景。 【相關建議:Redis影片教學】
[l/r]push-左/右新增元素
語法:[l/r]push key value [value ...]
127.0.0.1:6379> lpush pushkey 1 2 3 (integer) 3 127.0.0.1:6379> lpush pushkey 4 (integer) 4 127.0.0.1:6379> rpush pushkey 5 (integer) 5 127.0.0.1:6379> lrange pushkey 0 -1 1) "4" 2) "3" 3) "2" 4) "1" 5) "5"
lrange-查詢範圍元素
語法: lrange key start stop
127.0.0.1:6379> lpush products 1 2 3 (integer) 3 127.0.0.1:6379> lpush products 4 5 6 (integer) 6 127.0.0.1:6379> lrange products 0 -1 1) "6" 2) "5" 3) "4" 4) "3" 5) "2" 6) "1"
linsert-在某元素前後插入元素
語法:linsert key BEFORE|AFTER pivot value
127.0.0.1:6379> lrange products 0 -1 1) "6" 2) "5" 3) "4" 4) "3" 5) "2" 6) "1" 127.0.0.1:6379> linsert products before 5 a (integer) 7 127.0.0.1:6379> lrange products 0 -1 1) "6" 2) "a" 3) "5" 4) "4" 5) "3" 6) "2" 7) "1" 127.0.0.1:6379> linsert products after a b (integer) 8 127.0.0.1:6379> lrange products 0 -1 1) "6" 2) "a" 3) "b" 4) "5" 5) "4" 6) "3" 7) "2" 8) "1"
##llen-取得長度語法:
llen key取得清單長度
127.0.0.1:6379> lrange products 0 -1 1) "6" 2) "a" 3) "b" 4) "5" 5) "4" 6) "3" 7) "2" 8) "1" 127.0.0.1:6379> llen products (integer) 8
lindex-根據下標取得元素 語法:
lindex key index透過索引取得清單中的元素
127.0.0.1:6379> lrange products 0 -1 1) "6" 2) "a" 3) "b" 4) "5" 5) "4" 6) "3" 7) "2" 8) "1" 127.0.0.1:6379> lindex products 2 "b"
lset-根據下標設定值語法:
lset key index value透過索引設定清單元素的值
127.0.0.1:6379> lrange products 0 -1 1) "6" 2) "a" 3) "b" 4) "5" 5) "4" 6) "3" 7) "2" 8) "1" 127.0.0.1:6379> lset products 2 B OK 127.0.0.1:6379> lrange products 0 -1 1) "6" 2) "a" 3) "B" 4) "5" 5) "4" 6) "3" 7) "2" 8) "1"
#ltrim-截斷元素語法:
ltrim key start end截斷佇列指定區間的元素,其餘元素都刪除
127.0.0.1:6379> lrange products 0 -1 1) "6" 2) "a" 3) "B" 4) "5" 5) "4" 6) "3" 7) "2" 8) "1" 127.0.0.1:6379> ltrim products 0 3 OK 127.0.0.1:6379> lrange products 0 -1 1) "6" 2) "a" 3) "B" 4) "5"
lrem-移除元素語法:
lrem key count 值移除清單元素
127.0.0.1:6379> lpush test a 1 a 2 a 3 a 4 5 6 (integer) 10 127.0.0.1:6379> lrange test 0 -1 1) "6" 2) "5" 3) "4" 4) "a" 5) "3" 6) "a" 7) "2" 8) "a" 9) "1" 10) "a" 127.0.0.1:6379> lrem test 3 a (integer) 4 127.0.0.1:6379> lrange test 0 -1 1) "6" 2) "5" 3) "4" 4) "3" 5) "2" 6) "1" 7) "a"
[l/r]pop-從左/右彈出元素#語法:
[l/r]pop key從佇列的頭或尾彈出節點元素(傳回該元素並從佇列中刪除)
127.0.0.1:6379> lrange test 0 -1 1) "6" 2) "5" 3) "4" 4) "3" 5) "2" 6) "1" 127.0.0.1:6379> lpop test "6" 127.0.0.1:6379> lrange test 0 -1 1) "5" 2) "4" 3) "3" 4) "2" 5) "1" 127.0.0.1:6379> rpop test "1" 127.0.0.1:6379> lrange test 0 -1 1) "5" 2) "4" 3) "3" 4) "2"
rpoplpush-移除右元素加入到另一列表左語法:
rpoplpush source destination移除清單的最後一個元素(右),並將該元素加入到另一個列表(左)並返回
127.0.0.1:6379> lpush src 1 2 3 (integer) 3 127.0.0.1:6379> lrange src 0 -1 1) "3" 2) "2" 3) "1" 127.0.0.1:6379> lpush dst a b c (integer) 3 127.0.0.1:6379> lrange dst 0 -1 1) "c" 2) "b" 3) "a" 127.0.0.1:6379> rpoplpush src dst "1" 127.0.0.1:6379> lrange src 0 -1 1) "3" 2) "2" 127.0.0.1:6379> lrange dst 0 -1 1) "1" 2) "c" 3) "b" 4) "a"
b[l/r]pop-阻塞彈出左/右一個元素語法:
b[l/r]pop key1 [key2 ...] timeout移出並取得清單的第一個或最後一個元素, 如果清單沒有元素會
直到等待逾時
或發現
可彈出元素為止。 <pre class="brush:js;toolbar:false;">127.0.0.1:6379> lpush list1 1 2
(integer) 2
127.0.0.1:6379> lpush list2 a b
(integer) 2
127.0.0.1:6379> lrange list1 0 -1
1) "2"
2) "1"
127.0.0.1:6379> lrange list2 0 -1
1) "b"
2) "a"
127.0.0.1:6379> blpop list1 list2 10
1) "list1" #弹出元素所属的列表
2) "2" #弹出元素所属的值
127.0.0.1:6379> blpop list1 list2 10
1) "list1"
2) "1"
127.0.0.1:6379> blpop list1 list2 10
1) "list2"
2) "b"
127.0.0.1:6379> blpop list1 list2 10
1) "list2"
2) "a"
127.0.0.1:6379> blpop list1 list2 10
(nil)
(10.08s) # 列表为空的时候,就等待超时10秒</pre>
更多程式相關知識,請造訪:
以上是實戰:透過範例程式碼聊聊Redis中的list指令的詳細內容。更多資訊請關注PHP中文網其他相關文章!