LIST 整體結(jié)構(gòu)圖
?
圖畫的太大了,只能放地址:
http://dl.iteye.com/upload/picture/pic/115935/8e96f42d-3a7b-3cea-85ae-997496aa9521.jpg
?
LIST列表的操作,可想而知,對(duì)于列表我們需要的具備的功能列表
加入列表:
? 從頭部加入 ? LPUSH?
? 從底部加入 ? RPUSH
彈出列表
? 從頭部彈出 ? LPOP
? 從底部彈出 ? RPOP
截取子列表 ? ? LRANGE
計(jì)算列表的長(zhǎng)度 LLEN
?
?
實(shí)踐:
redis 127.0.0.1:6379> rpush l2 1 ? ? // 從底部添加一個(gè)元素
(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 ? ? ? // 從頭部彈出一個(gè)元素
"3"
redis 127.0.0.1:6379> rpop l2 ? ? ? // 從尾部彈出一個(gè)元素
"2"
redis 127.0.0.1:6379> llen l2 ? ? ? // 計(jì)算隊(duì)列的長(zhǎng)度
(integer) 1
?
擴(kuò)展
1.1 LPUSHX ?當(dāng)且隊(duì)列存在的情況下 在頭部插入數(shù)據(jù)?
?
LPUSHX key value
?
實(shí)踐:
redis 127.0.0.1:6379> LLEN empty1 ? ? ? ? ? ?
(integer) 0
redis 127.0.0.1:6379> lpushx empty1 hh ? ?// 將數(shù)據(jù)推入一個(gè)空的隊(duì)列,結(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ù)推入一個(gè)存在的隊(duì)列頭部,成功
(integer) 2
redis 127.0.0.1:6379> lrange l3 0 -1?
1) "hh2"
2) "hh"
?
1.2 RPUSHX 當(dāng)且隊(duì)列存在的情況下 在尾部插入數(shù)據(jù) ?基本同LPUSHX
redis 127.0.0.1:6379> rpushx empty1 hh3 ? ?// 嘗試將數(shù)據(jù)推入不存在隊(duì)列的尾部,失敗。
(integer) 0
redis 127.0.0.1:6379> rpushx l3 hh3 ? ? ? ?// 將數(shù)據(jù)推入存在隊(duì)列的尾巴,成功。
(integer) 3
redis 127.0.0.1:6379> lrange l3 0 -1
1) "hh2"
2) "hh"
3) "hh3"
?
1.3 BLPOP 對(duì)于LPOP的擴(kuò)展 ,阻塞式的獲取數(shù)據(jù)
BLPOP key [key ...] timeout
?
它是 LPOP 命令的阻塞版本,當(dāng)給定列表內(nèi)沒有任何元素可供彈出的時(shí)候,連接將被 BLPOP 命令阻塞,直到等待超時(shí)或發(fā)現(xiàn)可彈出元素為止。
當(dāng)給定多個(gè) key 參數(shù)時(shí),按參數(shù) key 的先后順序依次檢查各個(gè)列表,彈出第一個(gè)非空列表的頭元素。
?
假設(shè)現(xiàn)在有三個(gè)隊(duì)列 job 為空 , command ?request 不為空 。那么就開始輪訓(xùn)所有的key ,若是空,則跳過 。執(zhí)行順序?yàn)?job ==> command ==> request
?
實(shí)踐:
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>
?
查看其是如何阻塞的
在第一個(gè)圖片中,上面那個(gè)終端一直阻塞著。
?
在第二個(gè)終端中輸入數(shù)據(jù)后,上面終端取得數(shù)據(jù)并返回。
?
1.4 BRPOP 對(duì)于rpop的擴(kuò)展,原理基本同BLPOP
?
2.LREM
語(yǔ)法:LREM key count value
釋義:根據(jù)參數(shù) count 的值,移除列表中與參數(shù) value 相等的元素。
count 的值可以是以下幾種:
? count > 0 : 從表頭開始向表尾搜索,移除與 value 相等的元素,數(shù)量為 count 。
? count < 0 : 從表尾開始向表頭搜索,移除與 value 相等的元素,數(shù)量為 count 的絕對(duì)值。
? count = 0 : 移除表中所有與 value 相等的值。
?
實(shí)踐:
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 ? ? ?// 從頭部開始掃描,移除了兩個(gè)hello?
(integer) 2
redis 127.0.0.1:6379> lrem l1 -1 hello ? ? // 從尾部開始掃描,移除了一個(gè)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 ? ? // 移除隊(duì)中所有的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
語(yǔ)法:LSET key index value
釋義:將列表 key 下標(biāo)為 index 的元素的值設(shè)置為 value 。
實(shí)踐:
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è)置的話,報(bào)錯(cuò)
(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 ? ? ? ? ?// 對(duì)不存在的隊(duì)列進(jìn)行設(shè)置的話,報(bào)錯(cuò)
(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
語(yǔ)法:LTRIM key start stop
釋義:對(duì)一個(gè)列表進(jìn)行修剪(trim),就是說(shuō),讓列表只保留指定區(qū)間內(nèi)的元素,不在指定區(qū)間之內(nèi)的元素都將被刪除。
?超出范圍的下標(biāo)值不會(huì)引起錯(cuò)誤。
?如果 start 下標(biāo)比列表的最大下標(biāo) end ( LLEN list 減去 1 )還要大,或者 start > stop , LTRIM 返回一個(gè)空列表(因?yàn)?LTRIM 已經(jīng)將整個(gè)列表清空)。
?如果 stop 下標(biāo)比 end 下標(biāo)還要大,Redis將 stop 的值設(shè)置為 end?
?
實(shí)踐:
redis 127.0.0.1:6379> lrange l1 0 -1 ? ?// 新建一個(gè)隊(duì)列?
1) "h"
2) "e"
3) "l"
4) "l"
5) "o"
redis 127.0.0.1:6379> ltrim l1 0 3 ? ? // 只截取前四個(gè)?
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)大于隊(duì)列長(zhǎng)度 則 stop=隊(duì)列長(zhǎng)度
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 都大于 隊(duì)列長(zhǎng)度 且 start < stop 清空隊(duì)列
(empty list or set)
redis 127.0.0.1:6379> ltrim l1 3 1 ? ? // start ?< stop 清空隊(duì)列
OK
redis 127.0.0.1:6379> lrange l1 10 20?
(empty list or set)
?
5.LINDEX
語(yǔ)法:LINDEX key index
釋義:返回列表 key 中,下標(biāo)為 index 的元素。
?下標(biāo)(index)參數(shù) start 和 stop 都以 0 為底,也就是說(shuō),以 0 表示列表的第一個(gè)元素,以 1 表示列表的第二個(gè)元素,以此類推。
?你也可以使用負(fù)數(shù)下標(biāo),以 -1 表示列表的最后一個(gè)元素, -2 表示列表的倒數(shù)第二個(gè)元素,以此類推。
?
實(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 ? ? ? ? ?// 取最后一個(gè)數(shù)據(jù)
"1"
redis 127.0.0.1:6379>?
?
6. LINSERT ?類似于LSET,一個(gè)是根據(jù)下標(biāo)來(lái)插入,一個(gè)是根據(jù)pivot來(lái)插入數(shù)據(jù)。
語(yǔ)法:LINSERT key BEFORE|AFTER pivot value
釋義:將值 value 插入到列表 key 當(dāng)中,位于值 pivot 之前或之后。
? 當(dāng) pivot 不存在于列表 key 時(shí),不執(zhí)行任何操作。
? 當(dāng) key 不存在時(shí), key 被視為空列表,不執(zhí)行任何操作。
? 如果 key 不是列表類型,返回一個(gè)錯(cuò)誤。
?
實(shí)踐:
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 ? ? ? ? ?// 在第一個(gè)找到的hello后面插入了一個(gè)數(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 ? ? ? // 在第一個(gè)找到的hello前面插入了一個(gè)數(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 ? ? ? // 對(duì)于 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 ? ? ? // 插入一個(gè)空列表,直接報(bào)錯(cuò)
(integer) 0
?
7. RPOPLPUSH?
語(yǔ)法:RPOPLPUSH source destination
釋義:
命令 RPOPLPUSH 在一個(gè)原子時(shí)間內(nèi),執(zhí)行以下兩個(gè)動(dòng)作:
?將列表 source 中的最后一個(gè)元素(尾元素)彈出,并返回給客戶端。
?將 source 彈出的元素插入到列表 destination ,作為 destination 列表的的頭元素。
舉個(gè)例子,你有兩個(gè)列表 source 和 destination , source 列表有元素 a, b, c , destination 列表有元素 x, y, z ,執(zhí)行 RPOPLPUSH source destination 之后, source 列表包含元素 a, b , destination 列表包含元素 c, x, y, z ,并且元素 c 會(huì)被返回給客戶端。
?如果 source 不存在,值 nil 被返回,并且不執(zhí)行其他動(dòng)作。
?如果 source 和 destination 相同,則列表中的表尾元素被移動(dòng)到表頭,并返回該元素,可以把這種特殊情況視作列表的旋轉(zhuǎn)(rotation)操作。
?
實(shí)踐:
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的頭部,并將這個(gè)彈出的數(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?
語(yǔ)法:BRPOPLPUSH source destination timeout
釋義:BRPOPLPUSH 是 RPOPLPUSH 的阻塞版本,當(dāng)給定列表 source 不為空時(shí), BRPOPLPUSH 的表現(xiàn)和 RPOPLPUSH 一樣。
? ?當(dāng)列表 source 為空時(shí), BRPOPLPUSH 命令將阻塞連接,直到等待超時(shí),或有另一個(gè)客戶端對(duì) source 執(zhí)行 LPUSH 或 RPUSH 命令為止。
? ?超時(shí)參數(shù) timeout 接受一個(gè)以秒為單位的數(shù)字作為值。超時(shí)參數(shù)設(shè)為 0 表示阻塞時(shí)間可以無(wú)限期延長(zhǎng)(block indefinitely) 。
?
實(shí)踐:
我們?cè)O(shè)置等超時(shí)時(shí)間為1000?
?
在另一個(gè)客戶端添加數(shù)據(jù)后,就轉(zhuǎn)移了數(shù)據(jù)
?
?
?
已有 0 人發(fā)表留言,猛擊->>這里<<-參與討論
ITeye推薦