Home > Database > Redis > body text

What are the common operation commands of Redis basic data type List?

王林
Release: 2023-05-28 20:43:24
forward
3038 people have browsed it


    ##Redis data type List operation

    In redis , you can cleverly use lists into stacks, queues, blocking queues, etc.

    1. Push insert elements

    1. lpush, insert at the head
    Insert one value or multiple values ​​into the head of the list.

    lpush list onelpush list twolpush list three
    Copy after login

    What are the common operation commands of Redis basic data type List?

    Use

    lpush, the l here can be regarded as left, that is, inserted on the left, so the current list is like this[three , two, one].

    2. rpush, insert
    Next, use

    rpush to insert elements on the right, that is, at the end of the list.

    rpush list right1 right2
    Copy after login

    At this time, the list is like this

    [three, two, one, right1, right2]. Use lrange list 0 -1 to get:

    What are the common operation commands of Redis basic data type List?

    2. Range Get elements through intervals

    can be imagined as in python

    range(), get the element by passing in the start and end subscripts.

    lrange list 1 4
    Copy after login

    What are the common operation commands of Redis basic data type List?

    3. Pop removal element

    Since the above can be added to the left and right, then the removal can naturally be divided into left and right.

    Removal operation returns the removed element.

    1. lpop removes the left side
    lpop list
    Copy after login

    What are the common operation commands of Redis basic data type List?

    2. rpop removes the right side
    rpop list
    Copy after login

    What are the common operation commands of Redis basic data type List?

    4. lindex gets the element through the subscript

    lindex list 1
    Copy after login

    What are the common operation commands of Redis basic data type List?

    5. llen gets the list length

    Returns the length of the list.

    llen list
    Copy after login

    What are the common operation commands of Redis basic data type List?

    6. lrem removes specified elements

    You can specify the element to be removed, and specify the number.

    lrem list 2 yi222
    Copy after login

    There are 3 yi222s in the list now, and I want to remove 2 of them.

    What are the common operation commands of Redis basic data type List?

    7. ltrim trimming

    Through

    ltrim, only the specified part is retained, and other parts are removed, and the intercepted list changes. .

    ltrim list 1 4
    Copy after login

    Here the subscripts from 1 to 4 are retained, and the others are removed.

    What are the common operation commands of Redis basic data type List?

    8. Combination command rpoplpush remove and add

    This is a combination command, remove the last element of the list and add it to another list .

    rpoplpush list list2
    Copy after login

    Here

    list is the original list, list2 is the target list, and if the target list does not exist, it will be created.

    What are the common operation commands of Redis basic data type List?

    9. exists Determine whether the key exists

    exists list
    Copy after login

    Returns 1 if it exists, and returns 0 if it does not exist.

    What are the common operation commands of Redis basic data type List?

    10. lset, set the value of the specified subscript

    lset list 1 test
    Copy after login

    When the index parameter is out of range, or LSET is performed on an empty list, an error is returned.

    What are the common operation commands of Redis basic data type List?

    11. linsert, insert the value before/after the specified position

    before

    linsert list before test before_test
    Copy after login

    This is the element

    testBefore, insert element before_test.

    What are the common operation commands of Redis basic data type List?

    After

    linsert list after test after_test
    Copy after login

    This is after element

    test, insert element after_test.

    What are the common operation commands of Redis basic data type List?

    The above is the detailed content of What are the common operation commands of Redis basic data type List?. For more information, please follow other related articles on the PHP Chinese website!

    Related labels:
    source:yisu.com
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!