Redis學習(3)-資料類型List

來源:互聯網
上載者:User

標籤:

list類型及操作
List是一個鏈表結構,主要的功能是push、pop擷取一個範圍的所有值等等,操作中key理解為鏈表的名字,Redis的list類型其實就是一個每子項目都是string類型的雙向鏈表,我們可以通過push,pop操作從鏈表的頭部或者尾部添加刪除元素,這樣list既可以作為棧,又可以作為隊列

1.lpush
  在key對應list的頭部添加字串元素
  在此處我們先插入一個world,然後在world的頭部插入一個hello,其中lrange是用於取mylist的內容。
  命令:
  lpush mylist "world"
  lpush mylist "hello"
  lrange mylist 0 -1
  1)hello
  2)world


2.rpush
  在key對應list的尾部添加字串元素
  rpush mylist2 "hello"
  rpush mylist2 "world"
  lrange mylist2 0 -1
  1)"hello"
  2)"world"
  在此我們先插入了一個hello,然後在hello的尾部插入了一個world


3.linsert
  在key對應list的位置之前或者之後添加字串元素
  rpush mylist3 "hello"
  rpush mylist3 "world"
  linsert mylist3 before "world" "there"
  lrange mylist3 0 -1
  1)hello
  2)there
  3)world
  在此處我們先插入了一個hello,然後在hello的尾部插入了一個world,然後又在world的前面插入了there。


4.lset
  設定list中指定下標的元素值(下標從0開始)
  rpush mylist4 "one"
  rpush mylist4 "two"
  rpush mylist4 "three"
  lset mylist3 0 "four"
  lset mylist3 -2 "five"
  lrange mylist4 0 -1
  1)"four"
  2)"five"
  3)"three"
  在此處我們依次插入了one,two,three,然後將標是0的值設定為four,再將下標是-2的值設定為five。

5.lrem
  從key對應list中刪除count個和value相同的元素
  count > 0時,按從頭到尾的順序刪除,具體如下
  rpush mylist5 "hello"
  rpush mylist5 "hello"
  rpush mylist5 "foo"
  rpush mylist5 "hello"
  rpush mylist5 2 "hello"
  lrange mylist5 0 -1
  1)"foo"
  2)"hello"
  count<0時,按從尾到頭的順序刪除,具體如下:
  rpush mylist6 "hello"
  rpush mylist6 "hello"
  rpush mylist6 "foo"
  rpush mylist6 "hello"
  lrem mylist6 -2 "hello"
  lrange mylist6 0 -1
  1)"hello"
  2)"foo"
  count=0時,刪除全部,具體如下:
  rpush mylist7 "hello"
  rpush mylist7 "hello"
  rpush mylist7 "foo"
  rpush mylist7 "hello"
  lrem mylist7 0 "hello"
  lrange mylist7 0 -1
  1)"foo"


6.ltrim
  保留指定key的值範圍內的資料
  rpush mylist8 "one"
  rpush mylist8 "two"
  rpush mylist8 "three"
  rpush mylist8 "four"
  ltrim mylist8 1 -1
  lrange mylist8 0 -1
  1)two
  2)three
  3)four


7.lpop
  從list的頭部刪除元素,並返回刪除元素
  lrange mylist 0 -1
  1)"hello"
  2)"world"
  lpop mylist
  1)"hello"
  lrange mylist 0 -1
  1)"world"


8.rpop
  從list的尾部刪除元素,並返回刪除元素
  lrange mylist2 0 -1
  1)"hello"
  2)"world"
  rpop mylist
  1)"world"
  lrange mylist2 0 -1
  1)"hello"


9.rpoplpush
  從第一個list的尾部移除元素並添加到第二個list的頭部,最後返回被移除的元素值,整個操作是原子的.如果第一個list是空或者不存在返回nil:
  lrange mylist5 0 -1
  1)"three"
  2)"foo"
  3)"hello"
  lrange mylist6 0 -1
  1)"hello"
  2)"foo"
  rpoplpush mylist5 mylist6
  "hello"
  lrange mylist5 0 -1
  1)"three"
  2)"foo"
  lrange mylist6 0 -1
  1)"hello"
  2)"hello"
  3)"foo"


10.lindex
  返回名稱為key的list中index位置的元素
  lrange mylist5 0 -1
  1)"three"
  2)"foo"
  lindex mylist5 0
  "three"
  lindex mylist5 1
  "foo"


11.llen
  返回key對應list的長度
  llen mylist5
















Redis學習(3)-資料類型List

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.