Browse Source

修改redis默认地址

master v1.5.0
guzeng 2 years ago
parent
commit
237b07c53a
3 changed files with 17 additions and 5 deletions
  1. +1
    -0
      go.mod
  2. +2
    -0
      go.sum
  3. +14
    -5
      init.go

+ 1
- 0
go.mod View File

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

+ 2
- 0
go.sum View File

@ -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 h1:5+6xK6nr2BB1A0XVKrrA8R/GW0y3aUuKUIcz3zgAwZo=
git.tetele.net/tgo/helper v0.3.1/go.mod h1:89mQwyfqZ+t8YXiVwzSxA70gLlUNqoZGDEUxvV46jXk= git.tetele.net/tgo/helper v0.3.1/go.mod h1:89mQwyfqZ+t8YXiVwzSxA70gLlUNqoZGDEUxvV46jXk=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=


+ 14
- 5
init.go View File

@ -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))


Loading…
Cancel
Save