package redis
import (
"fmt"
"github.com/garyburd/redigo/redis"
"time"
)
var(
pool *redis.Pool
redisHost="127.0.0.1:6379"
//redisPass=""
)
//newRedisPool:创建redis连接池
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)
if err != nil {
fmt.Println(err)
return nil,err
}
//2、访问认证
//if _, err =c.Do("AUTH",redisPass);err!=nil{
// c.Close()
// return nil,err
//}
return c,nil
},
//定时检查redis是否出状况
TestOnBorrow: func(conn redis.Conn, t time.Time) error {
if time.Since(t)<time.Minute{
return nil
}
_,err := conn.Do("PING")
return err
},
}
}
//初始化redis连接池
func init(){
pool = newRedisPool()
}
//对外暴露连接池
func RedisPool()*redis.Pool{
return pool
}
Go连接redis
最新推荐文章于 2024-05-07 17:38:16 发布