使用beego的链接redis
package models
import (
"github.com/garyburd/redigo/redis"
"github.com/astaxie/beego"
"time"
"fmt"
)
var(
pool *redis.Pool
redisHost = beego.AppConfig.String("reids_host")
redisPass = beego.AppConfig.String("redis_passwd")
)
func newRedisPool()*redis.Pool{
return &redis.Pool{
MaxIdle:50,
MaxActive:30,
IdleTimeout:300*time.Second,
Dial: func() (redis.Conn, error) {
c,err := redis.Dial("tcp",redisHost,redis.DialPassword(redisPass))
if err != nil {
fmt.Println(err)
return nil,err
}
return c,nil
},
TestOnBorrow: func(conn redis.Conn, t time.Time) error {
if time.Since(t)<time.Minute{
return nil
}
_,err := conn.Do("PING")
return err
},
}
}
func init(){
pool = newRedisPool()
}
func RedisPool()*redis.Pool{
return pool
}
操作具体数据
rc := models.RedisPool().Get()
defer rc.Close()
beego.Info(redis.PoolStats{})
rc.Do("select",2)
_, err :=rc.Do("set","wohao",234234)
_, err = c.Do("SET", "password", "123456", "EX", "10")
if err != nil {
beego.Info(err.Error())
}