From 4b10313f4020c3ba757a7585eead4a00b33fa0a8 Mon Sep 17 00:00:00 2001 From: guzeng Date: Wed, 30 Mar 2022 14:49:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9redis=E8=BF=9E=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conn.go | 3 +++ exists_test.go | 4 ++-- init.go | 12 +++++++----- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/conn.go b/conn.go index 60a93af..8a19461 100644 --- a/conn.go +++ b/conn.go @@ -6,6 +6,9 @@ import ( //取值 func GetConn() redisdb.Conn { + if Pool == nil { + Conn() + } return Pool.Get() } diff --git a/exists_test.go b/exists_test.go index 4141461..efc49a1 100644 --- a/exists_test.go +++ b/exists_test.go @@ -12,7 +12,7 @@ func Test_Exists(t *testing.T) { t.Log(ret3) t.Log(err) - ret3, err := HExists("testing23") - t.Log(ret3) + ret4, err := HExists("testing23", "cc") + t.Log(ret4) t.Log(err) } diff --git a/init.go b/init.go index 26ebbe1..2656abd 100644 --- a/init.go +++ b/init.go @@ -9,7 +9,7 @@ import ( // 定义redis链接池 var Pool *redisdb.Pool -var redisServerUrl string = "127.0.0.1:6379" +var RedisServerUrl string = "127.0.0.1:6379" // func init() { // if Pool == nil { @@ -18,7 +18,7 @@ var redisServerUrl string = "127.0.0.1:6379" // } func Conn(server_url ...string) { - var url string = "127.0.0.1:6379" + var url string = RedisServerUrl if len(server_url) > 0 { url = server_url[0] } @@ -37,7 +37,7 @@ func RedisInit(serverUrl string, max ...int) { } if serverUrl != "" { - redisServerUrl = serverUrl + RedisServerUrl = serverUrl } Pool = &redisdb.Pool{ @@ -56,10 +56,12 @@ 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", redisServerUrl, 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) + log.Println("redis connect failed", err) return nil, err + } else { + log.Println("redis connected", RedisServerUrl) } return c, nil }