Redis Series-Storage list main Operation function summary (GO)

Source: Internet
Author: User

Before summing up the list, you need to understand several concepts related to list:

List: A left-to-right queue, a personal understanding is more like a stack, a regular mode, an advanced list of elements, and a post-out.

Header element: The first element of the leftmost list.

Footer element: The last element at the right end of the list. A list that does not contain any elements becomes an empty list.

1) New

A) Lpush

Syntax: Lpush key Value[value]

Explanation: Inserting one or more elements into a table header. If more than one value is inserted, follow the left-to-right order. Returns the number of inserted elements

[Plain]View Plaincopy
    1. [Email protected] ~]# REDIS-CLI
    2. Redis 127.0.0.1:6379> lpush lst.user Zhangsan #插入一个元素
    3. (integer) 1
    4. Redis 127.0.0.1:6379> lpush lst.user zhangsan Lisi #插入多个元素, List allows duplicate elements to be inserted
    5. (integer) 3


b) lpushx

Syntax: Lpushx key value

Explanation: Inserting a header element can only be inserted if and only if the list key exists. Returns the number of elements in a list

[Plain]View Plaincopy
    1. Redis 127.0.0.1:6379> lpushx lst.user Wangwu #列表存在
    2. (integer) 4
    3. Redis 127.0.0.1:6379> lpushx Lst.tech Wangwu #列表不存在, not inserted
    4. (integer) 0

c) Rpush

Syntax: Rpush key [value]

Explanation: Inserts one or more values into the queue's tail. Multiple values are added from left to right. Returns the number of elements in a list

[Plain]View Plaincopy
    1. Redis 127.0.0.1:6379> rpush lst.user ls005 #列表存在
    2. (integer) 5
    3. Redis 127.0.0.1:6379> rpush lst.tech tec01 tec02 tec03 #列表不存在
    4. (integer) 3

D) rpushx

Syntax: Rpushx key value

Explanation: The value is inserted at the end of the list, and is added only if the list key exists. Returns the number of list elements

[Plain]View Plaincopy
    1. Redis 127.0.0.1:6379> rpushx Lst.tech tec04 #key exist
    2. (integer) 4
    3. Redis 127.0.0.1:6379> rpushx lst.sub englist #key not present
    4. (integer) 0

e) Linsert

Syntax: Linsert key Before|after Pivot value

Explanation: Inserts a value into the front or back of the pivot. Returns the number of list elements. If the reference point pivot does not exist, do not insert. If there are multiple pivot, whichever is closest to the table header

[Plain]View Plaincopy
    1. REDIS 127.0.0.1:6379> LINSERT LST.TECH AFTER TEC04  tec06   #后面插   
    2. (integer)  5   
    3. redis  127.0.0.1:6379> linsert lst.tech before tec06 tec05  #前面插   
    4. (integer)  6   
    5. redis 127.0.0.1:6379> linsert  lst.tech before tec08 tec07  #参照点不存在,   
    6. (integer)  -1   
    7. redis 127.0.0.1:6379> linsert lst.user after zhangsan  zhangsan01   #列表中有多个pivot, the first one from left to right is the   
    8. (integer)  6  

2) query

A) lindex

Syntax: Lindex key index

Explanation: Gets the elements of the list by indexing index. index>=0, 0 table header, 1 second element, and so on, index<0, 1, footer, 2 the penultimate element, and so on

[Plain]View Plaincopy
    1. Redis 127.0.0.1:6379> lindex lst.user 0 #表头
    2. "Wangwu"
    3. Redis 127.0.0.1:6379> lindex lst.user-1 #表尾
    4. "Ls005"
    5. Redis 127.0.0.1:6379> lindex lst.user 2 #第三个元素
    6. "Zhangsan"
    7. Redis 127.0.0.1:6379> lindex lst.user-2 #倒数第二个元素
    8. "Zhangsan"

b) Lrange

Syntax: Lrange key start stop

Explanation: Gets some column elements that specify the start and end ranges. 0: Table header,-1: Footer. Returns an empty list if the stop specified element is to the left of start

[Plain]View Plaincopy
    1. redis 127.0.0.1:6379> lrange lst.user 0 -1 # return all   
    2. 1)   "Wangwu" &NBSP;&NBSP;
    3. 2)   "Lisi" &NBSP;&NBSP;
    4. 3 )   "Zhangsan" &NBSP;&NBSP;
    5. 4)   "Zhangsan01" &NBSP;&NBSP;
    6. 5)   "Zhangsan"   
    7. 6)   "ls005" &NBSP;&NBSP;
    8. Redis 127.0.0.1:6379> lrange  lst.user -1 0   #返回空   
    9. (empty list or  Set)   
    10. redis 127.0.0.1:6379> lrange lst.user 1 2  # Return multiple   ,
    11. 1)   "Lisi" &NBSP;&NBSP;
    12. 2)   "Zhangsan" &NBSP;&NBSP;
    13. redis 127.0.0.1:6379> lrange lst.user 1 1    #返回一个元素   
    14. 1)   "Lisi" &NBSP;&NBSP;


