Commit dc80cfbc24f88ccb370c77395261fba7f38c14b8
1 parent
4611a69d
Exists in
master
and in
1 other branch
feat✨:redis封装方法优化
Showing
9 changed files
with
176 additions
and
101 deletions
Show diff stats
configs/confbase/external.go
| ... | ... | @@ -10,13 +10,13 @@ import ( |
| 10 | 10 | |
| 11 | 11 | func SaveCache[T IConfData](gameId string, obj T) { |
| 12 | 12 | info := obj.ConfInfo(gameId) |
| 13 | - _ = zredis.SetEx(zredis.GetConn(), info.CacheKey, zjson.Str(obj), info.CacheTime) | |
| 13 | + _ = zredis.SetEx(info.CacheKey, zjson.Str(obj), info.CacheTime) | |
| 14 | 14 | } |
| 15 | 15 | |
| 16 | 16 | func LoadCache[T IConfData](gameId string, obj T) (has bool) { |
| 17 | 17 | has = true |
| 18 | 18 | info := obj.ConfInfo(gameId) |
| 19 | - text, err := zredis.Get(zredis.GetConn(), info.CacheKey) | |
| 19 | + text, err := zredis.Get(info.CacheKey) | |
| 20 | 20 | if err != nil { |
| 21 | 21 | has = false |
| 22 | 22 | return |
| ... | ... | @@ -77,7 +77,7 @@ func GetCurrent[T1 IConfData, T2 IConfRawData](gameId string, obj T1) bool { |
| 77 | 77 | has := false |
| 78 | 78 | info := obj.ConfInfo(gameId) |
| 79 | 79 | currentKey := info.CacheCurrent |
| 80 | - currentId := zredis.GetString(zredis.GetConn(), currentKey) | |
| 80 | + currentId := zredis.GetString(currentKey) | |
| 81 | 81 | confRaw := new(T2) |
| 82 | 82 | hasFind := false |
| 83 | 83 | if currentId == "" { |
| ... | ... | @@ -97,7 +97,7 @@ func GetCurrent[T1 IConfData, T2 IConfRawData](gameId string, obj T1) bool { |
| 97 | 97 | SaveCache(gameId, obj) |
| 98 | 98 | has = true |
| 99 | 99 | currentId = obj.GetUid() |
| 100 | - _ = zredis.Set(zredis.GetConn(), currentKey, currentId) | |
| 100 | + _ = zredis.Set(currentKey, currentId) | |
| 101 | 101 | } |
| 102 | 102 | return has |
| 103 | 103 | } | ... | ... |
service-common/svredis/index.go
| ... | ... | @@ -56,13 +56,13 @@ func Init() bool { |
| 56 | 56 | |
| 57 | 57 | func SaveData(gameId string, obj IRedisData) { |
| 58 | 58 | info := obj.RedisInfo(gameId) |
| 59 | - _ = zredis.SetEx(zredis.GetConn(), info.CacheKey, zjson.Str(obj), info.CacheTime) | |
| 59 | + _ = zredis.SetEx(info.CacheKey, zjson.Str(obj), info.CacheTime) | |
| 60 | 60 | } |
| 61 | 61 | |
| 62 | 62 | func LoadData[T IRedisData](gameId string, obj T) (has bool) { |
| 63 | 63 | has = true |
| 64 | 64 | info := obj.RedisInfo(gameId) |
| 65 | - text, err := zredis.Get(zredis.GetConn(), info.CacheKey) | |
| 65 | + text, err := zredis.Get(info.CacheKey) | |
| 66 | 66 | if err != nil { |
| 67 | 67 | has = false |
| 68 | 68 | return | ... | ... |
| ... | ... | @@ -0,0 +1,64 @@ |
| 1 | +package zredis | |
| 2 | + | |
| 3 | +import ( | |
| 4 | + "errors" | |
| 5 | + "fmt" | |
| 6 | + "github.com/gomodule/redigo/redis" | |
| 7 | + "time" | |
| 8 | +) | |
| 9 | + | |
| 10 | +// Init 初始化 | |
| 11 | +func Init(addr, pass string, db int) { | |
| 12 | + | |
| 13 | + _addr, _pass, _db = addr, pass, db | |
| 14 | + | |
| 15 | + pool = &redis.Pool{ | |
| 16 | + MaxActive: 10240, | |
| 17 | + MaxIdle: 10240, // Maximum number of idle connections in the pool. | |
| 18 | + IdleTimeout: 240 * time.Second, // Close connections after remaining idle for this duration | |
| 19 | + Dial: dial, | |
| 20 | + Wait: true, | |
| 21 | + } | |
| 22 | +} | |
| 23 | + | |
| 24 | +// GetConn 获取链接 | |
| 25 | +func GetConn() (conn redis.Conn, err error) { | |
| 26 | + if pool == nil { | |
| 27 | + err = errors.New("zredis pool nil") | |
| 28 | + fmt.Println("err", err) | |
| 29 | + return nil, err | |
| 30 | + } | |
| 31 | + conn = pool.Get() | |
| 32 | + if conn == nil { | |
| 33 | + err = errors.New("zredis conn nil") | |
| 34 | + fmt.Println("err", err) | |
| 35 | + return nil, err | |
| 36 | + } | |
| 37 | + return conn, nil | |
| 38 | +} | |
| 39 | + | |
| 40 | +// Check 检查 | |
| 41 | +func Check() bool { | |
| 42 | + | |
| 43 | + conn, err := GetConn() | |
| 44 | + if err != nil { | |
| 45 | + return false | |
| 46 | + } | |
| 47 | + defer autoClose(conn) | |
| 48 | + | |
| 49 | + _, err = redis.Int64(conn.Do("GET", "test")) | |
| 50 | + if err != nil { | |
| 51 | + if err == redis.ErrNil { | |
| 52 | + //fmt.Println("redis.ErrNil") | |
| 53 | + } else { | |
| 54 | + fmt.Println("err", err) | |
| 55 | + fmt.Println("redis conn error", | |
| 56 | + "_addr", _addr, | |
| 57 | + "_pass", _pass, | |
| 58 | + "_db", _db) | |
| 59 | + return false | |
| 60 | + } | |
| 61 | + } | |
| 62 | + | |
| 63 | + return true | |
| 64 | +} | ... | ... |
util/zredis/handler.go
| ... | ... | @@ -1,30 +0,0 @@ |
| 1 | -package zredis | |
| 2 | - | |
| 3 | -import ( | |
| 4 | - "fmt" | |
| 5 | - "github.com/gomodule/redigo/redis" | |
| 6 | -) | |
| 7 | - | |
| 8 | -func Check() bool { | |
| 9 | - if pool == nil { | |
| 10 | - fmt.Println("redis pool nil") | |
| 11 | - } | |
| 12 | - conn := pool.Get() | |
| 13 | - defer AutoClose(conn) | |
| 14 | - | |
| 15 | - _, err := redis.Int64(conn.Do("GET", "test")) | |
| 16 | - if err != nil { | |
| 17 | - if err == redis.ErrNil { | |
| 18 | - //fmt.Println("redis.ErrNil") | |
| 19 | - } else { | |
| 20 | - fmt.Println("err", err) | |
| 21 | - fmt.Println("redis conn error", | |
| 22 | - "_addr", _addr, | |
| 23 | - "_pass", _pass, | |
| 24 | - "_db", _db) | |
| 25 | - return false | |
| 26 | - } | |
| 27 | - } | |
| 28 | - | |
| 29 | - return true | |
| 30 | -} |
util/zredis/hash.go
| ... | ... | @@ -5,13 +5,25 @@ import ( |
| 5 | 5 | ) |
| 6 | 6 | |
| 7 | 7 | // HSet HSet |
| 8 | -func HSet(conn redis.Conn, tableName, key, value any) error { | |
| 9 | - _, err := conn.Do("HSET", tableName, key, value) | |
| 8 | +func HSet(tableName, key, value any) error { | |
| 9 | + conn, err := GetConn() | |
| 10 | + if err != nil { | |
| 11 | + return err | |
| 12 | + } | |
| 13 | + defer autoClose(conn) | |
| 14 | + | |
| 15 | + _, err = conn.Do("HSET", tableName, key, value) | |
| 10 | 16 | return err |
| 11 | 17 | } |
| 12 | 18 | |
| 13 | 19 | // HGet HGet |
| 14 | -func HGet(conn redis.Conn, tableName, key any) any { | |
| 20 | +func HGet(tableName, key any) any { | |
| 21 | + conn, err := GetConn() | |
| 22 | + if err != nil { | |
| 23 | + return nil | |
| 24 | + } | |
| 25 | + defer autoClose(conn) | |
| 26 | + | |
| 15 | 27 | reply, err := conn.Do("HGET", tableName, key) |
| 16 | 28 | if err != nil { |
| 17 | 29 | return nil |
| ... | ... | @@ -20,7 +32,13 @@ func HGet(conn redis.Conn, tableName, key any) any { |
| 20 | 32 | } |
| 21 | 33 | |
| 22 | 34 | // HGetInt64 HGetInt64 |
| 23 | -func HGetInt64(conn redis.Conn, tableName, key any) int64 { | |
| 35 | +func HGetInt64(tableName, key any) int64 { | |
| 36 | + conn, err := GetConn() | |
| 37 | + if err != nil { | |
| 38 | + return 0 | |
| 39 | + } | |
| 40 | + defer autoClose(conn) | |
| 41 | + | |
| 24 | 42 | reply, err := redis.Int64(conn.Do("HGET", tableName, key)) |
| 25 | 43 | if err == nil { |
| 26 | 44 | return reply | ... | ... |
util/zredis/hash_data.go
| ... | ... | @@ -6,13 +6,25 @@ import ( |
| 6 | 6 | ) |
| 7 | 7 | |
| 8 | 8 | // HSetData HSetData |
| 9 | -func HSetData(conn redis.Conn, value IData) error { | |
| 10 | - _, err := conn.Do("HSET", value.TableName(), value.DbKey(), value.ToString()) | |
| 9 | +func HSetData(value IData) error { | |
| 10 | + conn, err := GetConn() | |
| 11 | + if err != nil { | |
| 12 | + return err | |
| 13 | + } | |
| 14 | + defer autoClose(conn) | |
| 15 | + | |
| 16 | + _, err = conn.Do("HSET", value.TableName(), value.DbKey(), value.ToString()) | |
| 11 | 17 | return err |
| 12 | 18 | } |
| 13 | 19 | |
| 14 | 20 | // HGetData HSetData |
| 15 | -func HGetData(conn redis.Conn, value IData) IData { | |
| 21 | +func HGetData(value IData) IData { | |
| 22 | + conn, err := GetConn() | |
| 23 | + if err != nil { | |
| 24 | + return nil | |
| 25 | + } | |
| 26 | + defer autoClose(conn) | |
| 27 | + | |
| 16 | 28 | text, err := redis.String(conn.Do("HGET", value.TableName(), value.DbKey())) |
| 17 | 29 | if err == nil { |
| 18 | 30 | errJson := zjson.Obj(text, value) | ... | ... |
| ... | ... | @@ -0,0 +1,31 @@ |
| 1 | +package zredis | |
| 2 | + | |
| 3 | +import ( | |
| 4 | + "github.com/gomodule/redigo/redis" | |
| 5 | + "time" | |
| 6 | +) | |
| 7 | + | |
| 8 | +var ( | |
| 9 | + _addr string | |
| 10 | + _pass string | |
| 11 | + _db int | |
| 12 | +) | |
| 13 | +var pool *redis.Pool | |
| 14 | + | |
| 15 | +// autoClose 自动关闭 | |
| 16 | +func autoClose(conn redis.Conn) { | |
| 17 | + err := conn.Close() | |
| 18 | + if err != nil { | |
| 19 | + } | |
| 20 | +} | |
| 21 | + | |
| 22 | +func dial() (redis.Conn, error) { | |
| 23 | + conn, err := redis.Dial("tcp", _addr, | |
| 24 | + redis.DialPassword(_pass), | |
| 25 | + redis.DialDatabase(_db), | |
| 26 | + redis.DialConnectTimeout(5*time.Second), | |
| 27 | + redis.DialReadTimeout(5*time.Second), | |
| 28 | + redis.DialWriteTimeout(5*time.Second), | |
| 29 | + ) | |
| 30 | + return conn, err | |
| 31 | +} | ... | ... |
util/zredis/kv.go
| ... | ... | @@ -5,11 +5,11 @@ import ( |
| 5 | 5 | ) |
| 6 | 6 | |
| 7 | 7 | func Increment(key string) int64 { |
| 8 | - conn := GetConn() | |
| 9 | - if conn == nil { | |
| 8 | + conn, err := GetConn() | |
| 9 | + if err != nil { | |
| 10 | 10 | return 0 |
| 11 | 11 | } |
| 12 | - defer conn.Close() | |
| 12 | + defer autoClose(conn) | |
| 13 | 13 | |
| 14 | 14 | value, err := redis.Int64(conn.Do("INCR", key)) |
| 15 | 15 | if err == nil { |
| ... | ... | @@ -18,22 +18,46 @@ func Increment(key string) int64 { |
| 18 | 18 | return 0 |
| 19 | 19 | } |
| 20 | 20 | |
| 21 | -func Set(conn redis.Conn, key, value any) (err error) { | |
| 21 | +func Set(key, value any) (err error) { | |
| 22 | + conn, errConn := GetConn() | |
| 23 | + if errConn != nil { | |
| 24 | + return errConn | |
| 25 | + } | |
| 26 | + defer autoClose(conn) | |
| 27 | + | |
| 22 | 28 | _, err = conn.Do("Set", key, value) |
| 23 | 29 | return |
| 24 | 30 | } |
| 25 | 31 | |
| 26 | -func Get(conn redis.Conn, key string) (value string, err error) { | |
| 32 | +func Get(key string) (value string, err error) { | |
| 33 | + conn, errConn := GetConn() | |
| 34 | + if errConn != nil { | |
| 35 | + return "", errConn | |
| 36 | + } | |
| 37 | + defer autoClose(conn) | |
| 38 | + | |
| 27 | 39 | value, err = redis.String(conn.Do("Get", key)) |
| 28 | 40 | return |
| 29 | 41 | } |
| 30 | 42 | |
| 31 | -func SetEx(conn redis.Conn, key, value string, exTime int) (err error) { | |
| 43 | +func SetEx(key, value string, exTime int) (err error) { | |
| 44 | + conn, errConn := GetConn() | |
| 45 | + if errConn != nil { | |
| 46 | + return errConn | |
| 47 | + } | |
| 48 | + defer autoClose(conn) | |
| 49 | + | |
| 32 | 50 | _, err = conn.Do("Set", key, value, "EX", exTime) |
| 33 | 51 | return |
| 34 | 52 | } |
| 35 | 53 | |
| 36 | -func GetString(conn redis.Conn, key string) string { | |
| 54 | +func GetString(key string) string { | |
| 55 | + conn, errConn := GetConn() | |
| 56 | + if errConn != nil { | |
| 57 | + return "" | |
| 58 | + } | |
| 59 | + defer autoClose(conn) | |
| 60 | + | |
| 37 | 61 | value, err := redis.String(conn.Do("Get", key)) |
| 38 | 62 | if err == nil { |
| 39 | 63 | return value |
| ... | ... | @@ -41,7 +65,13 @@ func GetString(conn redis.Conn, key string) string { |
| 41 | 65 | return "" |
| 42 | 66 | } |
| 43 | 67 | |
| 44 | -func GetInt64(conn redis.Conn, key string) int64 { | |
| 68 | +func GetInt64(key string) int64 { | |
| 69 | + conn, errConn := GetConn() | |
| 70 | + if errConn != nil { | |
| 71 | + return 0 | |
| 72 | + } | |
| 73 | + defer autoClose(conn) | |
| 74 | + | |
| 45 | 75 | value, err := redis.Int64(conn.Do("Get", key)) |
| 46 | 76 | if err == nil { |
| 47 | 77 | return value | ... | ... |
util/zredis/redis.go
| ... | ... | @@ -1,50 +0,0 @@ |
| 1 | -package zredis | |
| 2 | - | |
| 3 | -import ( | |
| 4 | - "github.com/gomodule/redigo/redis" | |
| 5 | - "time" | |
| 6 | -) | |
| 7 | - | |
| 8 | -var ( | |
| 9 | - _addr string | |
| 10 | - _pass string | |
| 11 | - _db int | |
| 12 | -) | |
| 13 | -var pool *redis.Pool | |
| 14 | - | |
| 15 | -func Init(addr, pass string, db int) { | |
| 16 | - | |
| 17 | - _addr, _pass, _db = addr, pass, db | |
| 18 | - | |
| 19 | - pool = &redis.Pool{ | |
| 20 | - MaxActive: 10240, | |
| 21 | - MaxIdle: 10240, // Maximum number of idle connections in the pool. | |
| 22 | - IdleTimeout: 240 * time.Second, // Close connections after remaining idle for this duration | |
| 23 | - Dial: dial, | |
| 24 | - Wait: true, | |
| 25 | - } | |
| 26 | -} | |
| 27 | - | |
| 28 | -func dial() (redis.Conn, error) { | |
| 29 | - conn, err := redis.Dial("tcp", _addr, | |
| 30 | - redis.DialPassword(_pass), | |
| 31 | - redis.DialDatabase(_db), | |
| 32 | - redis.DialConnectTimeout(5*time.Second), | |
| 33 | - redis.DialReadTimeout(5*time.Second), | |
| 34 | - redis.DialWriteTimeout(5*time.Second), | |
| 35 | - ) | |
| 36 | - return conn, err | |
| 37 | -} | |
| 38 | - | |
| 39 | -func GetConn() redis.Conn { | |
| 40 | - if pool == nil { | |
| 41 | - return nil | |
| 42 | - } | |
| 43 | - return pool.Get() | |
| 44 | -} | |
| 45 | - | |
| 46 | -func AutoClose(conn redis.Conn) { | |
| 47 | - err := conn.Close() | |
| 48 | - if err != nil { | |
| 49 | - } | |
| 50 | -} |