Redis Series-Storage list main Operation function Summary

Source: Internet
Author: User

      1. 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 #参照点不存在, not plugged in
          6. (integer)-1
          7. Redis 127.0.0.1:6379> Linsert lst.user after Zhangsan Zhangsan01 #列表中有多个pivot, whichever is first from left to right
          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 #返回所有
          2. 1) "Wangwu"
          3. 2) "Lisi"
          4. 3) "Zhangsan"
          5. 4) "Zhangsan01"
          6. 5) "Zhangsan"
          7. 6) "ls005"
          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 #返回多个
          11. 1) "Lisi"
          12. 2) "Zhangsan"
          13. Redis 127.0.0.1:6379> lrange lst.user 1 1 #返回一个元素
          14. 1) "Lisi"

        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"
          6. 3) "Zhangsan1"
          7. 4) "Zhangsan01"
          8. 5) "Zhangsan"
          9. 6) "ls005"
          10. Redis 127.0.0.1:6379> LSet lst.user 6 ls006 #指定索引不存在
          11. (Error) ERR index out of range
        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 Operations function summary

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.