go redis incr的使用

redis中的INCR命令每次对key记录增加1.

Example

127.0.0.1:6379> SET mykey 2
OK
127.0.0.1:6379> INCR mykey
(integer) 3
127.0.0.1:6379> GET mykey
“3”
127.0.0.1:6379>

使用举例

package main

import (
    "log"

    "github.com/garyburd/redigo/redis"
)


func main() {

    server := "127.0.0.1:6379"

    option := redis.DialPassword("123456")
    c, err := redis.Dial("tcp", server, option)
    if err != nil {
        log.Println("connect server failed:", err)
        return
    }

    defer c.Close()


    v, err := redis.Int64(c.Do("INCR", "mykey"))
    if err != nil {
        log.Println("INCR failed:", err)
        return
    }

    log.Println("value:", v)

}

output:

2018/05/20 12:37:50 value: 4

查看redis数据:

127.0.0.1:6379> GET mykey
“4”
127.0.0.1:6379>

参考

https://blog.csdn.net/lanyang123456/article/details/80381335

https://redis.io/commands/incr

你可能感兴趣的:(Golang,redis)