Commit 17fdeb1214a45b94be9a2d32b024885326aae5f0
1 parent
c5e3ee0e
Exists in
master
and in
1 other branch
refactor♻️:项目目录重构
Showing
7 changed files
with
53 additions
and
37 deletions
Show diff stats
| @@ -0,0 +1 @@ | @@ -0,0 +1 @@ | ||
| 1 | +package svmysql |
common/svredis/index.go
| @@ -3,6 +3,7 @@ package svredis | @@ -3,6 +3,7 @@ package svredis | ||
| 3 | import ( | 3 | import ( |
| 4 | "apigame/common/svconst" | 4 | "apigame/common/svconst" |
| 5 | "apigame/util/utstring" | 5 | "apigame/util/utstring" |
| 6 | + "apigame/util/zjson" | ||
| 6 | "apigame/util/zredis" | 7 | "apigame/util/zredis" |
| 7 | "fmt" | 8 | "fmt" |
| 8 | "github.com/astaxie/beego" | 9 | "github.com/astaxie/beego" |
| @@ -33,3 +34,25 @@ func Init() bool { | @@ -33,3 +34,25 @@ func Init() bool { | ||
| 33 | fmt.Println(svconst.AppName + " redis init success") | 34 | fmt.Println(svconst.AppName + " redis init success") |
| 34 | return true | 35 | return true |
| 35 | } | 36 | } |
| 37 | + | ||
| 38 | +func SaveData(gameId string, obj IRedisInfo) { | ||
| 39 | + info := obj.RedisInfo(gameId) | ||
| 40 | + _ = zredis.SetEx(zredis.GetConn(), info.CacheKey, zjson.Str(obj), info.CacheTime) | ||
| 41 | +} | ||
| 42 | + | ||
| 43 | +func LoadData[T IRedisInfo](gameId string, obj T) (has bool) { | ||
| 44 | + has = true | ||
| 45 | + info := obj.RedisInfo(gameId) | ||
| 46 | + text, err := zredis.Get(zredis.GetConn(), info.CacheKey) | ||
| 47 | + if err != nil { | ||
| 48 | + has = false | ||
| 49 | + return | ||
| 50 | + } | ||
| 51 | + err = zjson.Obj(text, &obj) | ||
| 52 | + if err != nil { | ||
| 53 | + fmt.Println(err) | ||
| 54 | + has = false | ||
| 55 | + return | ||
| 56 | + } | ||
| 57 | + return | ||
| 58 | +} |
configs/feat-api.go
| @@ -3,6 +3,7 @@ package configs | @@ -3,6 +3,7 @@ package configs | ||
| 3 | import ( | 3 | import ( |
| 4 | "apigame/common/svconst" | 4 | "apigame/common/svconst" |
| 5 | "apigame/common/svdto" | 5 | "apigame/common/svdto" |
| 6 | + "apigame/common/svredis" | ||
| 6 | "apigame/util/utdto" | 7 | "apigame/util/utdto" |
| 7 | "fmt" | 8 | "fmt" |
| 8 | ) | 9 | ) |
| @@ -27,3 +28,11 @@ func (c *ApiGameConfig) GetRule(gameId string) *svdto.DtoRule { | @@ -27,3 +28,11 @@ func (c *ApiGameConfig) GetRule(gameId string) *svdto.DtoRule { | ||
| 27 | CacheTime: 300, | 28 | CacheTime: 300, |
| 28 | } | 29 | } |
| 29 | } | 30 | } |
| 31 | + | ||
| 32 | +func (c *ApiGameConfig) RedisInfo(gameId string) *svredis.RedisInfo { | ||
| 33 | + tableName := "s_game_config" | ||
| 34 | + return &svredis.RedisInfo{ | ||
| 35 | + CacheKey: fmt.Sprintf("%s:%s:%s", svdto.REDIS_CACHEP_REFIX, tableName, gameId), | ||
| 36 | + CacheTime: 300, | ||
| 37 | + } | ||
| 38 | +} |
configs/index.go
| @@ -2,9 +2,6 @@ package configs | @@ -2,9 +2,6 @@ package configs | ||
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "apigame/common/svdto" | 4 | "apigame/common/svdto" |
| 5 | - "apigame/util/zjson" | ||
| 6 | - "apigame/util/zredis" | ||
| 7 | - "fmt" | ||
| 8 | ) | 5 | ) |
| 9 | 6 | ||
| 10 | // func GetConfigRedis[T svconfig.IDtoData](gameId string) (result *T, has bool) { | 7 | // func GetConfigRedis[T svconfig.IDtoData](gameId string) (result *T, has bool) { |
| @@ -15,33 +12,3 @@ import ( | @@ -15,33 +12,3 @@ import ( | ||
| 15 | func GetConfigMysql[T svdto.IDtoData](gameId string, t T) (result *T, has bool) { | 12 | func GetConfigMysql[T svdto.IDtoData](gameId string, t T) (result *T, has bool) { |
| 16 | return | 13 | return |
| 17 | } | 14 | } |
| 18 | - | ||
| 19 | -func GetConfig[T svdto.IDtoData](gameId string, t T) (has bool) { | ||
| 20 | - has = CacheLoad[T](gameId, t) | ||
| 21 | - if !has { | ||
| 22 | - | ||
| 23 | - } | ||
| 24 | - return | ||
| 25 | -} | ||
| 26 | - | ||
| 27 | -func CacheSave(gameId string, conf svdto.IDtoData) { | ||
| 28 | - rule := conf.GetRule(gameId) | ||
| 29 | - _ = zredis.SetEx(zredis.GetConn(), rule.CacheKey, zjson.Str(conf), rule.CacheTime) | ||
| 30 | -} | ||
| 31 | - | ||
| 32 | -func CacheLoad[T svdto.IDtoData](gameId string, t T) (has bool) { | ||
| 33 | - has = true | ||
| 34 | - rule := t.GetRule(gameId) | ||
| 35 | - text, err := zredis.Get(zredis.GetConn(), rule.CacheKey) | ||
| 36 | - if err != nil { | ||
| 37 | - has = false | ||
| 38 | - return | ||
| 39 | - } | ||
| 40 | - err = zjson.Obj(text, &t) | ||
| 41 | - if err != nil { | ||
| 42 | - fmt.Println(err) | ||
| 43 | - has = false | ||
| 44 | - return | ||
| 45 | - } | ||
| 46 | - return | ||
| 47 | -} |
configs/registry.go
| 1 | package configs | 1 | package configs |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | + "apigame/common/svredis" | ||
| 4 | "apigame/util/utdto" | 5 | "apigame/util/utdto" |
| 5 | "apigame/util/util-lx/lxalilog" | 6 | "apigame/util/util-lx/lxalilog" |
| 6 | "fmt" | 7 | "fmt" |
| @@ -9,9 +10,9 @@ import ( | @@ -9,9 +10,9 @@ import ( | ||
| 9 | // GetApiGame 获取 api游戏配置 | 10 | // GetApiGame 获取 api游戏配置 |
| 10 | func GetApiGame(gameId string) (conf *ApiGameConfig, err error) { | 11 | func GetApiGame(gameId string) (conf *ApiGameConfig, err error) { |
| 11 | conf = new(ApiGameConfig) | 12 | conf = new(ApiGameConfig) |
| 12 | - has := CacheLoad(gameId, conf) | 13 | + has := svredis.LoadData(gameId, conf) |
| 13 | if has { | 14 | if has { |
| 14 | - fmt.Println("GetApiGame use cache") | 15 | + fmt.Println("dwjw GetApiGame use cache") |
| 15 | return | 16 | return |
| 16 | } | 17 | } |
| 17 | rule := conf.GetRule(gameId) | 18 | rule := conf.GetRule(gameId) |
| @@ -22,8 +23,8 @@ func GetApiGame(gameId string) (conf *ApiGameConfig, err error) { | @@ -22,8 +23,8 @@ func GetApiGame(gameId string) (conf *ApiGameConfig, err error) { | ||
| 22 | lxalilog.Errors(err, gameId) | 23 | lxalilog.Errors(err, gameId) |
| 23 | return | 24 | return |
| 24 | } | 25 | } |
| 25 | - fmt.Println("GetApiGame save cache") | ||
| 26 | - CacheSave(gameId, conf) | 26 | + fmt.Println("dwjw GetApiGame save cache") |
| 27 | + svredis.SaveData(gameId, conf) | ||
| 27 | 28 | ||
| 28 | return | 29 | return |
| 29 | } | 30 | } |
util/utdto/base.go
| 1 | package utdto | 1 | package utdto |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | + "fmt" | ||
| 4 | "gorm.io/gorm" | 5 | "gorm.io/gorm" |
| 5 | "strings" | 6 | "strings" |
| 6 | ) | 7 | ) |
| @@ -32,6 +33,7 @@ func Save(db *gorm.DB, value any, tableName string) *gorm.DB { | @@ -32,6 +33,7 @@ func Save(db *gorm.DB, value any, tableName string) *gorm.DB { | ||
| 32 | func First(db *gorm.DB, value any, tableName string) *gorm.DB { | 33 | func First(db *gorm.DB, value any, tableName string) *gorm.DB { |
| 33 | stmt := db.Session(&gorm.Session{DryRun: true}).First(value).Statement | 34 | stmt := db.Session(&gorm.Session{DryRun: true}).First(value).Statement |
| 34 | stmtSQL := stmt.SQL.String() | 35 | stmtSQL := stmt.SQL.String() |
| 36 | + fmt.Println("dwjw🐷 stmt.Table", stmt.Table) | ||
| 35 | sql := strings.Replace(stmtSQL, MYSQL_TABLE_TEMPLATE, tableName, -1) | 37 | sql := strings.Replace(stmtSQL, MYSQL_TABLE_TEMPLATE, tableName, -1) |
| 36 | return db.Raw(sql, stmt.Vars...).Scan(value) | 38 | return db.Raw(sql, stmt.Vars...).Scan(value) |
| 37 | } | 39 | } |