Go:redis

时间:Feb. 17, 2020 分类:

目录:

安装

go get -u github.com/go-redis/redis

连接

package main

import (
    "github.com/go-redis/redis"
    "fmt"
)

var redisdb *redis.Client

func initRedis() (err error) {
    redisdb = redis.NewClient(&redis.Options{
        Addr: "172.17.0.2:6379",
        Password: "",
        DB: 0,
    })
    _, err = redisdb.Ping().Result()
    return
}

func main() {
    err := initRedis()
    if err != nil {
        fmt.Printf("connect redis failed, err:%v\n", err)
    }
    fmt.Println("connect redis success!")
}

使用

GET/SET

package main

import (
    "github.com/go-redis/redis"
    "fmt"
)

var redisdb *redis.Client

func initRedis() (err error) {
    redisdb = redis.NewClient(&redis.Options{
        Addr: "172.17.0.2:6379",
        Password: "",
        DB: 0,
    })
    _, err = redisdb.Ping().Result()
    return
}

func redisdemo() {
    err := redisdb.Set("score", 100, 0).Err()
    if err != nil {
        fmt.Printf("set score failed, err:%v\n", err)
        return
    }
    val, err := redisdb.Get("score").Result()           
    if err != nil {
         fmt.Printf("get score failed, err:%v\n", err)
         return
    }
    fmt.Println("score", val)

    val2, err := redisdb.Get("name").Result()
    if err == redis.Nil {
        fmt.Println("name does not exist")
    } else if err != nil {
        fmt.Printf("get name failed, err:%v\n", err)
        return
    } else {
        fmt.Println("name", val2)
    }
}

func main() {
    err := initRedis()
    if err != nil {
        fmt.Printf("connect redis failed, err:%v\n", err)
    }
    fmt.Println("connect redis success!")
    redisdemo()
}

执行结果

connect redis success!
score 100
name does not exist

redis查询

172.17.0.2:6379> get score
"100"

ZSET

package main

import (
    "github.com/go-redis/redis"
    "fmt"
)

var redisdb *redis.Client

func initRedis() (err error) {
    redisdb = redis.NewClient(&redis.Options{
        Addr: "172.17.0.2:6379",
        Password: "",
        DB: 0,
    })
    _, err = redisdb.Ping().Result()
    return
}

func redisdemo() {
    zsetKey := "language_rank"
    languages := []*redis.Z{
        &redis.Z{Score: 90.0, Member: "Golang"},
        &redis.Z{Score: 98.0, Member: "Java"},
        &redis.Z{Score: 95.0, Member: "Python"},
        &redis.Z{Score: 97.0, Member: "JavaScript"},
        &redis.Z{Score: 99.0, Member: "C/C++"},
    }
    // ZADD
    num, err := redisdb.ZAdd(zsetKey, languages...).Result()
    if err != nil {
        fmt.Printf("zadd failed, err:%v\n", err)
    return
    }
    fmt.Printf("zadd %d succ.\n", num)
    // 把Golang的分数加10
    newScore, err := redisdb.ZIncrBy(zsetKey, 10.0, "Golang").Result()
    if err != nil {
        fmt.Printf("zincrby failed, err:%v\n", err)
        return
    }
    fmt.Printf("Golang's score is %f now.\n", newScore)
    // 取分数最高的3个
    ret, err := redisdb.ZRevRangeWithScores(zsetKey, 0, 2).Result()
    if err != nil {
        fmt.Printf("zrevrange failed, err:%v\n", err)
        return
    }
    for _, z := range ret {
        fmt.Println(z.Member, z.Score)
    }

    // 取95~100分的
    op := &redis.ZRangeBy{
        Min: "95",
        Max: "100",
    }
    ret, err = redisdb.ZRangeByScoreWithScores(zsetKey, op).Result()
    if err != nil {
        fmt.Printf("zrangebyscore failed, err:%v\n", err)
        return
    }
    for _, z := range ret {
        fmt.Println(z.Member, z.Score)
    }
}

func main() {
    err := initRedis()
    if err != nil {
        fmt.Printf("connect redis failed, err:%v\n", err)
    }
    fmt.Println("connect redis success!")
    redisdemo()
}

打印结果

connect redis success!
zadd 5 succ.
Golang's score is 100.000000 now.
Golang 100
C/C++ 99
Java 98
Python 95
JavaScript 97
Java 98
C/C++ 99
Golang 100

redis查询结果

172.17.0.2:6379> ZRANGE language_rank 0 100
1) "Python"
2) "JavaScript"
3) "Java"
4) "C/C++"
5) "Golang"
172.17.0.2:6379> zrange language_rank 0 4 withscores
 1) "Python"
 2) "95"
 3) "JavaScript"
 4) "97"
 5) "Java"
 6) "98"
 7) "C/C++"
 8) "99"
 9) "Golang"
10) "100"