|
@ -4,12 +4,15 @@ import ( |
|
|
"log" |
|
|
"log" |
|
|
"time" |
|
|
"time" |
|
|
|
|
|
|
|
|
|
|
|
"git.tetele.net/tgo/conf" |
|
|
|
|
|
|
|
|
redisdb "github.com/gomodule/redigo/redis" |
|
|
redisdb "github.com/gomodule/redigo/redis" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
// 定义redis链接池
|
|
|
// 定义redis链接池
|
|
|
var Pool *redisdb.Pool |
|
|
var Pool *redisdb.Pool |
|
|
var RedisServerUrl string = "127.0.0.1:6379" |
|
|
|
|
|
|
|
|
var RedisServerUrl string = conf.REDIS_SERVER + ":" + conf.REDIS_PORT |
|
|
|
|
|
var RedisPassword string = "" |
|
|
|
|
|
|
|
|
// func init() {
|
|
|
// func init() {
|
|
|
// if Pool == nil {
|
|
|
// if Pool == nil {
|
|
@ -18,15 +21,18 @@ var RedisServerUrl string = "127.0.0.1:6379" |
|
|
// }
|
|
|
// }
|
|
|
|
|
|
|
|
|
func Conn(server_url ...string) { |
|
|
func Conn(server_url ...string) { |
|
|
var url string = RedisServerUrl |
|
|
|
|
|
|
|
|
var url, pwd string |
|
|
if len(server_url) > 0 { |
|
|
if len(server_url) > 0 { |
|
|
url = server_url[0] |
|
|
url = server_url[0] |
|
|
} |
|
|
} |
|
|
RedisInit(url) |
|
|
|
|
|
|
|
|
if len(server_url) > 1 { |
|
|
|
|
|
pwd = server_url[1] |
|
|
|
|
|
} |
|
|
|
|
|
RedisInit(url, pwd) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 初始化redis链接池
|
|
|
// 初始化redis链接池
|
|
|
func RedisInit(serverUrl string, max ...int) { |
|
|
|
|
|
|
|
|
func RedisInit(serverUrl, password string, max ...int) { |
|
|
|
|
|
|
|
|
var MaxActive, MaxIdle int |
|
|
var MaxActive, MaxIdle int |
|
|
if len(max) > 0 { |
|
|
if len(max) > 0 { |
|
@ -39,6 +45,9 @@ func RedisInit(serverUrl string, max ...int) { |
|
|
if serverUrl != "" { |
|
|
if serverUrl != "" { |
|
|
RedisServerUrl = serverUrl |
|
|
RedisServerUrl = serverUrl |
|
|
} |
|
|
} |
|
|
|
|
|
if password != "" { |
|
|
|
|
|
RedisPassword = password |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
Pool = &redisdb.Pool{ |
|
|
Pool = &redisdb.Pool{ |
|
|
MaxIdle: MaxIdle, /*最大的空闲连接数*/ |
|
|
MaxIdle: MaxIdle, /*最大的空闲连接数*/ |
|
@ -50,7 +59,7 @@ func RedisInit(serverUrl string, max ...int) { |
|
|
func redisConn() (redisdb.Conn, error) { |
|
|
func redisConn() (redisdb.Conn, error) { |
|
|
|
|
|
|
|
|
dbOption := redisdb.DialDatabase(0) |
|
|
dbOption := redisdb.DialDatabase(0) |
|
|
pwOption := redisdb.DialPassword("") |
|
|
|
|
|
|
|
|
pwOption := redisdb.DialPassword(RedisPassword) |
|
|
// **重要** 设置读写超时
|
|
|
// **重要** 设置读写超时
|
|
|
readTimeout := redisdb.DialReadTimeout(time.Second * time.Duration(2)) |
|
|
readTimeout := redisdb.DialReadTimeout(time.Second * time.Duration(2)) |
|
|
writeTimeout := redisdb.DialWriteTimeout(time.Second * time.Duration(5)) |
|
|
writeTimeout := redisdb.DialWriteTimeout(time.Second * time.Duration(5)) |
|
|