LIST 整體結(jié)構(gòu)圖

?

圖畫的太大了,只能放地址:

http://dl.iteye.com/upload/picture/pic/115935/8e96f42d-3a7b-3cea-85ae-997496aa9521.jpg

?

LIST列表的操作,可想而知,對于列表我們需要的具備的功能列表

加入列表:

? 從頭部加入 ? LPUSH?

? 從底部加入 ? RPUSH

彈出列表

? 從頭部彈出 ? LPOP

? 從底部彈出 ? RPOP

截取子列表 ? ? LRANGE

計算列表的長度 LLEN

?

?

實踐:

redis 127.0.0.1:6379> rpush l2 1 ? ? // 從底部添加一個元素

(integer) 1

redis 127.0.0.1:6379> rpush l2 2?

(integer) 2

redis 127.0.0.1:6379> lrange l2 0 -1 // 展示所有的元素 ?-1 代表所有

1) "1"

2) "2"

redis 127.0.0.1:6379> lpush l2 3?

(integer) 3

redis 127.0.0.1:6379> lrange l2 0 -1?

1) "3"

2) "1"

3) "2"

redis 127.0.0.1:6379> lpop l2 ? ? ? // 從頭部彈出一個元素

"3"

redis 127.0.0.1:6379> rpop l2 ? ? ? // 從尾部彈出一個元素

"2"

redis 127.0.0.1:6379> llen l2 ? ? ? // 計算隊列的長度

(integer) 1

?

擴(kuò)展

1.1 LPUSHX ?當(dāng)且隊列存在的情況下 在頭部插入數(shù)據(jù)?

?

LPUSHX key value

?

實踐:

redis 127.0.0.1:6379> LLEN empty1 ? ? ? ? ? ?

(integer) 0

redis 127.0.0.1:6379> lpushx empty1 hh ? ?// 將數(shù)據(jù)推入一個空的隊列,結(jié)果失敗

(integer) 0

redis 127.0.0.1:6379> lpush l3 hh?

(integer) 1

redis 127.0.0.1:6379> lpushx l3 hh2 ? ? ?// 將數(shù)據(jù)推入一個存在的隊列頭部,成功

(integer) 2

redis 127.0.0.1:6379> lrange l3 0 -1?

1) "hh2"

2) "hh"

?

1.2 RPUSHX 當(dāng)且隊列存在的情況下 在尾部插入數(shù)據(jù) ?基本同LPUSHX

redis 127.0.0.1:6379> rpushx empty1 hh3 ? ?// 嘗試將數(shù)據(jù)推入不存在隊列的尾部,失敗。

(integer) 0

redis 127.0.0.1:6379> rpushx l3 hh3 ? ? ? ?// 將數(shù)據(jù)推入存在隊列的尾巴,成功。

(integer) 3

redis 127.0.0.1:6379> lrange l3 0 -1

1) "hh2"

2) "hh"

3) "hh3"

?

1.3 BLPOP 對于LPOP的擴(kuò)展 ,阻塞式的獲取數(shù)據(jù)

BLPOP key [key ...] timeout

?

它是 LPOP 命令的阻塞版本,當(dāng)給定列表內(nèi)沒有任何元素可供彈出的時候,連接將被 BLPOP 命令阻塞,直到等待超時或發(fā)現(xiàn)可彈出元素為止。

當(dāng)給定多個 key 參數(shù)時,按參數(shù) key 的先后順序依次檢查各個列表,彈出第一個非空列表的頭元素。

?

假設(shè)現(xiàn)在有三個隊列 job 為空 , command ?request 不為空 。那么就開始輪訓(xùn)所有的key ,若是空,則跳過 。執(zhí)行順序為 job ==> command ==> request

?

實踐:

redis 127.0.0.1:6379> del job?

(integer) 0

redis 127.0.0.1:6379> lpush command hh?

(integer) 1

redis 127.0.0.1:6379> lpush request kk?

(integer) 1

redis 127.0.0.1:6379> blpop job command kk?

(error) ERR timeout is not an integer or out of range

redis 127.0.0.1:6379> blpop job command kk ?100?

1) "command"

2) "hh"

redis 127.0.0.1:6379>

?

查看其是如何阻塞的

在第一個圖片中,上面那個終端一直阻塞著。

?

在第二個終端中輸入數(shù)據(jù)后,上面終端取得數(shù)據(jù)并返回。

?

1.4 BRPOP 對于rpop的擴(kuò)展,原理基本同BLPOP

?

2.LREM

語法:LREM key count value

釋義:根據(jù)參數(shù) count 的值,移除列表中與參數(shù) value 相等的元素。

count 的值可以是以下幾種:

? count > 0 : 從表頭開始向表尾搜索,移除與 value 相等的元素,數(shù)量為 count 。

? count < 0 : 從表尾開始向表頭搜索,移除與 value 相等的元素,數(shù)量為 count 的絕對值。

? count = 0 : 移除表中所有與 value 相等的值。

?

實踐:

redis 127.0.0.1:6379> lpush l1 hello?

