Redis學習筆記--list類型及操作1

list 是一個鏈表結構,主要功能是push、pop、獲取一個范圍的所有值等等,操作中key 理解為鏈表的名字。

Redis 的list 類型其實就是一個每個子元素都是string 類型的雙向鏈表。鏈表的最大長度是(2的32 次方)。我們可以通過push,pop 操作從鏈表的頭部或者尾部添加刪除元素。這使得list既可以用作棧,也可以用作隊列。

有意思的是list 的pop 操作還有阻塞版本的,當我們[lr]pop 一個list 對象時,如果list 是空,或者不存在,會立即返回nil。但是阻塞版本的b[lr]pop 可以則可以阻塞,當然可以加超時時間,超時后也會返回nil。為什么要阻塞版本的pop 呢,主要是為了避免輪詢。舉個簡單的例子如果我們用list 來實現一個工作隊列。執行任務的thread 可以調用阻塞版本的pop 去獲取任務這樣就可以避免輪詢去檢查是否有任務存在。當任務來時候工作線程可以立即返回,也可以避免輪詢帶來的延遲。說了這么多,接下來看一下實際操作的方法吧:

lpush
在key 對應list 的頭部添加字符串元素

redis 127.0.0.1:6379> lpush mylist "world"
(integer) 1
redis 127.0.0.1:6379> lpush mylist "hello"
(integer) 2
redis 127.0.0.1:6379> lrange mylist 0 -1
1) "hello"
2) "world"
redis 127.0.0.1:6379>

在此處我們先插入了一個world,然后在world 的頭部插入了一個hello。其中lrange 是用于取mylist 的內容。


rpush
在key 對應list 的尾部添加字符串元素

redis 127.0.0.1:6379> rpush mylist2 "hello"
(integer) 1
redis 127.0.0.1:6379> rpush mylist2 "world"
(integer) 2
redis 127.0.0.1:6379> lrange mylist2 0 -1
1) "hello"
2) "world"
redis 127.0.0.1:6379>

linsert
在key 對應list 的特定位置之前或之后添加字符串元素

redis 127.0.0.1:6379> rpush mylist3 "hello"
(integer) 1
redis 127.0.0.1:6379> rpush mylist3 "world"
(integer) 2
redis 127.0.0.1:6379> linsert mylist3 before "world" "there"
(integer) 3
redis 127.0.0.1:6379> lrange mylist3 0 -1
1) "hello"
2) "there"
3) "world"
redis 127.0.0.1:6379>

在此處我們先插入了一個hello,然后在hello 的尾部插入了一個world,然后又在world 的前面插入了there。


lset
設置list中下表的元素值(下標從0開始)

redis 127.0.0.1:6379> rpush mylist4 "one"
(integer) 1
redis 127.0.0.1:6379> rpush mylist4 "two"
(integer) 2
redis 127.0.0.1:6379> rpush mylist4 "three"
(integer) 3
redis 127.0.0.1:6379> lset mylist4 0 "four"
OK
redis 127.0.0.1:6379> lset mylist4 -2 "five"
OK
redis 127.0.0.1:6379> lrange mylist4 0 -1
1) "four"
2) "five"
3) "three"
redis 127.0.0.1:6379>

lrem
從key對應list中刪除count個和value相同的元素。
count>0時,按從頭到尾的順序刪除,具體如下:

redis 127.0.0.1:6379> rpush mylist5 "hello"
(integer) 1
redis 127.0.0.1:6379> rpush mylist5 "hello"
(integer) 2
redis 127.0.0.1:6379> rpush mylist5 "foo"
(integer) 3
redis 127.0.0.1:6379> rpush mylist5 "hello"
(integer) 4
redis 127.0.0.1:6379> lrem mylist5 2 "hello"
(integer) 2
redis 127.0.0.1:6379> lrange mylist5 0 -1
1) "foo"
2) "hello"
redis 127.0.0.1:6379>

lrange
lrange key start stop:返回存儲在 key 的列表里指定范圍內的元素。 start 和 end 偏移量都是基于0的下標,即list的第一個元素下標是0(list的表頭),第二個元素下標是1,以此類推。

偏移量也可以是負數,表示偏移量是從list尾部開始計數。 例如, -1 表示列表的最后一個元素,-2 是倒數第二個,以此類推。

redis> RPUSH mylist "one"
(integer) 1
redis> RPUSH mylist "two"
(integer) 2
redis> RPUSH mylist "three"
(integer) 3
redis> LRANGE mylist 0 0
1) "one"
redis> LRANGE mylist -3 2
1) "one"
2) "two"
3) "three"
redis> LRANGE mylist -100 100
1) "one"
2) "two"
3) "three"
redis> LRANGE mylist 5 10
(empty list or set)
redis> 

#count<0 時,按從尾到頭的順序刪除,具體如下:
redis 127.0.0.1:6379> rpush mylist6 "hello"
(integer) 1
redis 127.0.0.1:6379> rpush mylist6 "hello"
(integer) 2
redis 127.0.0.1:6379> rpush mylist6 "foo"
(integer) 3
redis 127.0.0.1:6379> rpush mylist6 "hello"
(integer) 4
redis 127.0.0.1:6379> lrem mylist6 -2 "hello"
(integer) 2
redis 127.0.0.1:6379> lrange mylist6 0 -1
1) "hello"
2) "foo"
redis 127.0.0.1:6379>

#count=0 時,刪除全部,具體如下:
redis 127.0.0.1:6379> rpush mylist7 "hello"
(integer) 1
redis 127.0.0.1:6379> rpush mylist7 "hello"
(integer) 2
redis 127.0.0.1:6379> rpush mylist7 "foo"
(integer) 3
redis 127.0.0.1:6379> rpush mylist7 "hello"
(integer) 4
redis 127.0.0.1:6379> lrem mylist7 0 "hello"
(integer) 3
redis 127.0.0.1:6379> lrange mylist7 0 -1
1) "foo"
redis 127.0.0.1:6379>
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 第二部分 redis Redis官網 https://redis.ioRedis 中國鏡像Redis中國用戶組(C...
    霄峰閱讀 562評論 0 4
  • Redis 的 list 類型其實就是一個每個子元素都是 string 類型的雙向鏈表。主要功能是 push、po...
    鬭闢閱讀 322評論 0 0
  • redis是一個以key-value存儲的非關系型數據庫。有五種數據類型,string、hashes、list、s...
    林ze宏閱讀 1,022評論 0 0
  • 前言 Redis作為cache服務器,支持多種數據結構,String、List、Hash、Set、Zset。多種數...
    小小小碼農閱讀 1,034評論 0 1
  • 介紹 REDIS_LIST (列 表) 是 LPUSH 、 LRANGE 等 命 令 的 操 作 對 象, 它 使...
    狗語閱讀 386評論 0 0