学习-2

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

查看key的过期时间

Code

func SingleKeyTtl(c redis.Conn) {
    colorlog.Info("Func SingleKeyTtl()...")
    _, err := c.Do("SET", "ttlKey", "Dunk", "EX", "360")
    if err != nil {
        colorlog.Error("redis set failed: " + err.Error())
        return
    }
    time.Sleep(3 * time.Second)
    //  get the remaining time(seconds)
    remainSeconds, err := c.Do("TTL", "ttlKey")
    if err != nil {
        colorlog.Error(err.Error())
    } else {
        fmt.Println("ttlKey remains seconds:", remainSeconds)
    }
    //  get the remaining time(milliseconds)
    remainMilliSeconds, err := c.Do("PTTL", "ttlKey")
    if err != nil {
        colorlog.Error(err.Error())
    } else {
        fmt.Println("ttlKey remains milliseconds:", remainMilliSeconds)
    }

    //  Once persist key, ttl and pttl will return -1
    result, err := c.Do("PERSIST", "ttlKey")
    if err != nil {
        colorlog.Error(err.Error())
    } else if result == int64(1) {
        fmt.Println("Persist key successfully")
    } else if result == int64(0) {
        fmt.Println("key persisted already")
    }
    //  get the remaining time(seconds)
    remainSeconds, err = c.Do("TTL", "ttlKey")
    if err != nil {
        colorlog.Error(err.Error())
    } else {
        fmt.Println("ttlKey remains seconds:", remainSeconds)
    }
    //  get the remaining time(milliseconds)
    remainMilliSeconds, err = c.Do("PTTL", "ttlKey")
    if err != nil {
        colorlog.Error(err.Error())
    } else {
        fmt.Println("ttlKey remains milliseconds:", remainMilliSeconds)
    }

    // If check the remaining time of key that doesn't exist, ttl and pttl will return -2
    //  get the remaining time(seconds)
    remainSeconds, err = c.Do("TTL", "fakeTtlKey")
    if err != nil {
        colorlog.Error(err.Error())
    } else {
        fmt.Println("fakeTtlKey remains seconds:", remainSeconds)
    }
    //  get the remaining time(milliseconds)
    remainMilliSeconds, err = c.Do("PTTL", "fakeTtlKey")
    if err != nil {
        colorlog.Error(err.Error())
    } else {
        fmt.Println("fakeTtlKey remains milliseconds:", remainMilliSeconds)
    }
}

Output

1.png

用RENAME命令修改key

Code

func SingleKeyRename(c redis.Conn) {
    colorlog.Info("Func SingleKeyRename()...")
    // Rename an existing key
    _, err := c.Do("SET", "OldKey", "ray")
    if err != nil {
        colorlog.Error("redis set failed: " + err.Error())
        return
    }
    _, err = c.Do("RENAME", "OldKey", "NewKey")
    if err != nil {
        colorlog.Error(err.Error())
        return
    }
    isExist, _ := c.Do("EXISTS", "OldKey")
    fmt.Println("OldKey doesn't exist:", isExist)
    isExist, _ = c.Do("EXISTS", "NewKey")
    fmt.Println("NewKey exist:", isExist)

    //  Rename a key that does not exist
    _, err = c.Do("RENAME", "OldKey", "MoreNewKey")
    if err != nil {
        colorlog.Error(err.Error())
    }

    //  Rename an existing key to an another existing key by RENAME command
    _, err = c.Do("SET", "whiteBird", "white")
    if err != nil {
        colorlog.Error("redis set failed: " + err.Error())
        return
    }
    _, err = c.Do("SET", "blackBird", "black")
    if err != nil {
        colorlog.Error("redis set failed: " + err.Error())
        return
    }
    // value of whiteBird will cover value of blackBird. And whiteBird key will disappear.
    _, err = c.Do("RENAME", "whiteBird", "blackBird")
    if err != nil {
        colorlog.Error(err.Error())
        return
    }
    valueOfBlackBird, _ := redis.String(c.Do("GET", "blackBird"))
    fmt.Println("Get value of blackBird:", valueOfBlackBird)
}

Output

2.png

用RENAMENX命令修改key

Code

func SingleKeyRenamenx(c redis.Conn) {
    colorlog.Info("Func SingleKeyRenamenx()...")
    // Rename an existing key
    _, err := c.Do("SET", "nx1", "wade")
    if err != nil {
        colorlog.Error("redis set failed: " + err.Error())
        return
    }
    _, err = c.Do("SET", "nx2", "wade")
    if err != nil {
        colorlog.Error("redis set failed: " + err.Error())
        return
    }
    // RENAMENX command only can rename an existing key to a key that doesn't exist
    res1, err := c.Do("RENAMENX", "nx1", "nx2")
    if err != nil {
        colorlog.Error(err.Error())
    }
    // if no error, here will get 0, means rename failed.
    fmt.Println("The result is ",res1)
    res2, err := c.Do("RENAMENX", "nx1", "nx3")
    if err != nil {
        colorlog.Error(err.Error())
    }
    // if no error, here will get 1, means rename successfully.
    fmt.Println("The result is ",res2)

    // RENAMENX also can not rename a key that doesn't exist
    _, err = c.Do("RENAMENX", "fakeNx", "newFakeNx")
    if err != nil {
        colorlog.Error(err.Error())
    }
}

