diff --git a/configs/confapi/config.go b/configs/confapi/config.go index ec05d0d..75277f6 100644 --- a/configs/confapi/config.go +++ b/configs/confapi/config.go @@ -13,12 +13,14 @@ type ApiGameConfig struct { func (c *ApiGameConfig) ConfInfo(suffix string) *confbase.ConfInfo { tableName := "s_game_config" + cacheKey := fmt.Sprintf("%s:%s:%s", svconst.REDIS_CACHEP_REFIX, tableName, suffix) return &confbase.ConfInfo{ - DbMysql: svconst.DbApi, - TableName: tableName, - KeyName: "gameid", - CacheKey: fmt.Sprintf("%s:%s:%s", svconst.REDIS_CACHEP_REFIX, tableName, suffix), - CacheTime: 300, + DbMysql: svconst.DbApi, + TableName: tableName, + KeyName: "gameid", + CacheKey: cacheKey, + CacheCurrent: cacheKey + ":current", + CacheTime: 300, } } diff --git a/configs/confbase/index.go b/configs/confbase/index.go index cdc6e4c..efa57ad 100644 --- a/configs/confbase/index.go +++ b/configs/confbase/index.go @@ -31,11 +31,11 @@ func LoadCache[T IConfData](gameId string, obj T) (has bool) { return } -func LoadData[T1 IConfData, T2 IConfRawData](gameId string, id any, obj T1) (has bool) { +func LoadData[T1 IConfData, T2 IConfRawData](gameId string, confId 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) + result := db.Table(info.TableName).Where(fmt.Sprintf("%s = ?", info.KeyName), confId).First(confRaw) has = result.RowsAffected != 0 err := result.Error @@ -60,12 +60,12 @@ func FindDuringTime(obj svmysql.IMysqlData, gameId string) (has bool) { return } -func GetConfig[T1 IConfData, T2 IConfRawData](gameId string, id any, obj T1) (has bool) { +func GetConfig[T1 IConfData, T2 IConfRawData](gameId string, confId any, obj T1) (has bool) { has = LoadCache(gameId, obj) if has { return } - has = LoadData[T1, T2](gameId, id, obj) + has = LoadData[T1, T2](gameId, confId, obj) if !has { return } diff --git a/configs/confbase/interface.go b/configs/confbase/interface.go index 7e2a785..61ca0e2 100644 --- a/configs/confbase/interface.go +++ b/configs/confbase/interface.go @@ -4,11 +4,12 @@ import "gorm.io/gorm" // ConfInfo 配置对象信息 type ConfInfo struct { - DbMysql *gorm.DB - TableName string - KeyName string - CacheKey string - CacheTime int + DbMysql *gorm.DB + TableName string + KeyName string + CacheKey string + CacheCurrent string + CacheTime int } // IConfData 配置对象 diff --git a/configs/confcardholder/config.go b/configs/confcardholder/config.go index cbbb777..959a811 100644 --- a/configs/confcardholder/config.go +++ b/configs/confcardholder/config.go @@ -1,6 +1,7 @@ package confcardholder import ( + "apigame/configs/confbase" "apigame/service-common/svconst" "apigame/service-common/svmysql" "apigame/service-common/svredis" @@ -31,6 +32,19 @@ type ActivityConfig struct { GameId string // 所属游戏ID } +func (c *ActivityConfig) ConfInfo(suffix string) *confbase.ConfInfo { + tableName := svconst.MYSQL_TABLE_S_CARDHOLDER_CONFIG + cacheKey := fmt.Sprintf("%s:%s:%s", svconst.REDIS_CACHEP_REFIX, tableName, suffix) + return &confbase.ConfInfo{ + DbMysql: svconst.DbConfig, + TableName: fmt.Sprintf("%s_%s", tableName, suffix), + KeyName: "gameid", + CacheKey: fmt.Sprintf("%s:%d", cacheKey, c.Id), + CacheCurrent: cacheKey + ":current", + CacheTime: 300, + } +} + func (c *ActivityConfig) RedisInfo(suffix string) *svredis.RedisInfo { tableName := svconst.MYSQL_TABLE_S_CARDHOLDER_CONFIG return &svredis.RedisInfo{ diff --git a/configs/confcardholder/decode.go b/configs/confcardholder/decode.go index 40485e6..742bf2d 100644 --- a/configs/confcardholder/decode.go +++ b/configs/confcardholder/decode.go @@ -9,17 +9,18 @@ import ( ) // Decode 解析配置原始数据 -func (c *ActivityConfig) Decode(gameId string, configRaw *ActivityConfigRaw) { +func (c *ActivityConfig) Decode(gameId string, rawData any) { + raw := rawData.(*ActivityConfigRaw) c.GameId = gameId - c.Raw = configRaw + c.Raw = raw - c.Id = configRaw.Id - c.OpenLevel = configRaw.OpenLevel - c.PreviewTime = configRaw.PreviewTime - c.StartTime = configRaw.StartTime - c.EndTime = configRaw.EndTime - c.Round = configRaw.Round - c.IconPath = configRaw.IconPath + c.Id = raw.Id + c.OpenLevel = raw.OpenLevel + c.PreviewTime = raw.PreviewTime + c.StartTime = raw.StartTime + c.EndTime = raw.EndTime + c.Round = raw.Round + c.IconPath = raw.IconPath c.Awards = make(map[string]string) c.AlbumConfig = make(map[int]AlbumConfig) @@ -30,18 +31,18 @@ func (c *ActivityConfig) Decode(gameId string, configRaw *ActivityConfigRaw) { c.StarShopConfig = make(map[int]StarShopConfig) // 解析奖励 { - err := json.Unmarshal([]byte(configRaw.Awards), &c.Awards) + err := json.Unmarshal([]byte(raw.Awards), &c.Awards) if err != nil { - lxalilog.Errors(err, configRaw.Awards, gameId, configRaw.Id) + lxalilog.Errors(err, raw.Awards, gameId, raw.Id) return } } // 卡组配置 { configs := make([]AlbumConfig, 0) - err := json.Unmarshal([]byte(configRaw.AlbumConfig), &configs) + err := json.Unmarshal([]byte(raw.AlbumConfig), &configs) if err != nil { - lxalilog.Errors(err, configRaw.AlbumConfig, gameId, configRaw.Id) + lxalilog.Errors(err, raw.AlbumConfig, gameId, raw.Id) return } for _, i2 := range configs { @@ -51,9 +52,9 @@ func (c *ActivityConfig) Decode(gameId string, configRaw *ActivityConfigRaw) { // 卡牌配置 { configs := make([]CardConfig, 0) - err := json.Unmarshal([]byte(configRaw.CardConfig), &configs) + err := json.Unmarshal([]byte(raw.CardConfig), &configs) if err != nil { - lxalilog.Errors(err, configRaw.CardConfig, gameId, configRaw.Id) + lxalilog.Errors(err, raw.CardConfig, gameId, raw.Id) return } for _, i2 := range configs { @@ -63,9 +64,9 @@ func (c *ActivityConfig) Decode(gameId string, configRaw *ActivityConfigRaw) { // 卡包开卡规则 { configs := make([]OpenCardholderConfig, 0) - err := json.Unmarshal([]byte(configRaw.CardHolderConfig), &configs) + err := json.Unmarshal([]byte(raw.CardHolderConfig), &configs) if err != nil { - lxalilog.Errors(err, configRaw.CardHolderConfig, gameId, configRaw.Id) + lxalilog.Errors(err, raw.CardHolderConfig, gameId, raw.Id) return } for _, i2 := range configs { @@ -75,9 +76,9 @@ func (c *ActivityConfig) Decode(gameId string, configRaw *ActivityConfigRaw) { // 卡片星级配置 { configs := make([]NormalCardStarConfig, 0) - err := json.Unmarshal([]byte(configRaw.NormalCardStarSequence), &configs) + err := json.Unmarshal([]byte(raw.NormalCardStarSequence), &configs) if err != nil { - lxalilog.Errors(err, configRaw.NormalCardStarSequence, gameId, configRaw.Id) + lxalilog.Errors(err, raw.NormalCardStarSequence, gameId, raw.Id) return } for _, i2 := range configs { @@ -89,9 +90,9 @@ func (c *ActivityConfig) Decode(gameId string, configRaw *ActivityConfigRaw) { // 卡片星级对应卡牌配置 { configs := make([]CardSequenceConfig, 0) - err := json.Unmarshal([]byte(configRaw.CardSequenceConfig), &configs) + err := json.Unmarshal([]byte(raw.CardSequenceConfig), &configs) if err != nil { - lxalilog.Errors(err, configRaw.CardSequenceConfig, gameId, configRaw.Id) + lxalilog.Errors(err, raw.CardSequenceConfig, gameId, raw.Id) return } for _, i2 := range configs { @@ -103,9 +104,9 @@ func (c *ActivityConfig) Decode(gameId string, configRaw *ActivityConfigRaw) { // 星星商店配置 { configs := make([]StarShopConfig, 0) - err := json.Unmarshal([]byte(configRaw.StarShopConfig), &configs) + err := json.Unmarshal([]byte(raw.StarShopConfig), &configs) if err != nil { - lxalilog.Errors(err, configRaw.StarShopConfig, gameId, configRaw.Id) + lxalilog.Errors(err, raw.StarShopConfig, gameId, raw.Id) return } for _, i2 := range configs { diff --git a/configs/confcardholder/get.go b/configs/confcardholder/get.go index cd2584f..1f817c2 100644 --- a/configs/confcardholder/get.go +++ b/configs/confcardholder/get.go @@ -3,9 +3,7 @@ package confcardholder import ( "apigame/configs/confbase" "apigame/service-common/svconst" - "apigame/service-common/svmysql" "apigame/service-common/svredis" - "apigame/util/util-lx/lxalilog" "apigame/util/util-lx/lxtime" "apigame/util/utstring" "apigame/util/zredis" @@ -47,27 +45,34 @@ func GetCurrent(gameId string) (conf *ActivityConfig, has bool) { // GetConfig 获取 配置根据Id func GetConfig(gameId string, confId int64) (conf *ActivityConfig, has bool) { - var err error - conf = &ActivityConfig{Id: confId} - has = svredis.LoadData(gameId, conf) - if has { - fmt.Println("dwjw confcardholder.GetConfig use cache") - return - } - confRaw := new(ActivityConfigRaw) - has, err = svmysql.First(confRaw, gameId) - if err != nil { - lxalilog.Errors(err, "confcardholder.GetConfig error", gameId) - return - } + conf = new(ActivityConfig) + has := confbase.GetConfig[*ActivityConfig, ActivityConfigRaw](gameId, gameId, conf) if !has { + err = errors.New("confapi.GetConfig error") return } - conf.Decode(gameId, confRaw) - - fmt.Println("dwjw confcardholder.GetConfig save cache") - svredis.SaveData(gameId, conf) + //var err error + //conf = &ActivityConfig{Id: confId} + //has = svredis.LoadData(gameId, conf) + //if has { + // fmt.Println("dwjw confcardholder.GetConfig use cache") + // return + //} + //confRaw := new(ActivityConfigRaw) + //has, err = svmysql.First(confRaw, gameId) + //if err != nil { + // lxalilog.Errors(err, "confcardholder.GetConfig error", gameId) + // return + //} + //if !has { + // return + //} + // + //conf.Decode(gameId, confRaw) + // + //fmt.Println("dwjw confcardholder.GetConfig save cache") + //svredis.SaveData(gameId, conf) return } -- libgit2 0.21.0