学习-3

作者: NotFoundW | 来源:发表于2020-04-07 22:31 被阅读0次

Decr和Incr相关命令

Code

func DecrAndIncr(c redis.Conn) {
    colorlog.Info("Func DecrAndIncr()...")
    c.Do("SET", "myNumber", "123")
    //  Decr 1
    decrOneResult, err := c.Do("DECR", "myNumber")
    if err != nil {
        colorlog.Error(err.Error())
        return
    }
    fmt.Println("After decr 1, the value of myNumber is:", decrOneResult)
    //  Decr any
    decrAnyResult, err := c.Do("DECRBY", "myNumber", 2)
    if err != nil {
        colorlog.Error(err.Error())
        return
    }
    // here result should be 120
    fmt.Println("After decr 2, the value of myNumber is:", decrAnyResult)
    //  Incr 1
    incrOneResult, err := c.Do("INCR", "myNumber")
    if err != nil {
        colorlog.Error(err.Error())
        return
    }
    // here result should be 121
    fmt.Println("After incr 1, the value of myNumber is:", incrOneResult)
    //  Incr any
    incrAnyResult, err := c.Do("INCRBY", "myNumber", 3)
    if err != nil {
        colorlog.Error(err.Error())
        return
    }
    // here result should be 124
    fmt.Println("After incr 3, the value of myNumber is:", incrAnyResult)
    //  Incr float
    incrAnyFloatResult,err:=redis.Float64(c.Do("INCRBYFLOAT","myNumber",0.24))
    if err != nil {
        colorlog.Error(err.Error())
        return
    }
    // here result should be 124.24
    fmt.Println("After incr float 0.24, the value of myNumber is:", incrAnyFloatResult)
}

Output

1.png

Strlen和Append命令

Code

func StrLenAndAppend(c redis.Conn) {
    colorlog.Info("Func StrLenAndAppend()...")
    c.Do("SET", "newStr", "original")
    originalLength, err := c.Do("STRLEN", "newStr")
    if err != nil {
        colorlog.Error(err.Error())
        return
    }
    // here length should be 8
    fmt.Println(originalLength)
    //  Append a new string to the end of original string
    appendLength, err := c.Do("APPEND", "newStr", "Append")
    if err != nil {
        colorlog.Error(err.Error())
        return
    }
    // here length should be 14
    fmt.Println(appendLength)
    //  and newStr should be "originalAppend"
    result, _ := redis.String(c.Do("Get", "newStr"))
    fmt.Println(result)
}

Output

2.png

getrange和setrange命令

Code

