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

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

王林
王林forward
2023-05-28 20:43:243060browse


    ##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

    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

    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

    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

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

    2. rpop removes the right side
    rpop list

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

    4. lindex gets the element through the subscript

    lindex list 1

    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

    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

    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

    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

    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

    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

    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

    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

    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!

    Statement:
    This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete