From a17aa6cab25fafff7232ecad89812e13465a6688 Mon Sep 17 00:00:00 2001 From: 王家文 Date: Thu, 18 Apr 2024 18:45:15 +0800 Subject: [PATCH] feat✨:游戏功能配置 --- configs/confapi/config.go | 24 ++++++------------------ configs/confapi/get.go | 21 +++++---------------- configs/confbase/index.go | 17 +++++------------ middleware/sign/index.go | 2 +- 4 files changed, 17 insertions(+), 47 deletions(-) diff --git a/configs/confapi/config.go b/configs/confapi/config.go index 0e612f8..ec05d0d 100644 --- a/configs/confapi/config.go +++ b/configs/confapi/config.go @@ -3,8 +3,6 @@ package confapi import ( "apigame/configs/confbase" "apigame/service-common/svconst" - "apigame/service-common/svmysql" - "apigame/service-common/svredis" "fmt" ) @@ -24,22 +22,6 @@ func (c *ApiGameConfig) ConfInfo(suffix string) *confbase.ConfInfo { } } -func (c *ApiGameConfig) RedisInfo(suffix string) *svredis.RedisInfo { - tableName := "s_game_config" - return &svredis.RedisInfo{ - CacheKey: fmt.Sprintf("%s:%s:%s", svconst.REDIS_CACHEP_REFIX, tableName, suffix), - CacheTime: 300, - } -} - -func (c *ApiGameConfig) MysqlInfo(suffix string) *svmysql.MysqlInfo { - tableName := "s_game_config" - return &svmysql.MysqlInfo{ - DbMysql: svconst.DbApi.Where("gameid = ?", suffix), - TableName: tableName, - } -} - // Raw 配置原始数据 type Raw struct { AppId string `gorm:"column:appid"` @@ -48,3 +30,9 @@ type Raw struct { AppKey string `gorm:"column:appkey"` Name string `gorm:"column:name"` } + +// Decode 解析配置原始数据 +func (c *ApiGameConfig) Decode(gameId string, rawData any) { + raw := rawData.(*Raw) + c.Raw = raw +} diff --git a/configs/confapi/get.go b/configs/confapi/get.go index 184f2a6..f14dce2 100644 --- a/configs/confapi/get.go +++ b/configs/confapi/get.go @@ -1,30 +1,19 @@ package confapi import ( - "apigame/service-common/svmysql" - "apigame/service-common/svredis" - "apigame/util/util-lx/lxalilog" - "fmt" + "apigame/configs/confbase" + "errors" ) // GetConfig 获取 api游戏配置 func GetConfig(gameId string) (conf *ApiGameConfig, err error) { + conf = new(ApiGameConfig) - has := svredis.LoadData(gameId, conf) - if has { - fmt.Println("dwjw confapi.GetConfig use cache") - return - } - has, err = svmysql.First(conf, gameId) - if err != nil { - lxalilog.Errors(err, "confapi.GetConfig error", gameId) - return - } + has := confbase.GetConfig[*ApiGameConfig, Raw](gameId, gameId, conf) if !has { + err = errors.New("confapi.GetConfig error") return } - fmt.Println("dwjw confapi.GetConfig save cache") - svredis.SaveData(gameId, conf) return } diff --git a/configs/confbase/index.go b/configs/confbase/index.go index f1906d2..cdc6e4c 100644 --- a/configs/confbase/index.go +++ b/configs/confbase/index.go @@ -9,7 +9,7 @@ import ( "fmt" ) -func SaveCache(gameId string, obj IConfData) { +func SaveCache[T IConfData](gameId string, obj T) { info := obj.ConfInfo(gameId) _ = zredis.SetEx(zredis.GetConn(), info.CacheKey, zjson.Str(obj), info.CacheTime) } @@ -61,21 +61,14 @@ func FindDuringTime(obj svmysql.IMysqlData, gameId string) (has bool) { } func GetConfig[T1 IConfData, T2 IConfRawData](gameId string, id any, obj T1) (has bool) { - confRaw := new(T2) - info := obj.ConfInfo(gameId) - db := info.DbMysql - result := db.Table(info.TableName).Where(fmt.Sprintf("%s = ?", info.KeyName), id).First(confRaw) - - has = result.RowsAffected != 0 - err := result.Error - if err != nil { - lxalilog.Errors(err, "confbase.LoadData error", gameId) + has = LoadCache(gameId, obj) + if has { return } + has = LoadData[T1, T2](gameId, id, obj) if !has { return } - - obj.Decode(gameId, confRaw) + SaveCache(gameId, obj) return } diff --git a/middleware/sign/index.go b/middleware/sign/index.go index f0284df..f0ab408 100644 --- a/middleware/sign/index.go +++ b/middleware/sign/index.go @@ -232,7 +232,7 @@ func CheckSign(data interface{}, checkToken bool) (code string, gameconfig *conf return } - appkey := gameconfig.AppKey + appkey := gameconfig.Raw.AppKey if appkey == "" { code = code_msg.RECODE_SIGNERROR -- libgit2 0.21.0