From 17fdeb1214a45b94be9a2d32b024885326aae5f0 Mon Sep 17 00:00:00 2001 From: 王家文 Date: Wed, 17 Apr 2024 10:16:10 +0800 Subject: [PATCH] refactor♻️:项目目录重构 --- common/svmysql/interface.go | 1 + common/svredis/index.go | 23 +++++++++++++++++++++++ common/svredis/interface.go | 13 +++++++++++++ configs/feat-api.go | 9 +++++++++ configs/index.go | 33 --------------------------------- configs/registry.go | 9 +++++---- util/utdto/base.go | 2 ++ 7 files changed, 53 insertions(+), 37 deletions(-) create mode 100644 common/svmysql/interface.go create mode 100644 common/svredis/interface.go diff --git a/common/svmysql/interface.go b/common/svmysql/interface.go new file mode 100644 index 0000000..0c6b64e --- /dev/null +++ b/common/svmysql/interface.go @@ -0,0 +1 @@ +package svmysql diff --git a/common/svredis/index.go b/common/svredis/index.go index 570eb23..42b0904 100644 --- a/common/svredis/index.go +++ b/common/svredis/index.go @@ -3,6 +3,7 @@ package svredis import ( "apigame/common/svconst" "apigame/util/utstring" + "apigame/util/zjson" "apigame/util/zredis" "fmt" "github.com/astaxie/beego" @@ -33,3 +34,25 @@ func Init() bool { fmt.Println(svconst.AppName + " redis init success") return true } + +func SaveData(gameId string, obj IRedisInfo) { + info := obj.RedisInfo(gameId) + _ = zredis.SetEx(zredis.GetConn(), info.CacheKey, zjson.Str(obj), info.CacheTime) +} + +func LoadData[T IRedisInfo](gameId string, obj T) (has bool) { + has = true + info := obj.RedisInfo(gameId) + text, err := zredis.Get(zredis.GetConn(), info.CacheKey) + if err != nil { + has = false + return + } + err = zjson.Obj(text, &obj) + if err != nil { + fmt.Println(err) + has = false + return + } + return +} diff --git a/common/svredis/interface.go b/common/svredis/interface.go new file mode 100644 index 0000000..47dedae --- /dev/null +++ b/common/svredis/interface.go @@ -0,0 +1,13 @@ +package svredis + +// RedisInfo redis存储信息 +type RedisInfo struct { + CacheKey string + CacheTime int +} + +// IRedisInfo redis存储信息 +type IRedisInfo interface { + // RedisInfo redis存储信息 + RedisInfo(gameId string) *RedisInfo +} diff --git a/configs/feat-api.go b/configs/feat-api.go index f19d113..3064700 100644 --- a/configs/feat-api.go +++ b/configs/feat-api.go @@ -3,6 +3,7 @@ package configs import ( "apigame/common/svconst" "apigame/common/svdto" + "apigame/common/svredis" "apigame/util/utdto" "fmt" ) @@ -27,3 +28,11 @@ func (c *ApiGameConfig) GetRule(gameId string) *svdto.DtoRule { CacheTime: 300, } } + +func (c *ApiGameConfig) RedisInfo(gameId string) *svredis.RedisInfo { + tableName := "s_game_config" + return &svredis.RedisInfo{ + CacheKey: fmt.Sprintf("%s:%s:%s", svdto.REDIS_CACHEP_REFIX, tableName, gameId), + CacheTime: 300, + } +} diff --git a/configs/index.go b/configs/index.go index 27d87c8..cac20bd 100644 --- a/configs/index.go +++ b/configs/index.go @@ -2,9 +2,6 @@ package configs import ( "apigame/common/svdto" - "apigame/util/zjson" - "apigame/util/zredis" - "fmt" ) // func GetConfigRedis[T svconfig.IDtoData](gameId string) (result *T, has bool) { @@ -15,33 +12,3 @@ import ( func GetConfigMysql[T svdto.IDtoData](gameId string, t T) (result *T, has bool) { return } - -func GetConfig[T svdto.IDtoData](gameId string, t T) (has bool) { - has = CacheLoad[T](gameId, t) - if !has { - - } - return -} - -func CacheSave(gameId string, conf svdto.IDtoData) { - rule := conf.GetRule(gameId) - _ = zredis.SetEx(zredis.GetConn(), rule.CacheKey, zjson.Str(conf), rule.CacheTime) -} - -func CacheLoad[T svdto.IDtoData](gameId string, t T) (has bool) { - has = true - rule := t.GetRule(gameId) - text, err := zredis.Get(zredis.GetConn(), rule.CacheKey) - if err != nil { - has = false - return - } - err = zjson.Obj(text, &t) - if err != nil { - fmt.Println(err) - has = false - return - } - return -} diff --git a/configs/registry.go b/configs/registry.go index 92af09c..ba84136 100644 --- a/configs/registry.go +++ b/configs/registry.go @@ -1,6 +1,7 @@ package configs import ( + "apigame/common/svredis" "apigame/util/utdto" "apigame/util/util-lx/lxalilog" "fmt" @@ -9,9 +10,9 @@ import ( // GetApiGame 获取 api游戏配置 func GetApiGame(gameId string) (conf *ApiGameConfig, err error) { conf = new(ApiGameConfig) - has := CacheLoad(gameId, conf) + has := svredis.LoadData(gameId, conf) if has { - fmt.Println("GetApiGame use cache") + fmt.Println("dwjw GetApiGame use cache") return } rule := conf.GetRule(gameId) @@ -22,8 +23,8 @@ func GetApiGame(gameId string) (conf *ApiGameConfig, err error) { lxalilog.Errors(err, gameId) return } - fmt.Println("GetApiGame save cache") - CacheSave(gameId, conf) + fmt.Println("dwjw GetApiGame save cache") + svredis.SaveData(gameId, conf) return } diff --git a/util/utdto/base.go b/util/utdto/base.go index e2f247b..aeebccd 100644 --- a/util/utdto/base.go +++ b/util/utdto/base.go @@ -1,6 +1,7 @@ package utdto import ( + "fmt" "gorm.io/gorm" "strings" ) @@ -32,6 +33,7 @@ func Save(db *gorm.DB, value any, tableName string) *gorm.DB { func First(db *gorm.DB, value any, tableName string) *gorm.DB { stmt := db.Session(&gorm.Session{DryRun: true}).First(value).Statement stmtSQL := stmt.SQL.String() + fmt.Println("dwjw🐷 stmt.Table", stmt.Table) sql := strings.Replace(stmtSQL, MYSQL_TABLE_TEMPLATE, tableName, -1) return db.Raw(sql, stmt.Vars...).Scan(value) } -- libgit2 0.21.0