3) Modify

LSet

Syntax: LSet key index value

Explanation: Sets the value of the specified index in the list, and if the specified index does not exist, an error

[Plain]View Plaincopy
    1. redis 127.0.0.1:6379> lset lst.user 2 zhangsan1    #设置第三个元素为zhangsan1   
    2. ok  
    3. redis  127.0.0.1:6379> lrange lst.user 0 -1  
    4. 1)   "Wangwu"   
    5. 2)   "Lisi" &NBSP;&NBSP;
    6. 3)   "Zhangsan1" &NBSP;&NBSP;
    7. 4)   "Zhangsan01" &NBSP;&NBSP;
    8. 5)   "Zhangsan" &NBSP;&NBSP;
    9. 6)   "ls005"   
    10. redis 127.0.0.1:6379> lset lst.user 6 ls006  # The specified index does not exist   

4) Delete

A) LTrim

Syntax: LTrim key start stop

Explanation: Preserves the elements of the specified range and removes all other elements

[Plain]View Plaincopy
    1. Redis 127.0.0.1:6379> LTrim Lst.user 0-2
    2. Ok
    3. Redis 127.0.0.1:6379> lrange Lst.user 0-1
    4. 1) "Wangwu"
    5. 2) "Lisi"
    6. 3) "Zhangsan1"
    7. 4) "Zhangsan01"
    8. 5) "Zhangsan"

b) Lrem

Syntax: Lrem key Count value

Explanation: Remove the element equal to value, when count>0, search from the table header, remove count, and when count=0, search from the table header, remove all equals value, and when count<0, start looking at the end of the table, remove |count| A.

[Plain]View Plaincopy
  1. Redis 127.0.0.1:6379> lrange Lst.user 0-1
  2. 1) "Zhangsan"
  3. 2) "Wangwu"
  4. 3) "Lisi"
  5. 4) "Zhangsan1"
  6. 5) "Zhangsan01"
  7. 6) "Zhangsan"
  8. 7) "Lisi"
  9. 8) "Zhangsan"
  10. 9) "Lisi"
  11. "Zhangsan"
  12. Redis 127.0.0.1:6379> lrem lst.user 2 Zhangsan #移除前两个zhangsan
  13. (integer) 2
  14. Redis 127.0.0.1:6379> lrange Lst.user 0-1
  15. 1) "Wangwu"
  16. 2) "Lisi"
  17. 3) "Zhangsan1"
  18. 4) "Zhangsan01"
  19. 5) "Lisi"
  20. 6) "Zhangsan"
  21. 7) "Lisi"
  22. 8) "Zhangsan"
  23. Redis 127.0.0.1:6379> lrange lst.user-1 Zhangsan #移除最后一个zhangsan
  24. (empty list or set)
  25. Redis 127.0.0.1:6379> lrange Lst.user 0-1
  26. 1) "Wangwu"
  27. 2) "Lisi"
  28. 3) "Zhangsan1"
  29. 4) "Zhangsan01"
  30. 5) "Lisi"
  31. 6) "Zhangsan"
  32. 7) "Lisi"
  33. Redis 127.0.0.1:6379> lrem lst.user 0 Lisi #移除所有lisi
  34. (integer) 3
  35. Redis 127.0.0.1:6379> lrange Lst.user 0-1
  36. 1) "Wangwu"
  37. 2) "Zhangsan1"
  38. 3) "Zhangsan01"
  39. 4) "Zhangsan"
  40. Redis 127.0.0.1:6379>

c) Rpop

Syntax: Rpop key

Explanation: Removing and returning a footer element

[Plain]View Plaincopy
    1. Redis 127.0.0.1:6379> Rpop Lst.user
    2. "Zhangsan"

D) Lpop

Syntax: Lpop key

Explanation: Removing and returning a footer element

[Plain]View Plaincopy
    1. Redis 127.0.0.1:6379> Lpop Lst.user
    2. "Wangwu"

5) Other

A) Llen

Syntax: Llen key

Explanation: Get list length

[Plain]View Plaincopy
    1. Redis 127.0.0.1:6379> Llen Lst.user
    2. (integer) 2

Reference: Http://redis.io/commands#list

Redis Series-Storage list main Operation function summary (GO)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.