redigo是一款redis官方推荐的go语言操作redis的client端工具。其封装了丰富的操作redis的api.支持事物,发布/订阅,连接池等功能。
github:https://github.com/gomodule/redigo
doc : https://godoc.org/github.com/gomodule/redigo/redis
download : go get -u github.com/gomodule/redigo
使用最简单的拨号的方式来实现redigo链接到redis服务器,返回一个链接对象redis.Conn和错误error
package main
import (
"github.com/gomodule/redigo/redis"
"log"
)
func main(){
conn,err:= redis.Dial("tcp","127.0.0.1:6379")
if err!=nil {
panic(err)
}
//执行
//Do方法 发送一条命令到redis服务器端执行并返回结果
_,err = conn.Do("GET","name")
if err!=nil {
log.Println(err)
return
}
//关闭链接
defer conn.Close()
}
通过连接池来管理conn,推荐使用此方式,支持各参数自定义
package main
import (
"github.com/gomodule/redigo/redis"
"log"
"fmt"
)
func main(){
//pool封装dail实现连接池
pool := &redis.Pool{
Dial: func() (redis.Conn, error) {
return redis.Dial("tcp","127.0.0.1:6379")
},
MaxActive:1,//redis在池中的最大链接数(conn个数)
}
//get方法获取conn链接对象
conn := pool.Get()
err := conn.Err()
if err !=nil{
panic(err)
}
//Do方法 发送一条命令到redis服务器端执行并返回结果
doReply,err := conn.Do("GET","name")
if err!=nil {
log.Println(err)
return
}
result,_:=redis.String(doReply,err)
fmt.Println("Do command : ",result)
//send发送命令到缓存,flush发送命令到服务端执行,Receive方法获取命令执行结果
conn.Send("SET","age",18)
conn.Flush()
sendReply,err:=conn.Receive()
send_result,_:=redis.String(sendReply,err)
fmt.Println("Send-Flush-Receive command : ",send_result)
//关闭链接
defer pool.Close()
}
package main
import (
"fmt"
"github.com/gomodule/redigo/redis"
"sync"
"time"
)
var wg sync.WaitGroup
//发布订阅
func main(){
const healthCheckPeriod = time.Minute
c, err := redis.Dial("tcp", "127.0.0.1:6379",
// Read timeout on server should be greater than ping period.
//单个命令执行的响应超时时间
redis.DialReadTimeout(healthCheckPeriod+10*time.Second),
//单个命令的写入超时时间
redis.DialWriteTimeout(10*time.Second))
if err != nil {
panic(err)
}
pubsubConn := redis.PubSubConn{Conn:c}
//订阅多个频道
if err := pubsubConn.Subscribe(redis.Args{}.AddFlat("ch1").Add("ch2","ch3")...); err != nil {
panic(err)
}
//链接测试
err = pubsubConn.Ping("OK")
if err!=nil {
panic(err)
}
wg.Add(1)
// Start a goroutine to receive notifications from the server.
go func() {
for{
fmt.Println("--------")
data := pubsubConn.Receive()//没有消息则阻塞在此
fmt.Println(data)
}
wg.Done()
}()
wg.Wait()
//关闭链接
defer pubsubConn.Close()
}
完整示例代码下载:https://github.com/ztind/redigo_sty