|
@ -3,14 +3,12 @@ package redis |
|
|
import ( |
|
|
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" |
|
|
// func init() {
|
|
|
// func init() {
|
|
|
// if Pool == nil {
|
|
|
// if Pool == nil {
|
|
|
// RedisInit()
|
|
|
// RedisInit()
|
|
@ -18,7 +16,7 @@ var Pool *redisdb.Pool |
|
|
// }
|
|
|
// }
|
|
|
|
|
|
|
|
|
// 初始化redis链接池
|
|
|
// 初始化redis链接池
|
|
|
func RedisInit(max ...int) { |
|
|
|
|
|
|
|
|
func RedisInit(serverUrl string,max ...int) { |
|
|
|
|
|
|
|
|
var MaxActive, MaxIdle int |
|
|
var MaxActive, MaxIdle int |
|
|
if len(max) > 0 { |
|
|
if len(max) > 0 { |
|
@ -28,6 +26,10 @@ func RedisInit(max ...int) { |
|
|
MaxIdle = max[1] |
|
|
MaxIdle = max[1] |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if serverUrl != "" { |
|
|
|
|
|
redisServerUrl = serverUrl |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
Pool = &redisdb.Pool{ |
|
|
Pool = &redisdb.Pool{ |
|
|
MaxIdle: MaxIdle, /*最大的空闲连接数*/ |
|
|
MaxIdle: MaxIdle, /*最大的空闲连接数*/ |
|
|
MaxActive: MaxActive, /*最大的激活连接数*/ |
|
|
MaxActive: MaxActive, /*最大的激活连接数*/ |
|
@ -37,11 +39,6 @@ func RedisInit(max ...int) { |
|
|
|
|
|
|
|
|
func redisConn() (redisdb.Conn, error) { |
|
|
func redisConn() (redisdb.Conn, error) { |
|
|
|
|
|
|
|
|
var url string = conf.REDIS_SERVER |
|
|
|
|
|
if url == "" { |
|
|
|
|
|
url = "127.0.0.1:6379" |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
dbOption := redisdb.DialDatabase(0) |
|
|
dbOption := redisdb.DialDatabase(0) |
|
|
pwOption := redisdb.DialPassword("") |
|
|
pwOption := redisdb.DialPassword("") |
|
|
// **重要** 设置读写超时
|
|
|
// **重要** 设置读写超时
|
|
@ -49,7 +46,7 @@ func redisConn() (redisdb.Conn, error) { |
|
|
writeTimeout := redisdb.DialWriteTimeout(time.Second * time.Duration(5)) |
|
|
writeTimeout := redisdb.DialWriteTimeout(time.Second * time.Duration(5)) |
|
|
conTimeout := redisdb.DialConnectTimeout(time.Second * time.Duration(2)) |
|
|
conTimeout := redisdb.DialConnectTimeout(time.Second * time.Duration(2)) |
|
|
|
|
|
|
|
|
c, err := redisdb.Dial("tcp", url, dbOption, pwOption, readTimeout, writeTimeout, conTimeout) |
|
|
|
|
|
|
|
|
c, err := redisdb.Dial("tcp", redisServerUrl, dbOption, pwOption, readTimeout, writeTimeout, conTimeout) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
log.Println("redis server connect failed", err) |
|
|
log.Println("redis server connect failed", err) |
|
|
return nil, err |
|
|
return nil, err |
|
|