(integer) 1

redis 127.0.0.1:6379> lpush l1 world?

(integer) 2

redis 127.0.0.1:6379> lpush l1 hello?

(integer) 3

redis 127.0.0.1:6379> lpush l1 world?

(integer) 4

redis 127.0.0.1:6379> lpush l1 hello?

(integer) 5

redis 127.0.0.1:6379> lrange l1 0 -1?

1) "hello"

2) "world"

3) "hello"

4) "world"

5) "hello"

redis 127.0.0.1:6379> ? ? ? ? ? ? ? ? ? ? ? // 數(shù)據(jù)準(zhǔn)備完成

?

redis 127.0.0.1:6379> lrem l1 2 hello ? ? ?// 從頭部開始掃描,移除了兩個hello?

(integer) 2

redis 127.0.0.1:6379> lrem l1 -1 hello ? ? // 從尾部開始掃描,移除了一個hello

(integer) 1

redis 127.0.0.1:6379> lrange l1 0 -1 ? ? ? // 現(xiàn)在只剩下 world了

1) "world"

2) "world"

redis 127.0.0.1:6379> lrem l1 0 world ? ? // 移除隊中所有的world?

(integer) 2

redis 127.0.0.1:6379> lrange l1 0 -1 ? ? ?// 已經(jīng)被清空了。

(empty list or set)

redis 127.0.0.1:6379>?

?

3.LSET

語法:LSET key index value

釋義:將列表 key 下標(biāo)為 index 的元素的值設(shè)置為 value 。

實踐:

redis 127.0.0.1:6379> lpush l1 hello?

(integer) 1

redis 127.0.0.1:6379> lpush l1 world?

(integer) 2

redis 127.0.0.1:6379> lset l1 1 ?--- ? ? ?// 下標(biāo)為1的數(shù)據(jù)被替換成 --- 了

OK

redis 127.0.0.1:6379> lrange l1 0 -1

1) "world"

2) "---"

redis 127.0.0.1:6379> lset l1 3 hh ? ? ? // 超出下標(biāo)進(jìn)行設(shè)置的話,報錯

(error) ERR index out of range

redis 127.0.0.1:6379> exist l2?

(error) ERR unknown command 'exist'

redis 127.0.0.1:6379> exists l2 ? ? ? ? ?// 對不存在的隊列進(jìn)行設(shè)置的話,報錯

(integer) 0

redis 127.0.0.1:6379> lset l2 0 hh?

(error) ERR no such key

redis 127.0.0.1:6379>?

?

?

4 LTRIM

語法:LTRIM key start stop

釋義:對一個列表進(jìn)行修剪(trim),就是說,讓列表只保留指定區(qū)間內(nèi)的元素,不在指定區(qū)間之內(nèi)的元素都將被刪除。

?超出范圍的下標(biāo)值不會引起錯誤。

?如果 start 下標(biāo)比列表的最大下標(biāo) end ( LLEN list 減去 1 )還要大,或者 start > stop , LTRIM 返回一個空列表(因為 LTRIM 已經(jīng)將整個列表清空)。

?如果 stop 下標(biāo)比 end 下標(biāo)還要大,Redis將 stop 的值設(shè)置為 end?

?

實踐:

redis 127.0.0.1:6379> lrange l1 0 -1 ? ?// 新建一個隊列?

1) "h"

2) "e"

3) "l"

4) "l"

5) "o"

redis 127.0.0.1:6379> ltrim l1 0 3 ? ? // 只截取前四個?

OK

redis 127.0.0.1:6379> lrange l1 0 -1

1) "h"

2) "e"

3) "l"

4) "l"

redis 127.0.0.1:6379> ltrim l1 0 10 ? ?// stop下標(biāo)大于隊列長度 則 stop=隊列長度

OK

redis 127.0.0.1:6379> lrange l1 0 -1

1) "h"

2) "e"

3) "l"

4) "l"

redis 127.0.0.1:6379> lset l1 10 20 ?// start stop 都大于 隊列長度 且 start < stop 清空隊列

(empty list or set)

redis 127.0.0.1:6379> ltrim l1 3 1 ? ? // start ?< stop 清空隊列

OK

redis 127.0.0.1:6379> lrange l1 10 20?

(empty list or set)

?

5.LINDEX

語法:LINDEX key index

釋義:返回列表 key 中,下標(biāo)為 index 的元素。

?下標(biāo)(index)參數(shù) start 和 stop 都以 0 為底,也就是說,以 0 表示列表的第一個元素,以 1 表示列表的第二個元素,以此類推。

?你也可以使用負(fù)數(shù)下標(biāo),以 -1 表示列表的最后一個元素, -2 表示列表的倒數(shù)第二個元素,以此類推。

?

實踐:

redis 127.0.0.1:6379> lpush l1 1?

(integer) 1

redis 127.0.0.1:6379> lpush l1 2

(integer) 2

redis 127.0.0.1:6379> lpush l1 3

(integer) 3

