Browse Source

更改获取连接地址的方式

master v1.0.2
listen 2 years ago
parent
commit
dc483698df
3 changed files with 8 additions and 13 deletions
  1. +0
    -1
      go.mod
  2. +1
    -2
      go.sum
  3. +7
    -10
      init.go

+ 0
- 1
go.mod View File

@ -3,7 +3,6 @@ module git.tetele.net/tgo/redis
go 1.17
require (
git.tetele.net/tgo/conf v0.47.0
git.tetele.net/tgo/helper v0.3.1
github.com/gomodule/redigo v1.8.8
)

+ 1
- 2
go.sum View File

@ -1,5 +1,3 @@
git.tetele.net/tgo/conf v0.47.0 h1:2nGvXjyFV6gO7BPliG4lwERP/bgTOeSJpUv/I2ALGPg=
git.tetele.net/tgo/conf v0.47.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=
@ -11,6 +9,7 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

+ 7
- 10
init.go View File

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


Loading…
Cancel
Save