go-redis

。。。

redigo

使用 go get 命令安装 redigo:

go get github.com/gomodule/redigo/redis

redigo 库中的 Conn 接口是操作 Redis 的主要接口。

type Conn interface {
 // Close closes the connection.
 Close() error
 // Err returns a non-nil value when the connection is not usable.
 Err() error
 // Do sends a command to the server and returns the received reply.
 Do(commandName string, args ...interface{}) (reply interface{}, err error)
 // Send writes the command to the client's output buffer.
 Send(commandName string, args ...interface{}) error
 // Flush flushes the output buffer to the Redis server.
 Flush() error
 // Receive receives a single reply from the Redis server
 Receive() (reply interface{}, err error)
}

例:

    // 连接redis
    c, err := redis.Dial("tcp", "192.168.151.158:12004")
    if err != nil {
        fmt.Println("Connect to redis error", err)
        return
    } else {
        fmt.Println("Connect to redis ok")
    }
    defer c.Close()

    // 密码鉴权
    _, err = c.Do("AUTH", "cqrm123151qaz2WSX")
    if err != nil {
        fmt.Println("auth failed:", err)
    } else {
        fmt.Println("auth ok:")
    }

    // 写入数据
    _, err = c.Do("SET", "gokey", "gokeyvalue")
    if err != nil {
        fmt.Println("redis set failed:", err)
    } else {
        fmt.Println("redis set ok")
    }

    // 读取数据
    value, err := redis.String(c.Do("GET", "gokey"))
    if err != nil {
        fmt.Println("redis get failed:", err)
    } else {
        fmt.Printf("Get gokey: %v \n", value)
    }

    // 删除key
    _, err = c.Do("DEL", "gokey")
    if err != nil {
        fmt.Println("redis delelte failed:", err)
    }

    // 读取数据
    value, err = redis.String(c.Do("GET", "gokey"))
    if err != nil {
        fmt.Println("redis get failed:", err)
    } else {
        fmt.Printf("Get gokey: %v \n", value)
    }

    // 组装JSON字符串
    key := "profile"
    imap := map[string]string{"username": "666", "phonenumber": "888"}
    jsonvalue, _ := json.Marshal(imap)

    // 写入JSON字符串
    n, err := c.Do("SETNX", key, jsonvalue)
    if err != nil {
        fmt.Println(err)
    }
    if n == int64(1) {
        fmt.Println("success")
    }

    // 读取JSON字符串
    var imapGet map[string]string
    valueGet, err := redis.Bytes(c.Do("GET", key))
    if err != nil {
        fmt.Println(err)
    }

    // 解析JSON
    errShal := json.Unmarshal(valueGet, &imapGet)
    if errShal != nil {
        fmt.Println(err)
    }
    fmt.Println(imapGet["username"])
    fmt.Println(imapGet["phonenumber"])

    // 设置过期时间为6秒
    ret, _ := c.Do("EXPIRE", key, 6)
    if ret == int64(1) {
        fmt.Println("success")
    }

    // 休眠8秒
    time.Sleep(8 * time.Second)

    // 判断key是否存在
    is_key_exit, err := redis.Bool(c.Do("EXISTS", key))
    if err != nil {
        fmt.Println("error:", err)
    } else {
        fmt.Printf("exists or not: %v \n", is_key_exit)
    }

sh

Connect to redis ok
auth failed: ERR Client sent AUTH, but no password is set
redis set ok
Get gokey: gokeyvalue
redis get failed: redigo: nil returned
success
666
888
success
exists or not: false


参考:
Golang 官方推荐使用的 Redis 客户端 redigo
Go实战–golang中使用redis(redigo和go-redis/redis)
DENIED Redis is running in protected mode报错解决办法

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