redis 127.0.0.1:6379> lindex l1 0 ? ? ? ? ? ?// 取下標(biāo)為0的數(shù)據(jù)

"3"

redis 127.0.0.1:6379> lrange l1 0 -1?

1) "3"

2) "2"

3) "1"

redis 127.0.0.1:6379> lindex l1 -1 ? ? ? ? ?// 取最后一個數(shù)據(jù)

"1"

redis 127.0.0.1:6379>?

?

6. LINSERT ?類似于LSET,一個是根據(jù)下標(biāo)來插入,一個是根據(jù)pivot來插入數(shù)據(jù)。

語法:LINSERT key BEFORE|AFTER pivot value

釋義:將值 value 插入到列表 key 當(dāng)中,位于值 pivot 之前或之后。

? 當(dāng) pivot 不存在于列表 key 時,不執(zhí)行任何操作。

? 當(dāng) key 不存在時, key 被視為空列表,不執(zhí)行任何操作。

? 如果 key 不是列表類型,返回一個錯誤。

?

實踐:

redis 127.0.0.1:6379> lrange l1 0 -1?

1) "redis"

2) "hello"

3) "world"

4) "hello"

redis 127.0.0.1:6379> linsert l1 after hello after-insert ? ? ? ? ?// 在第一個找到的hello后面插入了一個數(shù)據(jù)?

(integer) 5

redis 127.0.0.1:6379> lrange l1 0 -1?

1) "redis"

2) "hello"

3) "after-insert"

4) "world"

5) "hello"

redis 127.0.0.1:6379> linsert l1 before hello before-insert ? ? ? // 在第一個找到的hello前面插入了一個數(shù)據(jù)?

(integer) 6

redis 127.0.0.1:6379> lrange l1 0 -1?

1) "redis"

2) "before-insert"

3) "hello"

4) "after-insert"

5) "world"

6) "hello"

redis 127.0.0.1:6379> linsert l1 before hoho before-insert ? ? ? // 對于 pivot 不存在的列表,插入失敗

(integer) -1

redis 127.0.0.1:6379> lrange l1 0 -1?

1) "redis"

2) "before-insert"

3) "hello"

4) "after-insert"

5) "world"

6) "hello"

redis 127.0.0.1:6379> linsert l2 before hoho before-insert ? ? ? // 插入一個空列表,直接報錯

(integer) 0

?

7. RPOPLPUSH?

語法:RPOPLPUSH source destination

釋義:

命令 RPOPLPUSH 在一個原子時間內(nèi),執(zhí)行以下兩個動作:

?將列表 source 中的最后一個元素(尾元素)彈出,并返回給客戶端。

?將 source 彈出的元素插入到列表 destination ,作為 destination 列表的的頭元素。

舉個例子,你有兩個列表 source 和 destination , source 列表有元素 a, b, c , destination 列表有元素 x, y, z ,執(zhí)行 RPOPLPUSH source destination 之后, source 列表包含元素 a, b , destination 列表包含元素 c, x, y, z ,并且元素 c 會被返回給客戶端。

?如果 source 不存在,值 nil 被返回,并且不執(zhí)行其他動作。

?如果 source 和 destination 相同,則列表中的表尾元素被移動到表頭,并返回該元素,可以把這種特殊情況視作列表的旋轉(zhuǎn)(rotation)操作。

?

實踐:

redis 127.0.0.1:6379> lrange l1 0 -1?

1) "c"

2) "b"

3) "a"

redis 127.0.0.1:6379> lrange l2 0 -1

1) "3"

2) "2"

3) "1"

redis 127.0.0.1:6379> rpoplpush l1 l2 ? ? //將l1尾部的數(shù)據(jù)彈出進(jìn)入l2的頭部,并將這個彈出的數(shù)據(jù)返回

"a"

redis 127.0.0.1:6379> lrange l1 0 -1?

1) "c"

2) "b"

redis 127.0.0.1:6379> lrange l2 0 -1?

1) "a"

2) "3"

3) "2"

4) "1"

redis 127.0.0.1:6379>

?

8. BRPOPLPUSH?

語法:BRPOPLPUSH source destination timeout

釋義:BRPOPLPUSH 是 RPOPLPUSH 的阻塞版本,當(dāng)給定列表 source 不為空時, BRPOPLPUSH 的表現(xiàn)和 RPOPLPUSH 一樣。

? ?當(dāng)列表 source 為空時, BRPOPLPUSH 命令將阻塞連接,直到等待超時,或有另一個客戶端對 source 執(zhí)行 LPUSH 或 RPUSH 命令為止。

? ?超時參數(shù) timeout 接受一個以秒為單位的數(shù)字作為值。超時參數(shù)設(shè)為 0 表示阻塞時間可以無限期延長(block indefinitely) 。

?

實踐:

我們設(shè)置等超時時間為1000?

?

在另一個客戶端添加數(shù)據(jù)后,就轉(zhuǎn)移了數(shù)據(jù)

?

?

?



已有 0 人發(fā)表留言,猛擊->>這里<<-參與討論


ITeye推薦