diff --git a/configs/confapi/config.go b/configs/confapi/config.go index 75277f6..2cbe322 100644 --- a/configs/confapi/config.go +++ b/configs/confapi/config.go @@ -11,6 +11,14 @@ type ApiGameConfig struct { Raw *Raw } +func (c *ApiGameConfig) GetUid() string { + return c.Raw.GameId +} + +func (c *ApiGameConfig) CheckCurrent() bool { + return true +} + func (c *ApiGameConfig) ConfInfo(suffix string) *confbase.ConfInfo { tableName := "s_game_config" cacheKey := fmt.Sprintf("%s:%s:%s", svconst.REDIS_CACHEP_REFIX, tableName, suffix) diff --git a/configs/confbase/index.go b/configs/confbase/index.go index 348a8a6..822a9fa 100644 --- a/configs/confbase/index.go +++ b/configs/confbase/index.go @@ -1,9 +1,7 @@ package confbase import ( - "apigame/service-common/svmysql" "apigame/util/util-lx/lxalilog" - "apigame/util/util-lx/lxtime" "apigame/util/zjson" "apigame/util/zredis" "fmt" @@ -53,11 +51,10 @@ func LoadData[T1 IConfData, T2 IConfRawData](gameId string, confId any, obj T1) return } -func FindDuringTime(obj svmysql.IMysqlData, gameId string) (has bool) { - info := obj.MysqlInfo(gameId) +func FindDuringTime[T1 IConfData, T2 IConfRawData](obj T1, raw T2, gameId string) (has bool) { + info := obj.ConfInfo(gameId) db := info.DbMysql - timeNow := lxtime.NowUninx() - result := db.Table(info.TableName).Where("start_time <= ? AND end_time >= ?", timeNow, timeNow).First(obj) + result := db.Table(info.TableName).Where(info.CurrentQuery, info.CurrentArgs).First(raw) has = result.RowsAffected != 0 return } @@ -74,3 +71,33 @@ func GetConfig[T1 IConfData, T2 IConfRawData](gameId string, confId any, obj T1) SaveCache(gameId, obj) return } + +// GetCurrent 获取 当前配置 +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) + confRaw := *new(T2) + hasFind := false + if currentId == "" { + hasFind = FindDuringTime[T1, T2](obj, confRaw, gameId) + } else { + has = GetConfig[T1, T2](gameId, currentId, obj) + if has { + if obj.CheckCurrent() { + hasFind = FindDuringTime[T1, T2](obj, confRaw, gameId) + } + } else { + hasFind = FindDuringTime[T1, T2](obj, confRaw, gameId) + } + } + if hasFind { + obj.Decode(gameId, confRaw) + SaveCache(gameId, obj) + has = true + currentId = obj.GetUid() + _ = zredis.Set(zredis.GetConn(), currentKey, currentId) + } + return has +} diff --git a/configs/confbase/interface.go b/configs/confbase/interface.go index 61ca0e2..7974c97 100644 --- a/configs/confbase/interface.go +++ b/configs/confbase/interface.go @@ -7,6 +7,8 @@ type ConfInfo struct { DbMysql *gorm.DB TableName string KeyName string + CurrentQuery any + CurrentArgs []any CacheKey string CacheCurrent string CacheTime int @@ -18,6 +20,10 @@ type IConfData interface { ConfInfo(suffix string) *ConfInfo // Decode 解码 Decode(gameId string, rawData any) + // GetUid 获取keyID + GetUid() string + // CheckCurrent 判断当前开放 + CheckCurrent() bool } // IConfRawData mysql存储对象 diff --git a/configs/confcardholder/config.go b/configs/confcardholder/config.go index fa5d188..ec69ba2 100644 --- a/configs/confcardholder/config.go +++ b/configs/confcardholder/config.go @@ -5,6 +5,8 @@ import ( "apigame/service-common/svconst" "apigame/service-common/svmysql" "apigame/service-common/svredis" + "apigame/util/util-lx/lxtime" + "apigame/util/utstring" "fmt" ) @@ -32,13 +34,25 @@ type ActivityConfig struct { GameId string // 所属游戏ID } +func (c *ActivityConfig) GetUid() string { + return utstring.Int64ToString(c.Id) +} + +func (c *ActivityConfig) CheckCurrent() bool { + timeNow := lxtime.NowUninx() + return timeNow >= c.StartTime && timeNow <= c.EndTime && c.Raw.Status == 1 +} + 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) + timeNow := lxtime.NowUninx() return &confbase.ConfInfo{ DbMysql: svconst.DbConfig, TableName: fmt.Sprintf("%s_%s", tableName, suffix), KeyName: "id", + CurrentQuery: "start_time <= ? AND end_time >= ? AND status = ?", + CurrentArgs: []any{timeNow, timeNow, 1}, CacheKey: fmt.Sprintf("%s:%d", cacheKey, c.Id), CacheCurrent: cacheKey + ":current", CacheTime: 300, diff --git a/configs/confcardholder/get.go b/configs/confcardholder/get.go index 07c70eb..78e8fd2 100644 --- a/configs/confcardholder/get.go +++ b/configs/confcardholder/get.go @@ -2,44 +2,40 @@ package confcardholder import ( "apigame/configs/confbase" - "apigame/service-common/svconst" - "apigame/service-common/svredis" - "apigame/util/util-lx/lxtime" - "apigame/util/utstring" - "apigame/util/zredis" - "fmt" ) // GetCurrent 获取 当前配置 func GetCurrent(gameId string) (conf *ActivityConfig, has bool) { - currentKey := fmt.Sprintf("%s:%s:%s:current", - svconst.REDIS_CACHEP_REFIX, - svconst.MYSQL_TABLE_S_CARDHOLDER_CONFIG, - gameId) - currentId := zredis.GetInt64(zredis.GetConn(), currentKey) - timeNow := lxtime.NowUninx() - confRaw := new(ActivityConfigRaw) - hasFind := false - if currentId == 0 { - hasFind = confbase.FindDuringTime(confRaw, gameId) - } else { - conf, has = GetConfig(gameId, currentId) - if has { - if timeNow < conf.StartTime || timeNow > conf.EndTime { - hasFind = confbase.FindDuringTime(confRaw, gameId) - } - } else { - hasFind = confbase.FindDuringTime(confRaw, gameId) - } - } - if hasFind { - conf = new(ActivityConfig) - conf.Decode(gameId, confRaw) - svredis.SaveData(gameId, conf) - has = true - currentId = conf.Id - _ = zredis.Set(zredis.GetConn(), currentKey, utstring.Int64ToString(currentId)) - } + //currentKey := fmt.Sprintf("%s:%s:%s:current", + // svconst.REDIS_CACHEP_REFIX, + // svconst.MYSQL_TABLE_S_CARDHOLDER_CONFIG, + // gameId) + //currentId := zredis.GetInt64(zredis.GetConn(), currentKey) + //timeNow := lxtime.NowUninx() + //confRaw := new(ActivityConfigRaw) + //hasFind := false + //if currentId == 0 { + // hasFind = confbase.FindDuringTime(confRaw, gameId) + //} else { + // conf, has = GetConfig(gameId, currentId) + // if has { + // if timeNow < conf.StartTime || timeNow > conf.EndTime { + // hasFind = confbase.FindDuringTime(confRaw, gameId) + // } + // } else { + // hasFind = confbase.FindDuringTime(confRaw, gameId) + // } + //} + //if hasFind { + // conf = new(ActivityConfig) + // conf.Decode(gameId, confRaw) + // svredis.SaveData(gameId, conf) + // has = true + // currentId = conf.Id + // _ = zredis.Set(zredis.GetConn(), currentKey, utstring.Int64ToString(currentId)) + //} + conf = new(ActivityConfig) + has = confbase.GetCurrent[*ActivityConfig, ActivityConfigRaw](gameId, conf) return } @@ -48,27 +44,5 @@ func GetConfig(gameId string, confId int64) (conf *ActivityConfig, has bool) { conf = new(ActivityConfig) has = confbase.GetConfig[*ActivityConfig, ActivityConfigRaw](gameId, confId, 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 } diff --git a/configs/confdemo/1.go b/configs/confdemo/1.go deleted file mode 100644 index c1a0bc1..0000000 --- a/configs/confdemo/1.go +++ /dev/null @@ -1,42 +0,0 @@ -package confdemo - -import ( - "apigame/configs/confbase" - "apigame/service-common/svconst" - "fmt" -) - -// DemoConfig 房间排行活动配置 分析后数据 -type DemoConfig struct { - Raw *DemoConfigRaw `json:"-"` - - Id int64 // ID - OpenLevel int // 开启等级 -} - -func (c *DemoConfig) ConfInfo(suffix string) *confbase.ConfInfo { - tableName := "s_demo" - return &confbase.ConfInfo{ - DbMysql: svconst.DbConfig, - TableName: fmt.Sprintf("%s_%s", tableName, suffix), - KeyName: "id", - CacheKey: fmt.Sprintf("%s:%s:%s:%d", svconst.REDIS_CACHEP_REFIX, tableName, suffix, c.Id), - CacheTime: 300, - } -} - -// DemoConfigRaw 房间排行活动配置 原始数据 -type DemoConfigRaw struct { - Id int64 `gorm:"column:id;primaryKey"` // ID - OpenLevel int // 开启等级 - PreviewTime int64 // 预告时间 - StartTime int64 // 开始时间 - EndTime int64 // 结束时间 - - Robot string // 机器人配置 - Room string // 房间配置 - - Ver string // 版本号 - Status int // 状态 0=关闭 1=开启 - UpdateTime int64 // 修改时间戳 -} diff --git a/configs/confdemo/decode.go b/configs/confdemo/decode.go deleted file mode 100644 index 8c7cbbf..0000000 --- a/configs/confdemo/decode.go +++ /dev/null @@ -1,10 +0,0 @@ -package confdemo - -// Decode 解析配置原始数据 -func (c *DemoConfig) Decode(gameId string, rawData any) { - raw := rawData.(*DemoConfigRaw) - c.Raw = raw - - c.Id = raw.Id - c.OpenLevel = raw.OpenLevel -} diff --git a/controllers/demo.go b/controllers/demo.go index 7c6da39..87a3b4a 100644 --- a/controllers/demo.go +++ b/controllers/demo.go @@ -2,8 +2,6 @@ package controllers import ( "apigame/configs" - "apigame/configs/confbase" - "apigame/configs/confdemo" "apigame/models" "apigame/service/code-msg" "apigame/util/zjson" @@ -69,23 +67,23 @@ func (c *DemoController) Demo() { fmt.Println(zjson.Str(list)) } - { - gameId := "10149" - { - conf := new(confdemo.DemoConfig) - has := confbase.LoadData[*confdemo.DemoConfig, confdemo.DemoConfigRaw](gameId, 1, conf) - fmt.Println(has) - fmt.Println(conf) - - confbase.SaveCache(gameId, conf) - } - { - conf := &confdemo.DemoConfig{Id: 1} - has := confbase.LoadCache(gameId, conf) - fmt.Println(has) - fmt.Println(conf) - } - } + //{ + // gameId := "10149" + // { + // conf := new(confdemo.DemoConfig) + // has := confbase.LoadData[*confdemo.DemoConfig, confdemo.DemoConfigRaw](gameId, 1, conf) + // fmt.Println(has) + // fmt.Println(conf) + // + // confbase.SaveCache(gameId, conf) + // } + // { + // conf := &confdemo.DemoConfig{Id: 1} + // has := confbase.LoadCache(gameId, conf) + // fmt.Println(has) + // fmt.Println(conf) + // } + //} c.RetRspCodeData(code_msg.RECODE_OK, rsp) } diff --git a/util/zredis/kv.go b/util/zredis/kv.go index e91ba90..78e2cee 100644 --- a/util/zredis/kv.go +++ b/util/zredis/kv.go @@ -18,7 +18,7 @@ func Increment(key string) int64 { return 0 } -func Set(conn redis.Conn, key, value string) (err error) { +func Set(conn redis.Conn, key, value any) (err error) { _, err = conn.Do("Set", key, value) return } @@ -33,6 +33,14 @@ func SetEx(conn redis.Conn, key, value string, exTime int) (err error) { return } +func GetString(conn redis.Conn, key string) string { + value, err := redis.String(conn.Do("Get", key)) + if err == nil { + return value + } + return "" +} + func GetInt64(conn redis.Conn, key string) int64 { value, err := redis.Int64(conn.Do("Get", key)) if err == nil { -- libgit2 0.21.0