Browse Source

修改redis连接

master v1.2.0
guzeng 2 years ago
parent
commit
4b10313f40
3 changed files with 12 additions and 7 deletions
  1. +3
    -0
      conn.go
  2. +2
    -2
      exists_test.go
  3. +7
    -5
      init.go

+ 3
- 0
conn.go View File

@ -6,6 +6,9 @@ import (
//取值 //取值
func GetConn() redisdb.Conn { func GetConn() redisdb.Conn {
if Pool == nil {
Conn()
}
return Pool.Get() return Pool.Get()
} }


+ 2
- 2
exists_test.go View File

@ -12,7 +12,7 @@ func Test_Exists(t *testing.T) {
t.Log(ret3) t.Log(ret3)
t.Log(err) t.Log(err)
ret3, err := HExists("testing23")
t.Log(ret3)
ret4, err := HExists("testing23", "cc")
t.Log(ret4)
t.Log(err) t.Log(err)
} }

+ 7
- 5
init.go View File

@ -9,7 +9,7 @@ import (
// 定义redis链接池 // 定义redis链接池
var Pool *redisdb.Pool var Pool *redisdb.Pool
var redisServerUrl string = "127.0.0.1:6379"
var RedisServerUrl string = "127.0.0.1:6379"
// func init() { // func init() {
// if Pool == nil { // if Pool == nil {
@ -18,7 +18,7 @@ var redisServerUrl string = "127.0.0.1:6379"
// } // }
func Conn(server_url ...string) { func Conn(server_url ...string) {
var url string = "127.0.0.1:6379"
var url string = RedisServerUrl
if len(server_url) > 0 { if len(server_url) > 0 {
url = server_url[0] url = server_url[0]
} }
@ -37,7 +37,7 @@ func RedisInit(serverUrl string, max ...int) {
} }
if serverUrl != "" { if serverUrl != "" {
redisServerUrl = serverUrl
RedisServerUrl = serverUrl
} }
Pool = &redisdb.Pool{ Pool = &redisdb.Pool{
@ -56,10 +56,12 @@ 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", redisServerUrl, 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 connect failed", err)
return nil, err return nil, err
} else {
log.Println("redis connected", RedisServerUrl)
} }
return c, nil return c, nil
} }

Loading…
Cancel
Save