goweb/initialization/redis.go

36 lines
896 B
Go

package initialization
import (
"fmt"
"github.com/garyburd/redigo/redis"
"go_api_tmpl/config"
"go_api_tmpl/global"
"time"
)
// InitRedisConn 初始化Redis连接对象
func InitRedisConn() {
// 读取配置
config.InitRedisConfig()
// 初始化连接
conn, err := redis.Dial("tcp",
// Redis连接信息
fmt.Sprintf("%s:%s", config.RedisConfig.Host, config.RedisConfig.Port),
// 密码
redis.DialPassword(config.RedisConfig.Password),
// 默认使用数据库
redis.DialDatabase(config.RedisConfig.Db),
redis.DialKeepAlive(1*time.Second),
redis.DialConnectTimeout(5*time.Second),
redis.DialReadTimeout(1*time.Second),
redis.DialWriteTimeout(1*time.Second))
if err != nil {
global.Log.Panicf("Redis初始化连接失败: %v", err.Error())
} else {
global.Log.Info("Redis连接初始化成功")
global.RedisConn = global.RedisClient{
Client: conn,
}
}
}