Output

image.png

查看单个key的value的type

Code

func SingleValueOfKeyType(c redis.Conn) {
    colorlog.Info("Func SingleValueOfKeyType()...")
    // To save space, ignore the set error
    c.Do("SET", "ball", "don't lie")
    c.Do("SET", "soccer", 99)
    c.Do("SET", "NBA", false)
    c.Do("SET", "complex", 3.1415)
    str := "hello"
    by := []byte(str)
    c.Do("SET", "tree", by)
    //  get type of value-> all results should be string. Because string is the basic data type of redis.
    re1, _ := c.Do("TYPE", "ball")
    fmt.Println(re1)
    re2, _ := c.Do("TYPE", "soccer")
    fmt.Println(re2)
    re3, _ := c.Do("TYPE", "NBA")
    fmt.Println(re3)
    re4, _ := c.Do("TYPE", "complex")
    fmt.Println(re4)
    re5, _ := c.Do("TYPE", "tree")
    fmt.Println(re5)
    // if key doesn't exist, will return none.
    re6, _ := c.Do("TYPE", "nothing")
    fmt.Println(re6)
}

Output

4.png

随意返回一个key

Code

func RandomKey(c redis.Conn) {
    colorlog.Info("Func SingleValueOfKeyType()...")
    key, err := redis.String(c.Do("RANDOMKEY"))
    if err != nil {
        colorlog.Error(err.Error())
        return
    }
    fmt.Println(key)
}

Output

5.png

查找所有符合给定模式( pattern)的 key

Code

func PatternKeys(c redis.Conn) {
    colorlog.Info("Func PatternKeys()...")
    // To keep things short, do not handle set errors.
    c.Do("SET", "computer1", "c1")
    c.Do("SET", "computer2", "c2")
    c.Do("SET", "computer3", "c3")
    results, err := redis.Strings(c.Do("KEYS", "computer*"))
    if err != nil {
        colorlog.Error(err.Error())
        return
    }
    for _, k := range results {
        fmt.Println(k)
    }
}

Output

6.png

使用SETNX命令设定一个全新的key

Code

func SingleKeySetNx(c redis.Conn) {
    colorlog.Info("Func PatternKeys()...")
    //  SETNX command only set a key which doesn't exist
    c.Do("SET", "bxj1", "1")
    result, err := c.Do("SETNX", "bxj1", "2")
    if err != nil {
        colorlog.Error(err.Error())
        return
    }
    // here will be failed, result is 0
    fmt.Println(result)
    //  and value of bxj1 still is 1
    val, _ := redis.Int(c.Do("GET", "bxj1"))
    fmt.Println(val)

    //  SETNX can set a key which doesn't exist(assuming that this NewestKey doesn't exist)
    resultNew, err := c.Do("SETNX", "NewestKey", "24")
    if err != nil {
        colorlog.Error(err.Error())
        return
    }
    //  here will be successful, result is 1
    fmt.Println(resultNew)
    // and value of NewestKey is 24
    val, _ = redis.Int(c.Do("GET", "NewestKey"))
    fmt.Println(val)
}

Output

7.png

使用SETEX命令设定会过期的key

Code

func SingleKeySetEx(c redis.Conn) {
    colorlog.Info("Func SingleKeySetEx()...")
    _, err := c.Do("SETEX", "bingo", "20", "3000")
    if err != nil {
        colorlog.Error(err.Error())
        return
    }
    time.Sleep(3 * time.Second)
    remainedTime, _ := c.Do("TTL", "bingo")
    fmt.Println(remainedTime)
}

Output

8.png

相关文章

  • Dagger2学习笔记5(关于Lazy,Provide的使用)

    Dagger2学习笔记1(基础概念学习)Dagger2学习笔记2(学习Dagger2的简单使用)Dagger2学习...

  • Dagger2学习笔记4(@Singleton 与@ Scope

    Dagger2学习笔记1(基础概念学习)Dagger2学习笔记2(学习Dagger2的简单使用)Dagger2学习...

  • Qt5学习地址

    Qt 学习之路 2(1):序(Qt 学习之路 2(1):序) Qt 学习之路 2(2):Qt 简介(Qt 学习之路...

  • 学习2

    块链和投资思考了 + 2在学习认知在做记录和写作,输出 + 3英语 — 焦虑感,是因为没有对时间的正确认识,当我们...

  • 学习2

    haofeng领导视角 自信 积极

  • 学习2

    我想我从未想过上大学后,还会熬夜。写那个入党申请书写了一个钟头,其实也不累,就是时间到一点多了!

  • 学习2

    1.取消保存时的兼容性检查:将高版本保存为低版本时可能出现兼容性检查器。文件-信息-检查问题2.文件无法保存为低版...

  • 学习2

    只有不断终身学习,才能与时俱进。 拿更新换代快速的计算机行业来说,一开始是只有团体共享的大部头的超级计算机,到后来...

  • 学习2

    时间:2017年12月5日06:40 地点:东莞 来源:李笑来【通往财富自由之路】 一、思维导图梳理架构 二、罗列...

  • 学习2

    看着自己的成绩,在小学时的自信瞬间没有了!第一次因为成绩觉得好难过,看着同学们都在为了回家怎么交代发愁的时候,我...

网友评论

      本文标题:学习-2

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