安装
go get github.com/gomodule/redigo/redis
简单例子:
package main
import (
"github.com/gomodule/redigo/redis"
"fmt"
)
func main() {
c, err := redis.Dial("tcp", "127.0.0.1:6379")
if err != nil {
fmt.Println(err)
return
}
defer c.Close()
c.Do("set", "c1", "hello world!")
}
字节流编解码
redis只能转化基本类型,自定义类型无法解析,这里可以存入字节流,取出后解码即可
存
var types modle.types
var buffer bytes.Buffer
// 获取编码器
enc := gob.NewBuffer(&buffer)
// 编码,存入buffer即可
enc.Encode(&types)
_, err := conn.Do("set", "types", buffer.Bytes())
取
rel, err := redis.valuse(redis.Do("get", "types"))
if err != nil{
beego.Info("获取redis数据错误!")
return
}
var types modle.types
dec := gob.NewDecode(bytes.NewReader(rel))
dec.Decode(&types)
网友评论