From 237b07c53a1857d1fc18bfdd1cb75cf097b2060d Mon Sep 17 00:00:00 2001 From: guzeng Date: Thu, 21 Jul 2022 13:39:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9redis=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 1 + go.sum | 2 ++ init.go | 19 ++++++++++++++----- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index 240257e..2be6438 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module git.tetele.net/tgo/redis go 1.17 require ( + git.tetele.net/tgo/conf v0.48.0 git.tetele.net/tgo/helper v0.3.1 github.com/gomodule/redigo v1.8.8 ) diff --git a/go.sum b/go.sum index 45cb84b..b107184 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +git.tetele.net/tgo/conf v0.48.0 h1:goHmxuzkSnoPFJbTVIF7IL13SAINQWWilEQVrRJR9Wg= +git.tetele.net/tgo/conf v0.48.0/go.mod h1:AWVIBEDE5dtotthUgR0SWaR2Qa6/f+O5WQ3s7Tj8q7A= git.tetele.net/tgo/helper v0.3.1 h1:5+6xK6nr2BB1A0XVKrrA8R/GW0y3aUuKUIcz3zgAwZo= git.tetele.net/tgo/helper v0.3.1/go.mod h1:89mQwyfqZ+t8YXiVwzSxA70gLlUNqoZGDEUxvV46jXk= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= diff --git a/init.go b/init.go index 2656abd..11afb5b 100644 --- a/init.go +++ b/init.go @@ -4,12 +4,15 @@ import ( "log" "time" + "git.tetele.net/tgo/conf" + redisdb "github.com/gomodule/redigo/redis" ) // 定义redis链接池 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() { // if Pool == nil { @@ -18,15 +21,18 @@ var RedisServerUrl string = "127.0.0.1:6379" // } func Conn(server_url ...string) { - var url string = RedisServerUrl + var url, pwd string if len(server_url) > 0 { url = server_url[0] } - RedisInit(url) + if len(server_url) > 1 { + pwd = server_url[1] + } + RedisInit(url, pwd) } // 初始化redis链接池 -func RedisInit(serverUrl string, max ...int) { +func RedisInit(serverUrl, password string, max ...int) { var MaxActive, MaxIdle int if len(max) > 0 { @@ -39,6 +45,9 @@ func RedisInit(serverUrl string, max ...int) { if serverUrl != "" { RedisServerUrl = serverUrl } + if password != "" { + RedisPassword = password + } Pool = &redisdb.Pool{ MaxIdle: MaxIdle, /*最大的空闲连接数*/ @@ -50,7 +59,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))