|
|
@ -9,26 +9,33 @@ import ( |
|
|
|
|
|
|
|
// 定义redis链接池
|
|
|
|
var Pool *redisdb.Pool |
|
|
|
|
|
|
|
// redis服务地址,如需更改,在api服务中设置
|
|
|
|
var RedisServerUrl string = "127.0.0.1:6379" |
|
|
|
|
|
|
|
// redis密码,如需更改,在api服务中设置
|
|
|
|
var RedisPassword string = "" |
|
|
|
|
|
|
|
var MaxActive, MaxIdle int = 1, 1 //最大的激活连接数,最大的空闲连接数
|
|
|
|
|
|
|
|
// func init() {
|
|
|
|
// if Pool == nil {
|
|
|
|
// RedisInit()
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
func Conn(server_url ...string) { |
|
|
|
var url string = RedisServerUrl |
|
|
|
if len(server_url) > 0 { |
|
|
|
url = server_url[0] |
|
|
|
} |
|
|
|
RedisInit(url) |
|
|
|
func Conn() { |
|
|
|
RedisInit(RedisServerUrl, RedisPassword, MaxActive, MaxIdle) |
|
|
|
} |
|
|
|
|
|
|
|
// 初始化redis链接池
|
|
|
|
func RedisInit(serverUrl string, max ...int) { |
|
|
|
/* |
|
|
|
* 初始化redis链接池 |
|
|
|
* @param serverUrl 服务地址 |
|
|
|
* @param password 密码 |
|
|
|
* @param max 最大的激活连接数,最大的空闲连接数 |
|
|
|
*/ |
|
|
|
func RedisInit(serverUrl, password string, max ...int) { |
|
|
|
|
|
|
|
var MaxActive, MaxIdle int |
|
|
|
if len(max) > 0 { |
|
|
|
MaxActive = max[0] |
|
|
|
} |
|
|
@ -39,6 +46,9 @@ func RedisInit(serverUrl string, max ...int) { |
|
|
|
if serverUrl != "" { |
|
|
|
RedisServerUrl = serverUrl |
|
|
|
} |
|
|
|
if password != "" { |
|
|
|
RedisPassword = password |
|
|
|
} |
|
|
|
|
|
|
|
Pool = &redisdb.Pool{ |
|
|
|
MaxIdle: MaxIdle, /*最大的空闲连接数*/ |
|
|
@ -50,7 +60,7 @@ func RedisInit(serverUrl string, max ...int) { |
|
|
|
func redisConn() (redisdb.Conn, error) { |
|
|
|
|
|
|
|
dbOption := redisdb.DialDatabase(0) |
|
|
|
pwOption := redisdb.DialPassword("") |
|
|
|
pwOption := redisdb.DialPassword(RedisPassword) |
|
|
|
// **重要** 设置读写超时
|
|
|
|
readTimeout := redisdb.DialReadTimeout(time.Second * time.Duration(2)) |
|
|
|
writeTimeout := redisdb.DialWriteTimeout(time.Second * time.Duration(5)) |
|
|
|