func GetAndSetRange(c redis.Conn) {
    colorlog.Info("Func GetAndSetRange()...")
    c.Do("SET", "swan", "black")
    //get sub string of white from 0 to 1: bl
    resultGetRange, err := redis.String(c.Do("GETRANGE", "swan", 0, 1))
    if err != nil {
        colorlog.Error(err.Error())
        return
    }
    fmt.Println("get sub string of white from 0 to 1:", resultGetRange)
    // get sub string of white from end to end : k
    resultGetRangeEnd, err := redis.String(c.Do("GETRANGE", "swan", 4, 4))
    if err != nil {
        colorlog.Error(err.Error())
        return
    }
    fmt.Println("get sub string of white from end to end:", resultGetRangeEnd)
    // get sub string of white from end to end+1 or end+more: k
    resultGetRangeEndMore, err := redis.String(c.Do("GETRANGE", "swan", 4, 5))
    if err != nil {
        colorlog.Error(err.Error())
        return
    }
    fmt.Println("get sub string of white from end to end+1:", resultGetRangeEndMore)

    //  only set a char on a index
    //  If set successfully, will return new string's length.
    setOneResult, err := c.Do("SETRANGE", "swan", 0, "c")
    if err != nil {
        colorlog.Error(err.Error())
        return
    }
    // new length should be 5. set "c" char on index 0 : clack
    fmt.Println("after set c char on index 0, new string's length is:", setOneResult)
    newStr, _ := redis.String(c.Do("GET", "swan"))
    fmt.Println("after set c char on index 0, new string is:", newStr)
    //  set multiple chars on index 0
    setTwoResult, err := c.Do("SETRANGE", "swan", 0, "white")
    if err != nil {
        colorlog.Error(err.Error())
        return
    }
    // new length should be 5. set "white" chars on index 0 : white
    fmt.Println("after set 'white' chars on index 0, new string's length is:", setTwoResult)
    newStr, _ = redis.String(c.Do("GET", "swan"))
    fmt.Println("after set 'white' chars on index 0, new string is:", newStr)
    //  set a char/chars on length index, will return new string's length. And append the char/chars to the end.
    oldLength := len(newStr)
    setEndResult, err := c.Do("SETRANGE", "swan", oldLength, "Bird")
    if err != nil {
        colorlog.Error(err.Error())
        return
    }
    //  new length should be 9. set "Bird" chars on length index: whiteBird
    fmt.Println("after set 'Bird' chars on length index, new string's length is:", setEndResult)
    newStr, _ = redis.String(c.Do("GET", "swan"))
    fmt.Println("after set 'Bird' chars on length index, new string is:", newStr)
    // set a char/chars on length+1 index, will return new string's length.
    // And will append a space to the end, then append the char/chars to the end.
    oldLength = len(newStr)
    setFurtherResult, err := c.Do("SETRANGE", "swan", oldLength+1, "Fly")
    if err != nil {
        colorlog.Error(err.Error())
        return
    }
    //  new length should be 13. set "Fly" chars on length+1 index: whiteBirdFly
    fmt.Println("after set 'Fly' chars on length+1 index, new string's length is:", setFurtherResult)
    newStr, _ = redis.String(c.Do("GET", "swan"))
    fmt.Println("after set 'Fly' chars on length+1 index, new string is:", newStr)
}

Output

3.png

mset和mget命令

Code

func MsetAndMget(c redis.Conn) {
    colorlog.Info("Func msetAndMget()...")
    _, err := c.Do("MSET", "m1", "v1", "m2", "v2", "m3", "v3")
    if err != nil {
        colorlog.Error(err.Error())
        return
    }
    mgetResult, err := redis.Strings(c.Do("MGET", "m1", "m2", "m3"))
    if err != nil {
        colorlog.Error(err.Error())
        return
    }
    for _, v := range mgetResult {
        fmt.Println(v)
    }
}

Output

4.png

Dump和Restore命令

Code

func DumpAndRestore(c redis.Conn) {
    colorlog.Info("Func DumpAndRestore()...")
    c.Do("SET", "fish", "long")
    // if key doesn't exist, dump will return nil
    dumpRes, err := c.Do("DUMP", "fish")
    if err != nil {
        colorlog.Error(err.Error())
        return
    } else if dumpRes == nil {
        colorlog.Error("The key doesn't exist.")
        return
    }
    //  delete fish key, then restore it
    c.Do("DEL", "fish")
    _, err = c.Do("RESTORE", "fish", 0, dumpRes)
    if err != nil {
        colorlog.Error(err.Error())
        return
    }
    fishVal, _ := redis.String(c.Do("GET", "fish"))
    fmt.Println("Restore value to fish key:", fishVal)
    //  RESTORE command can also restore value to a new key that doesn't exist
    // Will create the new key with dumped value
    _, err = c.Do("RESTORE", "newFish", 0, dumpRes)
    if err != nil {
        colorlog.Error(err.Error())
        return
    }
    newFishVal, _ := redis.String(c.Do("GET", "newFish"))
    fmt.Println("Restore value to newFish key:", newFishVal)
    //  RESTORE command can not restore value to a existing key, will return error
    _, err = c.Do("RESTORE", "fish", 0, dumpRes)
    if err != nil {
        colorlog.Error(err.Error())
        return
    }
}

Output

5.png

Setbit和Getbit命令

Code

