博客:Golang项目中读取配置文件)
package setting
import (
"fmt"
"github.com/gomodule/redigo/redis"
"log"
"time"
)
var (
RedisPool *redis.Pool
)
func InitRedisPool() {
RedisPool = &redis.Pool{
MaxIdle: 30,
MaxActive: 30,
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", "127.0.0.1:6379")
if err != nil {
c.Close()
fmt.Printf("fail to dial redis: %v\n", err)
return nil, err
}
if _, err = c.Do("AUTH", ""); err != nil {
c.Close()
fmt.Printf("fail to auth redis: %v\n", err)
return nil, err
}
if _, err = c.Do("SELECT", ""); err != nil {
c.Close()
fmt.Printf("fail to SELECT DB redis: %v\n", err)
return nil, err
}
return c, err
},
TestOnBorrow: func(c redis.Conn, t time.Time) error {
_, err := c.Do("PING")
if err != nil {
c.Close()
fmt.Printf("fail to ping redis: %v\n", err)
return err
}
return nil
},
}
log.Println("connect redis success")
}
func Get() redis.Conn {
return RedisPool.Get()
}