From dc80cfbc24f88ccb370c77395261fba7f38c14b8 Mon Sep 17 00:00:00 2001 From: 王家文 Date: Mon, 22 Apr 2024 17:29:51 +0800 Subject: [PATCH] feat✨:redis封装方法优化 --- configs/confbase/external.go | 8 ++++---- service-common/svredis/index.go | 4 ++-- util/zredis/external.go | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ util/zredis/handler.go | 30 ------------------------------ util/zredis/hash.go | 26 ++++++++++++++++++++++---- util/zredis/hash_data.go | 18 +++++++++++++++--- util/zredis/internal.go | 31 +++++++++++++++++++++++++++++++ util/zredis/kv.go | 46 ++++++++++++++++++++++++++++++++++++++-------- util/zredis/redis.go | 50 -------------------------------------------------- 9 files changed, 176 insertions(+), 101 deletions(-) create mode 100644 util/zredis/external.go delete mode 100644 util/zredis/handler.go create mode 100644 util/zredis/internal.go delete mode 100644 util/zredis/redis.go diff --git a/configs/confbase/external.go b/configs/confbase/external.go index 6bef177..54123ac 100644 --- a/configs/confbase/external.go +++ b/configs/confbase/external.go @@ -10,13 +10,13 @@ import ( func SaveCache[T IConfData](gameId string, obj T) { info := obj.ConfInfo(gameId) - _ = zredis.SetEx(zredis.GetConn(), info.CacheKey, zjson.Str(obj), info.CacheTime) + _ = zredis.SetEx(info.CacheKey, zjson.Str(obj), info.CacheTime) } func LoadCache[T IConfData](gameId string, obj T) (has bool) { has = true info := obj.ConfInfo(gameId) - text, err := zredis.Get(zredis.GetConn(), info.CacheKey) + text, err := zredis.Get(info.CacheKey) if err != nil { has = false return @@ -77,7 +77,7 @@ func GetCurrent[T1 IConfData, T2 IConfRawData](gameId string, obj T1) bool { has := false info := obj.ConfInfo(gameId) currentKey := info.CacheCurrent - currentId := zredis.GetString(zredis.GetConn(), currentKey) + currentId := zredis.GetString(currentKey) confRaw := new(T2) hasFind := false if currentId == "" { @@ -97,7 +97,7 @@ func GetCurrent[T1 IConfData, T2 IConfRawData](gameId string, obj T1) bool { SaveCache(gameId, obj) has = true currentId = obj.GetUid() - _ = zredis.Set(zredis.GetConn(), currentKey, currentId) + _ = zredis.Set(currentKey, currentId) } return has } diff --git a/service-common/svredis/index.go b/service-common/svredis/index.go index 0b9294d..01a0697 100644 --- a/service-common/svredis/index.go +++ b/service-common/svredis/index.go @@ -56,13 +56,13 @@ func Init() bool { func SaveData(gameId string, obj IRedisData) { info := obj.RedisInfo(gameId) - _ = zredis.SetEx(zredis.GetConn(), info.CacheKey, zjson.Str(obj), info.CacheTime) + _ = zredis.SetEx(info.CacheKey, zjson.Str(obj), info.CacheTime) } func LoadData[T IRedisData](gameId string, obj T) (has bool) { has = true info := obj.RedisInfo(gameId) - text, err := zredis.Get(zredis.GetConn(), info.CacheKey) + text, err := zredis.Get(info.CacheKey) if err != nil { has = false return diff --git a/util/zredis/external.go b/util/zredis/external.go new file mode 100644 index 0000000..ae2a426 --- /dev/null +++ b/util/zredis/external.go @@ -0,0 +1,64 @@ +package zredis + +import ( + "errors" + "fmt" + "github.com/gomodule/redigo/redis" + "time" +) + +// Init 初始化 +func Init(addr, pass string, db int) { + + _addr, _pass, _db = addr, pass, db + + pool = &redis.Pool{ + MaxActive: 10240, + MaxIdle: 10240, // Maximum number of idle connections in the pool. + IdleTimeout: 240 * time.Second, // Close connections after remaining idle for this duration + Dial: dial, + Wait: true, + } +} + +// GetConn 获取链接 +func GetConn() (conn redis.Conn, err error) { + if pool == nil { + err = errors.New("zredis pool nil") + fmt.Println("err", err) + return nil, err + } + conn = pool.Get() + if conn == nil { + err = errors.New("zredis conn nil") + fmt.Println("err", err) + return nil, err + } + return conn, nil +} + +// Check 检查 +func Check() bool { + + conn, err := GetConn() + if err != nil { + return false + } + defer autoClose(conn) + + _, err = redis.Int64(conn.Do("GET", "test")) + if err != nil { + if err == redis.ErrNil { + //fmt.Println("redis.ErrNil") + } else { + fmt.Println("err", err) + fmt.Println("redis conn error", + "_addr", _addr, + "_pass", _pass, + "_db", _db) + return false + } + } + + return true +} diff --git a/util/zredis/handler.go b/util/zredis/handler.go deleted file mode 100644 index 47267bc..0000000 --- a/util/zredis/handler.go +++ /dev/null @@ -1,30 +0,0 @@ -package zredis - -import ( - "fmt" - "github.com/gomodule/redigo/redis" -) - -func Check() bool { - if pool == nil { - fmt.Println("redis pool nil") - } - conn := pool.Get() - defer AutoClose(conn) - - _, err := redis.Int64(conn.Do("GET", "test")) - if err != nil { - if err == redis.ErrNil { - //fmt.Println("redis.ErrNil") - } else { - fmt.Println("err", err) - fmt.Println("redis conn error", - "_addr", _addr, - "_pass", _pass, - "_db", _db) - return false - } - } - - return true -} diff --git a/util/zredis/hash.go b/util/zredis/hash.go index 1a0df02..f7724ff 100644 --- a/util/zredis/hash.go +++ b/util/zredis/hash.go @@ -5,13 +5,25 @@ import ( ) // HSet HSet -func HSet(conn redis.Conn, tableName, key, value any) error { - _, err := conn.Do("HSET", tableName, key, value) +func HSet(tableName, key, value any) error { + conn, err := GetConn() + if err != nil { + return err + } + defer autoClose(conn) + + _, err = conn.Do("HSET", tableName, key, value) return err } // HGet HGet -func HGet(conn redis.Conn, tableName, key any) any { +func HGet(tableName, key any) any { + conn, err := GetConn() + if err != nil { + return nil + } + defer autoClose(conn) + reply, err := conn.Do("HGET", tableName, key) if err != nil { return nil @@ -20,7 +32,13 @@ func HGet(conn redis.Conn, tableName, key any) any { } // HGetInt64 HGetInt64 -func HGetInt64(conn redis.Conn, tableName, key any) int64 { +func HGetInt64(tableName, key any) int64 { + conn, err := GetConn() + if err != nil { + return 0 + } + defer autoClose(conn) + reply, err := redis.Int64(conn.Do("HGET", tableName, key)) if err == nil { return reply diff --git a/util/zredis/hash_data.go b/util/zredis/hash_data.go index 45c292c..379564c 100644 --- a/util/zredis/hash_data.go +++ b/util/zredis/hash_data.go @@ -6,13 +6,25 @@ import ( ) // HSetData HSetData -func HSetData(conn redis.Conn, value IData) error { - _, err := conn.Do("HSET", value.TableName(), value.DbKey(), value.ToString()) +func HSetData(value IData) error { + conn, err := GetConn() + if err != nil { + return err + } + defer autoClose(conn) + + _, err = conn.Do("HSET", value.TableName(), value.DbKey(), value.ToString()) return err } // HGetData HSetData -func HGetData(conn redis.Conn, value IData) IData { +func HGetData(value IData) IData { + conn, err := GetConn() + if err != nil { + return nil + } + defer autoClose(conn) + text, err := redis.String(conn.Do("HGET", value.TableName(), value.DbKey())) if err == nil { errJson := zjson.Obj(text, value) diff --git a/util/zredis/internal.go b/util/zredis/internal.go new file mode 100644 index 0000000..8d22b46 --- /dev/null +++ b/util/zredis/internal.go @@ -0,0 +1,31 @@ +package zredis + +import ( + "github.com/gomodule/redigo/redis" + "time" +) + +var ( + _addr string + _pass string + _db int +) +var pool *redis.Pool + +// autoClose 自动关闭 +func autoClose(conn redis.Conn) { + err := conn.Close() + if err != nil { + } +} + +func dial() (redis.Conn, error) { + conn, err := redis.Dial("tcp", _addr, + redis.DialPassword(_pass), + redis.DialDatabase(_db), + redis.DialConnectTimeout(5*time.Second), + redis.DialReadTimeout(5*time.Second), + redis.DialWriteTimeout(5*time.Second), + ) + return conn, err +} diff --git a/util/zredis/kv.go b/util/zredis/kv.go index 78e2cee..b8da90c 100644 --- a/util/zredis/kv.go +++ b/util/zredis/kv.go @@ -5,11 +5,11 @@ import ( ) func Increment(key string) int64 { - conn := GetConn() - if conn == nil { + conn, err := GetConn() + if err != nil { return 0 } - defer conn.Close() + defer autoClose(conn) value, err := redis.Int64(conn.Do("INCR", key)) if err == nil { @@ -18,22 +18,46 @@ func Increment(key string) int64 { return 0 } -func Set(conn redis.Conn, key, value any) (err error) { +func Set(key, value any) (err error) { + conn, errConn := GetConn() + if errConn != nil { + return errConn + } + defer autoClose(conn) + _, err = conn.Do("Set", key, value) return } -func Get(conn redis.Conn, key string) (value string, err error) { +func Get(key string) (value string, err error) { + conn, errConn := GetConn() + if errConn != nil { + return "", errConn + } + defer autoClose(conn) + value, err = redis.String(conn.Do("Get", key)) return } -func SetEx(conn redis.Conn, key, value string, exTime int) (err error) { +func SetEx(key, value string, exTime int) (err error) { + conn, errConn := GetConn() + if errConn != nil { + return errConn + } + defer autoClose(conn) + _, err = conn.Do("Set", key, value, "EX", exTime) return } -func GetString(conn redis.Conn, key string) string { +func GetString(key string) string { + conn, errConn := GetConn() + if errConn != nil { + return "" + } + defer autoClose(conn) + value, err := redis.String(conn.Do("Get", key)) if err == nil { return value @@ -41,7 +65,13 @@ func GetString(conn redis.Conn, key string) string { return "" } -func GetInt64(conn redis.Conn, key string) int64 { +func GetInt64(key string) int64 { + conn, errConn := GetConn() + if errConn != nil { + return 0 + } + defer autoClose(conn) + value, err := redis.Int64(conn.Do("Get", key)) if err == nil { return value diff --git a/util/zredis/redis.go b/util/zredis/redis.go deleted file mode 100644 index df5d214..0000000 --- a/util/zredis/redis.go +++ /dev/null @@ -1,50 +0,0 @@ -package zredis - -import ( - "github.com/gomodule/redigo/redis" - "time" -) - -var ( - _addr string - _pass string - _db int -) -var pool *redis.Pool - -func Init(addr, pass string, db int) { - - _addr, _pass, _db = addr, pass, db - - pool = &redis.Pool{ - MaxActive: 10240, - MaxIdle: 10240, // Maximum number of idle connections in the pool. - IdleTimeout: 240 * time.Second, // Close connections after remaining idle for this duration - Dial: dial, - Wait: true, - } -} - -func dial() (redis.Conn, error) { - conn, err := redis.Dial("tcp", _addr, - redis.DialPassword(_pass), - redis.DialDatabase(_db), - redis.DialConnectTimeout(5*time.Second), - redis.DialReadTimeout(5*time.Second), - redis.DialWriteTimeout(5*time.Second), - ) - return conn, err -} - -func GetConn() redis.Conn { - if pool == nil { - return nil - } - return pool.Get() -} - -func AutoClose(conn redis.Conn) { - err := conn.Close() - if err != nil { - } -} -- libgit2 0.21.0