func SetbitAndGetbit(c redis.Conn) {
    colorlog.Info("Func SetbitAndGetbit()...")
    c.Do("SET", "dove", "a")
    //  The ascii code for a is 97, and the binary value of 97 is 01100001
    //  Let's set the 6 offset to 1, means 01100011, it's 99, c.
    setbitRes, err := c.Do("SETBIT", "dove", 6, 1)
    if err != nil {
        colorlog.Error(err.Error())
        return
    }
    fmt.Println("set bit on existing key dove, result should be:", setbitRes)
    //  we can get bit on 6 offset, it should be 1 now.
    getbitRes, err := c.Do("GETBIT", "dove", 6)
    if err != nil {
        colorlog.Error(err.Error())
        return
    }
    fmt.Println("get bit of 6 offset on existing key dove, result should be:", getbitRes)
    newVal, _ := redis.String(c.Do("GET", "dove"))
    fmt.Println("The new value of dove key should be:", newVal)
}

Output

6.png

MSETNX命令

Code

func Msetnx(c redis.Conn) {
    colorlog.Info("Func Msetnx()...")
    // It is assumed that the keys mg1 and mg2 do not exist.
    res1, err := c.Do("MSETNX", "mg1", "v1", "mg2", "v2")
    if err != nil {
        colorlog.Error(err.Error())
        return
    }
    //  If msetnx successfully, the result should be 1.
    fmt.Println("msetnx two keys that don't exist, result is:", res1)
    // Now mg1 and mg2 are existing. Msetnx mg1 and other keys that don't exist, will fail and return 0
    res2, err := c.Do("MSETNX", "mg1", "v2", "mg3", "v3")
    if err != nil {
        colorlog.Error(err.Error())
        return
    }
    //  msetnx should be failed, the result should be 0.
    fmt.Println("msetnx mg1 which is existing and other keys that don't exist, result is:", res2)
    //  the value of mg1 still is v1, and mg3 key doesn't exist.
    mg1Val, _ := redis.String(c.Do("GET", "mg1"))
    fmt.Println("The value of mg1 key is still:", mg1Val)
    isExist, err := c.Do("EXISTS", "mg3")
    if err != nil {
        colorlog.Error(err.Error())
    } else {
        if isExist == int64(1) {
            fmt.Println("The mg3 exists")
        } else {
            fmt.Println("The mg3 doesn't exist")
        }
    }
}

Output

7.png

GETSET命令

Code

func Getset(c redis.Conn) {
    colorlog.Info("Func Getset()...")
    //  Getset will return the old value.
    c.Do("SET", "rhino", "big")
    oldVal, err := redis.String(c.Do("GETSET", "rhino", "small"))
    if err != nil {
        colorlog.Error(err.Error())
        return
    }
    fmt.Println("The old value of key is:", oldVal)
}

Output

8.png

相关文章

  • 学习3

    1.ALT键 任务栏显示相应字母的快捷键,按相应字母,再次出现此命令下的快捷键,如F 3.公式求值键可看到每一步的...

  • 学习3

    看来已经习惯了这种生活模式,不会觉得有多冲突,累了就放松一下,无聊就学习,看似无差别的生活却少了几分忧虑。不能说是...

  • 学习-3

    Decr和Incr相关命令 Code Output Strlen和Append命令 Code Output get...

  • 学习——3

    今天发的作品被标记违规。 心里猛地一惊,觉得自己已经很小心了,非常注意自己的措辞,怎么还会违规呢? 所有敏感话题、...

  • 学习3

    王双雄《私域提款机》第3节课!怎么找到源源不断的客户所在的“精准社群”? 一、心法: 充分站在你客户的角度,去换位...

  • 学习3

    1.双方父母婚后购买的不动产,产权登记在一方子女名下的,认定为夫妻双方按份。 2.一方个人财产在婚后产生的利益,除...

  • 学习3

    前路纵然坎坷,也要全力以赴。 每个人在生活中都会经历一段难熬的时光,工作上的不如意,感情上的患得患失。 日子过得一...

  • 新港实验小学学习有感

    学习时间:2019年3月6日星期三 学习主题:习作研讨 学习内容:3-6年级习作教学3、4单元案例分享 学习收获:...

  • TensorFlow实战(三)-简介和开发环境搭建

    3-1 什么是tensorflow 3-2 tensorflow和其他机器学习库的对比 3-3 如何学习tenso...

  • 3号总结,4号计划

    3 德语学习 get 4 德语学习

网友评论

      本文标题:学习-3

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