美文网首页
Redis-List-1

Redis-List-1

作者: NotFoundW | 来源:发表于2020-04-09 11:36 被阅读0次

    以下代码片段,涵盖对redis list的命令操作:

    1. LPUSH
    2. LRANGE
    3. LLEN
    4. LPUSHX
    5. LINSERT
    6. LPOP
    7. LSET
    8. LINDEX
    9. LREM
    10. LTRIM

    Code

    func ListOne(c redis.Conn) {
        colorlog.Info("Func ListOne()...")
        // For test, need to delete the listOne at first.
        c.Do("DEL", "listOne")
        // create a list
        length, err := c.Do("LPUSH", "listOne", 1, 2, 3, 4)
        if err != nil {
            colorlog.Error(err.Error())
            return
        }
        fmt.Println("New list's length is:", length)
        //  read the list
        values, err := redis.Ints(c.Do("LRANGE", "listOne", 0, length))
        if err != nil {
            colorlog.Error(err.Error())
            return
        }
        for _, v := range values {
            fmt.Println(v)
        }
        //  get length of list
        listLength, err := c.Do("LLEN", "listOne")
        if err != nil {
            colorlog.Error(err.Error())
            return
        }
        fmt.Println("list's length is:", listLength)
        //  use LPUSHX to add a element to the head of list. And this list must be a existing list.
        newLength, err := c.Do("LPUSHX", "listOne", 5)
        if err != nil {
            colorlog.Error(err.Error())
            return
        }
        fmt.Println("list's new length is:", newLength)
        //  read the list
        values, _ = redis.Ints(c.Do("LRANGE", "listOne", 0, newLength))
        for _, v := range values {
            fmt.Println(v)
        }
        //  use LPUSHX to add a element to the head of a list that doesn't exist, if no error, will return 0
        fakeLength, err := c.Do("LPUSHX", "fakeList", 5)
        if err != nil {
            colorlog.Error(err.Error())
            return
        }
        fmt.Println("return code of LPUSHX to a list that doesn't exist is:", fakeLength)
        //  LINSERT: insert a element before or after a element.
        //  1. If successfully, return length of new list
        insertLength, err := c.Do("LINSERT", "listOne", "BEFORE", 5, 6)
        if err != nil {
            colorlog.Error(err.Error())
            return
        }
        fmt.Println("insert a element 6 before 5, length is:", insertLength)
        values, _ = redis.Ints(c.Do("LRANGE", "listOne", 0, insertLength))
        for _, v := range values {
            fmt.Println(v)
        }
        insertLength, err = c.Do("LINSERT", "listOne", "AFTER", 1, 0)
        if err != nil {
            colorlog.Error(err.Error())
            return
        }
        fmt.Println("insert a element 0 after 1, length is:", insertLength)
        values, _ = redis.Ints(c.Do("LRANGE", "listOne", 0, insertLength))
        for _, v := range values {
            fmt.Println(v)
        }
        //  2. If no specified element, return -1.
        insertRes, err := c.Do("LINSERT", "listOne", "AFTER", 11, 0)
        if err != nil {
            colorlog.Error(err.Error())
            return
        }
        fmt.Println("insert a element before/after a element that doesn't exist, result is:", insertRes)
        //  3. If key doesn't exist or list is null, return 0
        insertResNull, err := c.Do("LINSERT", "NewFakeList", "AFTER", 11, 0)
        if err != nil {
            colorlog.Error(err.Error())
            return
        }
        fmt.Println("insert a element before/after a element into a list that doesn't exist, result is:", insertResNull)
        //  create a null list
        c.Do("LPUSH", "NullList", 1)
        c.Do("LPOP", "NullList")
        insertResNull, err = c.Do("LINSERT", "NullList", "AFTER", 11, 0)
        if err != nil {
            colorlog.Error(err.Error())
            return
        }
        fmt.Println("insert a element before/after a element into a null list, result is:", insertResNull)
        //  LPOP: remove and return the first element(head).
        // 1. Once remove successfully, return head element
        headElement, err := redis.Int(c.Do("LPOP", "listOne"))
        if err != nil {
            colorlog.Error(err.Error())
            return
        }
        fmt.Println("remove the head element:", headElement)
        //  2. Once key doesn't exist, return nil
        nilHeadElement, err := c.Do("LPOP", "MoreNewFakeKey")
        if err != nil {
            colorlog.Error(err.Error())
            return
        }
        if nilHeadElement == nil {
            fmt.Println("head element is nil, key doesn't exist.")
        }
        //  LSET: set value on specified index
        _, err = c.Do("LSET", "listOne", 0, 10)
        if err != nil {
            colorlog.Error(err.Error())
            return
        }
        values, _ = redis.Ints(c.Do("LRANGE", "listOne", 0, -1))
        fmt.Println("LSET 10 on index 0, then list will be:")
        for _, v := range values {
            fmt.Println(v)
        }
        //  LINDEX: get value by index, if index out of range, return nil.
        specifiedIndexValue, err := redis.Int(c.Do("LINDEX", "listOne", 0))
        if err != nil {
            colorlog.Error(err.Error())
            return
        }
        fmt.Println("value of index 0 is:", specifiedIndexValue)
        specifiedIndexNilValue, err := c.Do("LINDEX", "listOne", 100)
        if err != nil {
            colorlog.Error(err.Error())
            return
        }
        if specifiedIndexNilValue == nil {
            fmt.Println("value of index out of range is nil")
        }
        //  LREM: lrem key count value
        // create a list for test
        c.Do("DEL", "lremList")
        c.Do("LPUSH", "lremList", 0, 1, 1, 2, 3, 2, 4, 5, 6, 5, 7, 8, 8)
        //  1. if count > 0, search elements from head to end, remove "count" "value"s, return the real count which be removed
        removeCount, err := c.Do("LREM", "lremList", 1, 2)
        if err != nil {
            colorlog.Error(err.Error())
            return
        }
        fmt.Println(removeCount)
        values, _ = redis.Ints(c.Do("LRANGE", "lremList", 0, -1))
        fmt.Println("LREM the front 2, then list will be:")
        for _, v := range values {
            fmt.Println(v)
        }
        //  2. if count < 0, search elements from end to head, remove "|count|" "value"s,
        //  return the real count which be removed
        removeCount, err = c.Do("LREM", "lremList", -1, 5)
        if err != nil {
            colorlog.Error(err.Error())
            return
        }
        fmt.Println("removeCount:", removeCount)
        values, _ = redis.Ints(c.Do("LRANGE", "lremList", 0, -1))
        fmt.Println("LREM the end 5, then list will be:")
        for _, v := range values {
            fmt.Println(v)
        }
        //  3. if count == 0, remove all "value"s.
        removeCount, err = c.Do("LREM", "lremList", 0, 8)
        if err != nil {
            colorlog.Error(err.Error())
            return
        }
        fmt.Println("removeCount:", removeCount)
        values, _ = redis.Ints(c.Do("LRANGE", "lremList", 0, -1))
        fmt.Println("LREM all the 8, then list will be:")
        for _, v := range values {
            fmt.Println(v)
        }
        //  4. if value doesn't exist in list, return 0.
        removeCount, err = c.Do("LREM", "lremList", 0, 100)
        if err != nil {
            colorlog.Error(err.Error())
            return
        }
        fmt.Println("removeCount:", removeCount)
        //  LTRIM: ltrim key start end
        //  keep elements from start index to end index
        c.Do("DEL", "trimList")
        c.Do("LPUSH", "trimList", 0, 1, 2, 3, 4)
        _, err = c.Do("LTRIM", "trimList", 0, 1)
        if err!=nil {
            colorlog.Error(err.Error())
            return
        }
        values, _ = redis.Ints(c.Do("LRANGE", "trimList", 0, -1))
        fmt.Println("Ltrim from 0 to 1 index, then list will be:")
        for _, v := range values {
            fmt.Println(v)
        }
        //  keep elements from start index to end index
        c.Do("DEL", "trimList")
        c.Do("LPUSH", "trimList", 0, 1, 2, 3, 4)
        _, err = c.Do("LTRIM", "trimList", 0, -2)
        if err!=nil {
            colorlog.Error(err.Error())
            return
        }
        values, _ = redis.Ints(c.Do("LRANGE", "trimList", 0, -1))
        fmt.Println("Ltrim from 0 to -2 index, then list will be:")
        for _, v := range values {
            fmt.Println(v)
        }
    }
    

    Output

    $ go run main.go 
    [INF]2020/04/09 11:40:27 Func ListOne()...
    New list's length is: 4
    4
    3
    2
    1
    list's length is: 4
    list's new length is: 5
    5
    4
    3
    2
    1
    return code of LPUSHX to a list that doesn't exist is: 0
    insert a element 6 before 5, length is: 6
    6
    5
    4
    3
    2
    1
    insert a element 0 after 1, length is: 7 
    6
    5
    4
    3
    2
    1
    0
    insert a element before/after a element that doesn't exist, result is: -1
    insert a element before/after a element into a list that doesn't exist, result is: 0
    insert a element before/after a element into a null list, result is: 0
    remove the head element: 6
    head element is nil, key doesn't exist.
    LSET 10 on index 0, then list will be:
    10
    4
    3
    2
    1
    0
    value of index 0 is: 10
    value of index out of range is nil
    1
    LREM the front 2, then list will be:
    8
    8
    7
    5
    6
    5
    4
    3
    2
    1
    1
    0
    removeCount: 1
    LREM the end 5, then list will be:
    8
    8
    7
    5
    6
    4
    3
    2
    1
    1
    0
    removeCount: 2
    LREM all the 8, then list will be:
    7
    5
    6
    4
    3
    2
    1
    1
    0
    removeCount: 0
    Ltrim from 0 to 1 index, then list will be:
    4
    3
    Ltrim from 0 to -2 index, then list will be:
    4
    3
    2
    1
    

    相关文章

      网友评论

          本文标题:Redis-List-1

          本文链接:https://www.haomeiwen.com/subject/nqxxmhtx.html