redis list類(lèi)型命令的實(shí)現(xiàn)
redis的list類(lèi)型,可以存儲(chǔ)雙向鏈表作為value,key保留有head和tail指針可以指向雙向鏈表的頭和尾,因此可以直接從頭或尾對(duì)list進(jìn)行操作。
全部命令如下:
127.0.0.1:6379> help @list ? BLMOVE source destination LEFT|RIGHT LEFT|RIGHT timeout ? summary: Pop an element from a list, push it to another list and return it; or block until one is available ? since: 6.2.0 ? BLMPOP timeout numkeys key [key ...] LEFT|RIGHT [COUNT count] ? summary: Pop elements from a list, or block until one is available ? since: 7.0.0 ? BLPOP key [key ...] timeout ? summary: Remove and get the first element in a list, or block until one is available ? since: 2.0.0 ? BRPOP key [key ...] timeout ? summary: Remove and get the last element in a list, or block until one is available ? since: 2.0.0 ? BRPOPLPUSH source destination timeout ? summary: Pop an element from a list, push it to another list and return it; or block until one is available ? since: 2.2.0 ? LINDEX key index ? summary: Get an element from a list by its index ? since: 1.0.0 ? LINSERT key BEFORE|AFTER pivot element ? summary: Insert an element before or after another element in a list ? since: 2.2.0 ? LLEN key ? summary: Get the length of a list ? since: 1.0.0 ? LMOVE source destination LEFT|RIGHT LEFT|RIGHT ? summary: Pop an element from a list, push it to another list and return it ? since: 6.2.0 ? LMPOP numkeys key [key ...] LEFT|RIGHT [COUNT count] ? summary: Pop elements from a list ? since: 7.0.0 ? LPOP key [count] ? summary: Remove and get the first elements in a list ? since: 1.0.0 ? LPOS key element [RANK rank] [COUNT num-matches] [MAXLEN len] ? summary: Return the index of matching elements on a list ? since: 6.0.6 ? LPUSH key element [element ...] ? summary: Prepend one or multiple elements to a list ? since: 1.0.0 ? LPUSHX key element [element ...] ? summary: Prepend an element to a list, only if the list exists ? since: 2.2.0 ? LRANGE key start stop ? summary: Get a range of elements from a list ? since: 1.0.0 ? LREM key count element ? summary: Remove elements from a list ? since: 1.0.0 ? LSET key index element ? summary: Set the value of an element in a list by its index ? since: 1.0.0 ? LTRIM key start stop ? summary: Trim a list to the specified range ? since: 1.0.0 ? RPOP key [count] ? summary: Remove and get the last elements in a list ? since: 1.0.0 ? RPOPLPUSH source destination ? summary: Remove the last element in a list, prepend it to another list and return it ? since: 1.2.0 ? RPUSH key element [element ...] ? summary: Append one or multiple elements to a list ? since: 1.0.0 ? RPUSHX key element [element ...] ? summary: Append an element to a list, only if the list exists ? since: 2.2.0
下面示例如下:
- lpush:lpush key e1 e2 e3…將數(shù)據(jù)從頭那里推入list
- lpop:lpop key,將數(shù)據(jù)從head彈出
這樣2個(gè)同向的命令組合起來(lái),可以實(shí)現(xiàn)一個(gè)隊(duì)列。
反向的命令組合起來(lái),可以實(shí)現(xiàn)一個(gè)棧。
127.0.0.1:6379> lpush k1 a b c d e (integer) 5 127.0.0.1:6379> lpop k1 "e" 127.0.0.1:6379> lpop k1 "d"
lrange :lrange key start end,展示key對(duì)應(yīng)的從下標(biāo)start到end的所有數(shù)據(jù)
127.0.0.1:6379> lrange k1 0 -1 1) "c" 2) "b" 3) "a"
lindex:lindex key index ,返回key對(duì)應(yīng)的List指定index位置的值
127.0.0.1:6379> lrange k1 0 -1 1) "f" 2) "e" 3) "d" 4) "c" 5) "b" 6) "a" 127.0.0.1:6379> lindex k1 1 "e"
lset:lset key index value,在key對(duì)應(yīng)的list中的指定下標(biāo)處替換為value
127.0.0.1:6379> lset k1 3 x OK 127.0.0.1:6379> lrange k1 0 -1 1) "f" 2) "e" 3) "d" 4) "x" 5) "b" 6) "a"
lrem:lrem key num target,刪除key對(duì)應(yīng)的list中的target元素,如果num大于0,從head開(kāi)始刪num個(gè)。如果num小于0,從tail開(kāi)始刪abs(num)個(gè)
127.0.0.1:6379> lrange k3 0 -1 1) "d" 2) "6" 3) "a" 4) "5" 5) "c" 6) "4" 7) "a" 8) "3" 9) "b" 10) "2" 11) "a" 12) "1" 127.0.0.1:6379> lrem k3 2 a (integer) 2 127.0.0.1:6379> lrange k3 0 -1 1) "d" 2) "6" 3) "5" 4) "c" 5) "4" 6) "3" 7) "b" 8) "2" 9) "a" 10) "1"
linsert:linsert key before/after element value,在key對(duì)應(yīng)的list中,在元素element(不是下標(biāo))之前或者之后,添加value
127.0.0.1:6379> lrange k3 0 -1 1) "d" 2) "6" 3) "5" 4) "c" 5) "4" 6) "3" 7) "b" 8) "2" 9) "a" 10) "1" 127.0.0.1:6379> linsert k3 after 6 a (integer) 11 127.0.0.1:6379> lrange k3 0 -1 1) "d" 2) "6" 3) "a" 4) "5" 5) "c" 6) "4" 7) "3" 8) "b" 9) "2" 10) "a" 11) "1"
llen:llen key,返回長(zhǎng)度
redis的List類(lèi)型有很多關(guān)于下標(biāo)的操作,也可以將其抽象為一個(gè)數(shù)組來(lái)使用。
127.0.0.1:6379> llen k3 (integer) 10
blpop:blpop key time,彈出指定key對(duì)應(yīng)的list中的一個(gè)元素,如果list沒(méi)有元素或者不存在key對(duì)應(yīng)的這個(gè)list,則阻塞等待time指定的時(shí)間,0表示一直等待,單位是s。
127.0.0.1:6379> blpop k1 0
阻塞期間如果list有了元素,則會(huì)中斷阻塞并彈出
127.0.0.1:6379> blpop k1 0 1) "k1" 2) "a" (35.77s)
ltrim:ltrim key start end,刪除key對(duì)應(yīng)的list start和end之外的兩端的元素
127.0.0.1:6379> lrange k4 0 -1 1) "f" 2) "e" 3) "d" 4) "c" 5) "b" 6) "a" 127.0.0.1:6379> ltrim k4 1 -2 OK 127.0.0.1:6379> lrange k4 0 -1 1) "e" 2) "d" 3) "c" 4) "b"
到此這篇關(guān)于redis list類(lèi)型命令的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)redis list類(lèi)型內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Redis之如何實(shí)現(xiàn)用戶(hù)關(guān)注
這篇文章主要介紹了Redis之如何實(shí)現(xiàn)用戶(hù)關(guān)注問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-03-03詳解redis大幅性能提升之使用管道(PipeLine)和批量(Batch)操作
這篇文章主要介紹了詳解redis大幅性能提升之使用管道(PipeLine)和批量(Batch)操作 ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-12-12在Centos?8.0中安裝Redis服務(wù)器的教程詳解
由于考慮到linux服務(wù)器的性能,所以經(jīng)常需要把一些中間件安裝在linux服務(wù)上,今天通過(guò)本文給大家介紹下在Centos?8.0中安裝Redis服務(wù)器的詳細(xì)過(guò)程,感興趣的朋友一起看看吧2022-03-03Redis快速實(shí)現(xiàn)分布式session的方法詳解
Session是客戶(hù)端與服務(wù)器通訊會(huì)話跟蹤技術(shù),服務(wù)器與客戶(hù)端保持整個(gè)通訊的會(huì)話基本信息。本文主要介紹了Redis快速實(shí)現(xiàn)分布式session的方法,感興趣的可以學(xué)習(xí)一下2022-01-01無(wú)法連接redis服務(wù)器問(wèn)題的解決辦法(非常詳細(xì)!)
這篇文章主要介紹了如何解決Spring?Boot項(xiàng)目連接Redis失敗的問(wèn)題,通過(guò)修改Redis配置文件、添加防火墻白名單或關(guān)閉防火墻,并使用RESP工具進(jìn)行測(cè)試,需要的朋友可以參考下2025-02-02Redis特殊數(shù)據(jù)類(lèi)型HyperLogLog基數(shù)統(tǒng)計(jì)算法講解
這篇文章主要為大家介紹了Redis特殊數(shù)據(jù)類(lèi)型HyperLogLog基數(shù)統(tǒng)計(jì)算法講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05