Search This Blog

2018/09/23

Exploring redis commands - part 2

Redis Lists are simply lists of strings, sorted by insertion order. You can add elements in Redis lists in the head or the tail of the list.
Max length of list is (2^32 - 1).

    127.0.0.1:6379> auth sangram
    OK

    LPUSH creates list where last pushed element is at index 0.

        127.0.0.1:6379> LPUSH subjects english
        (integer) 1
        127.0.0.1:6379> LPUSH subjects marathi
        (integer) 2
        127.0.0.1:6379> LPUSH subjects hindi
        (integer) 3
        127.0.0.1:6379> LPUSH subjects history
        (integer) 4
        127.0.0.1:6379> LPUSH subjects geography
        (integer) 5
        127.0.0.1:6379> LPUSH subjects economics
        (integer) 6
        127.0.0.1:6379> LPUSH subjects maths
        (integer) 7
        127.0.0.1:6379> LPUSH subjects science
        (integer) 8
        127.0.0.1:6379> LLEN subjects
        (integer) 8


    LINDEX get element at specified index in list.

        127.0.0.1:6379> LINDEX subjects 0
        "science"
        127.0.0.1:6379> LINDEX subjects 1
        "maths"
        127.0.0.1:6379> LINDEX subjects 3
        "geography"

    BLPOP command removes the first element in a list it returns the first element, if available, or blocks the client for specific time to execute any command.
        127.0.0.1:6379> BLPOP subjects 100
        1) "subjects"
        2) "science"
        127.0.0.1:6379> LLEN subjects
        (integer) 7

    BRPOP removes element from bottom of list.

        127.0.0.1:6379> BRPOP subjects 100
        1) "subjects"
        2) "english"
        127.0.0.1:6379> LLEN subjects
        (integer) 6

    LRANGE prints  values in list based on start Index & end Index

        127.0.0.1:6379> LRANGE subjects 0 20
         1) "mathsssds"
         2) "mathss"
         3) "maths"
         4) "maths"
         5) "maths"
         6) "english"
         7) "maths"
         8) "economics"
         9) "geography"
        10) "history"
        11) "hindi"
        12) "marathi"

    LPUSHX insert push at head of list only if list is already defined.Here "subjectss" is not defined to insertion fails at 1st attempts then succeed.
        127.0.0.1:6379> LPUSHX subjectss mathsssds
        (integer) 0
        127.0.0.1:6379> LPUSH subjectss mathsssds
        (integer) 1
        127.0.0.1:6379> LPUSHX subjectss mathsssds
        (integer) 2

        RPUSH -insert at bottom of list
        127.0.0.1:6379> RPUSH subjects geometry
        (integer) 13
        127.0.0.1:6379> LRANGE subjects 0 20
         1) "mathsssds"
         2) "mathss"
         3) "maths"
         4) "maths"
         5) "maths"
         6) "english"
         7) "maths"
         8) "economics"
         9) "geography"
        10) "history"
        11) "hindi"
        12) "marathi"
        13) "geometry"

      below LREM removes 2 occurances "maths" from list starting search from top to bottom.if instead 2 its negative value say -2 them search start from bottom to top,if instead of 2 its 0 then remove     all occurances of search string in list.


        127.0.0.1:6379> LRANGE subjects 0 20
         1) "mathsssds"
         2) "mathss"
         3) "maths"
         4) "maths"
         5) "maths"
         6) "english"
         7) "maths"
         8) "economics"
         9) "geography"
        10) "history"
        11) "hindi"
        12) "marathi"
        13) "geometry"
        127.0.0.1:6379> LREM subjects 2 maths
        (integer) 2
        127.0.0.1:6379> LRANGE subjects 0 20
         1) "mathsssds"
         2) "mathss"
         3) "maths"
         4) "english"
         5) "maths"
         6) "economics"
         7) "geography"
         8) "history"
         9) "hindi"
        10) "marathi"
        11) "geometry"

    RPUSHX command inserts the value at the bottom of the list  only if the list already exists

        127.0.0.1:6379> RPUSHX s math
        (integer) 0
        127.0.0.1:6379> RPUSHX subject math
        (integer) 0
        127.0.0.1:6379> RPUSHX subjects math
        (integer) 8

       RPOP removes and return last element from bottom in list

    127.0.0.1:6379> LRANGE subjects 0 20
    1) "english"
    2) "economics"
    3) "geography"
    4) "history"
    5) "hindi"
    6) "marathi"
    7) "geometry"
    8) "math"

    127.0.0.1:6379> RPOP subjects
    "math"

    127.0.0.1:6379> LRANGE subjects 0 20
    1) "english"
    2) "economics"
    3) "geography"
    4) "history"
    5) "hindi"
    6) "marathi"
    7) "geometry"

    LPOP removes and returns the first element in top list.

        127.0.0.1:6379> LRANGE subjects 0 20
        1) "english"
        2) "economics"
        3) "geography"
        4) "history"
        5) "hindi"
        6) "marathi"
        7) "geometry"
        127.0.0.1:6379> LPOP subjects
        "english"

    RPOPLPUSH -removes element from bottom of list and insert into new list


    127.0.0.1:6379> LRANGE subjects 0 20
    1) "economics"
    2) "geography"
    3) "history"
    4) "hindi"
    5) "marathi"
    6) "geometry"

    127.0.0.1:6379> RPOPLPUSH subjects s
    "geometry"

    127.0.0.1:6379> LRANGE subjects 0 20
    1) "economics"
    2) "geography"
    3) "history"
    4) "hindi"
    5) "marathi

    127.0.0.1:6379> LRANGE s 0 20
    1) "geometry"

No comments:

Post a Comment