一、概述

redigo 是一个redisgo client ,官方描述为:Redigo is a Go client for the Redis database.

基本特点:

  • 简单的API调用,并支持所有的redis命令。
  • 支持管道、事务、订阅以及连接池。

项目首页:https://github.com/garyburd/redigo/

官方文档:https://godoc.org/github.com/garyburd/redigo/redis

安装:go get github.com/garyburd/redigo/redis

二、基本使用方法

2.1 连接和关闭

使用Dail 方法来连接服务:

func Dial(network, address string, options ...DialOption) (Conn, error) {}

network 表示网络类型,address 是服务地址,options 是一些可选的选项,如果连接成功将返回一个redis.Conn 对象,在连接使用完毕后,使用Close() 关闭。

coon, err := redis.Dial("tcp", ":6379")
if err != nil{
    log.Error(err)
    return
}
defer coon.Close()

2.2 执行命令

执行redis命令使用Do() 方法:

Do(commandName string, args ...interface{}) (reply interface{}, err error)

commandName 是redis命令,后面接参数,例如执行一个SET 命令:

rs, err := gCoon.Do("SET", "xxx", "hello")
if err != nil{
    log.Error(err)
    return
}
log.Debug(rs)

要注意的是不能使用类似Do("SET", "xxx hello") 的命令,因为系统会把这个命令解析成set 一个名为xxx hello 的键,它的值为空。
运行后将返回一个reply 对象和错误标志err ,如果err != nilreply 中保存redis服务的响应。reply 是一个接口对象,根据命令的不同返回的形式也不同,它将会根据值的类型做如下转换:

Redis type              Go type
error                   redis.Error  //错误类型
integer                 int64  // 整形
simple string           string  //简单的字符串将会转成string类型,如OK等
bulk string             []byte or nil if value not present.  //[]byte,一般是数据返回
array                   []interface{} or nil if value not present.  // 数组

在获取到返回值后可以使用断言来转换数据类型,或者使用库中自带的Reply Helpers进行转换。

2.3 Reply Helpers

Reply Helpers 是库里提供的类型转换功能,可以把replayinterface{} 转换成我们想要的类型,包里提供了很多类型转换函数,如:

func Strings(reply interface{}, err error) ([]string, error)

可以把Do 函数返回的数据类型转换成[]string 类型,相同的函数还有Float64Int64 等等。

rs, err := redis.Strings(gCoon.Do("keys", "*"))
if err != nil{
    log.Error(err)
    return
}
log.Debug(rs)

2.4 管道

管道的使用方法设计到三个函数:

c.Send()
c.Flush()
c.Receive()

send() 方法把命令写到输出缓冲区,Flush() 把缓冲区的命令刷新到redis服务器,Receive() 函数接收redis给予的响应,三个操作共同完成一套命令流程:

c.Send("SET", "foo", "bar")
c.Send("GET", "foo")
c.Flush()
c.Receive() // reply from SET
v, err = c.Receive() // reply from GET

合并这三个操作就成了Do() 函数,官方对Do() 的描述为:

The Do method combines the functionality of the Send, Flush and Receive methods. The Do method starts by writing the command and flushing the output buffer. Next, the Do method receives all pending replies including the reply for the command just sent by Do. If any of the received replies is an error, then Do returns the error. If there are no errors, then Do returns the last reply. If the command argument to the Do method is "", then the Do method will flush the output buffer and receive pending replies without sending a command.

三、操作示例

package main
import (
    "github.com/garyburd/redigo/redis"
    "fmt"
)
var gCoon redis.Conn
func main(){
    var err error
    //连接redis
    gCoon, err = redis.Dial("tcp", ":6379")
    if err != nil{
        fmt.Println(err)  
        return
    }
    defer gCoon.Close()
    SetAndGet()
    Keys()
}
func Keys(){
    //使用Reply Helper转换类型
    rs, err := redis.Strings(gCoon.Do("keys", "*"))
    if err != nil{
        fmt.Println(err)  // [xxx]
        return
    }
    fmt.Println(rs)
}
func SetAndGet(){
    //设置键值
    rs, err := gCoon.Do("SET", "xxx", "hello")
    if err != nil{
        fmt.Println(err)
        return
    }
    fmt.Println(rs)  // OK
    //获取键值
    rs, err = gCoon.Do("GET", "xxx")
    if err != nil{
        fmt.Println(err)
        return
    }
    fmt.Println(rs)  //[104 101 108 108 111] 获取到的是[]byte类型
}
最后修改:2017 年 11 月 28 日
喜欢就给我点赞吧