Commit dc16f901cb8457c5ceba5ab450383d58d46524f8
1 parent
17fdeb12
Exists in
master
and in
1 other branch
refactor♻️:redis对象和mysql对象持久化和读取
Showing
6 changed files
with
123 additions
and
11 deletions
Show diff stats
common/svmysql/index.go
| @@ -4,8 +4,11 @@ import ( | @@ -4,8 +4,11 @@ import ( | ||
| 4 | "apigame/common/svconst" | 4 | "apigame/common/svconst" |
| 5 | "apigame/service/constd" | 5 | "apigame/service/constd" |
| 6 | "apigame/util/utdto" | 6 | "apigame/util/utdto" |
| 7 | + "apigame/util/util-lx/lxalilog" | ||
| 7 | "fmt" | 8 | "fmt" |
| 8 | "github.com/astaxie/beego" | 9 | "github.com/astaxie/beego" |
| 10 | + "gorm.io/gorm" | ||
| 11 | + "strings" | ||
| 9 | ) | 12 | ) |
| 10 | 13 | ||
| 11 | func Init() bool { | 14 | func Init() bool { |
| @@ -32,3 +35,85 @@ func Init() bool { | @@ -32,3 +35,85 @@ func Init() bool { | ||
| 32 | return true | 35 | return true |
| 33 | 36 | ||
| 34 | } | 37 | } |
| 38 | + | ||
| 39 | +func InitTable(obj IMysqlData, gameId string) { | ||
| 40 | + info := obj.MysqlInfo(gameId) | ||
| 41 | + utdto.InitTable(info.DbMysql, obj, info.TableName) | ||
| 42 | +} | ||
| 43 | + | ||
| 44 | +func Insert(obj IMysqlData, gameId string) (err error) { | ||
| 45 | + info := obj.MysqlInfo(gameId) | ||
| 46 | + db := info.DbMysql | ||
| 47 | + stmt := db.Session(&gorm.Session{DryRun: true}).Create(obj).Statement | ||
| 48 | + stmtSQL := stmt.SQL.String() | ||
| 49 | + sql := strings.Replace(stmtSQL, stmt.Table, info.TableName, -1) | ||
| 50 | + result := db.Exec(sql, stmt.Vars...) | ||
| 51 | + err = result.Error | ||
| 52 | + if err != nil { | ||
| 53 | + lxalilog.Errors(err, gameId) | ||
| 54 | + return | ||
| 55 | + } | ||
| 56 | + return | ||
| 57 | +} | ||
| 58 | + | ||
| 59 | +func Updates(obj IMysqlData, gameId string) (err error) { | ||
| 60 | + info := obj.MysqlInfo(gameId) | ||
| 61 | + db := info.DbMysql | ||
| 62 | + stmt := db.Session(&gorm.Session{DryRun: true}).Updates(obj).Statement | ||
| 63 | + stmtSQL := stmt.SQL.String() | ||
| 64 | + sql := strings.Replace(stmtSQL, stmt.Table, info.TableName, -1) | ||
| 65 | + result := db.Exec(sql, stmt.Vars...) | ||
| 66 | + err = result.Error | ||
| 67 | + if err != nil { | ||
| 68 | + lxalilog.Errors(err, gameId) | ||
| 69 | + return | ||
| 70 | + } | ||
| 71 | + return | ||
| 72 | +} | ||
| 73 | + | ||
| 74 | +func Save(obj IMysqlData, gameId string) (err error) { | ||
| 75 | + info := obj.MysqlInfo(gameId) | ||
| 76 | + db := info.DbMysql | ||
| 77 | + stmt := db.Session(&gorm.Session{DryRun: true}).Save(obj).Statement | ||
| 78 | + stmtSQL := stmt.SQL.String() | ||
| 79 | + sql := strings.Replace(stmtSQL, stmt.Table, info.TableName, -1) | ||
| 80 | + result := db.Exec(sql, stmt.Vars...) | ||
| 81 | + err = result.Error | ||
| 82 | + if err != nil { | ||
| 83 | + lxalilog.Errors(err, gameId) | ||
| 84 | + return | ||
| 85 | + } | ||
| 86 | + return | ||
| 87 | +} | ||
| 88 | + | ||
| 89 | +func First(obj IMysqlData, gameId string) (has bool, err error) { | ||
| 90 | + info := obj.MysqlInfo(gameId) | ||
| 91 | + db := info.DbMysql | ||
| 92 | + stmt := db.Session(&gorm.Session{DryRun: true}).First(obj).Statement | ||
| 93 | + stmtSQL := stmt.SQL.String() | ||
| 94 | + sql := strings.Replace(stmtSQL, stmt.Table, info.TableName, -1) | ||
| 95 | + result := db.Raw(sql, stmt.Vars...).Scan(obj) | ||
| 96 | + has = result.RowsAffected != 0 | ||
| 97 | + err = result.Error | ||
| 98 | + if err != nil { | ||
| 99 | + lxalilog.Errors(err, gameId) | ||
| 100 | + return | ||
| 101 | + } | ||
| 102 | + return | ||
| 103 | +} | ||
| 104 | + | ||
| 105 | +func Find(obj IMysqlData, gameId string) (has bool, err error) { | ||
| 106 | + info := obj.MysqlInfo(gameId) | ||
| 107 | + db := info.DbMysql | ||
| 108 | + stmt := db.Session(&gorm.Session{DryRun: true}).Find(obj).Statement | ||
| 109 | + stmtSQL := stmt.SQL.String() | ||
| 110 | + sql := strings.Replace(stmtSQL, stmt.Table, info.TableName, -1) | ||
| 111 | + result := db.Raw(sql, stmt.Vars...).Scan(obj) | ||
| 112 | + has = result.RowsAffected != 0 | ||
| 113 | + err = result.Error | ||
| 114 | + if err != nil { | ||
| 115 | + lxalilog.Errors(err, gameId) | ||
| 116 | + return | ||
| 117 | + } | ||
| 118 | + return | ||
| 119 | +} |
common/svmysql/interface.go
| 1 | package svmysql | 1 | package svmysql |
| 2 | + | ||
| 3 | +import "gorm.io/gorm" | ||
| 4 | + | ||
| 5 | +// MysqlInfo mysql存储信息 | ||
| 6 | +type MysqlInfo struct { | ||
| 7 | + DbMysql *gorm.DB | ||
| 8 | + TableName string | ||
| 9 | +} | ||
| 10 | + | ||
| 11 | +// IMysqlData mysql存储对象 | ||
| 12 | +type IMysqlData interface { | ||
| 13 | + // MysqlInfo mysql存储信息 | ||
| 14 | + MysqlInfo(gameId string) *MysqlInfo | ||
| 15 | +} |
common/svredis/index.go
| @@ -35,12 +35,12 @@ func Init() bool { | @@ -35,12 +35,12 @@ func Init() bool { | ||
| 35 | return true | 35 | return true |
| 36 | } | 36 | } |
| 37 | 37 | ||
| 38 | -func SaveData(gameId string, obj IRedisInfo) { | 38 | +func SaveData(gameId string, obj IRedisData) { |
| 39 | info := obj.RedisInfo(gameId) | 39 | info := obj.RedisInfo(gameId) |
| 40 | _ = zredis.SetEx(zredis.GetConn(), info.CacheKey, zjson.Str(obj), info.CacheTime) | 40 | _ = zredis.SetEx(zredis.GetConn(), info.CacheKey, zjson.Str(obj), info.CacheTime) |
| 41 | } | 41 | } |
| 42 | 42 | ||
| 43 | -func LoadData[T IRedisInfo](gameId string, obj T) (has bool) { | 43 | +func LoadData[T IRedisData](gameId string, obj T) (has bool) { |
| 44 | has = true | 44 | has = true |
| 45 | info := obj.RedisInfo(gameId) | 45 | info := obj.RedisInfo(gameId) |
| 46 | text, err := zredis.Get(zredis.GetConn(), info.CacheKey) | 46 | text, err := zredis.Get(zredis.GetConn(), info.CacheKey) |
common/svredis/interface.go
| @@ -6,8 +6,8 @@ type RedisInfo struct { | @@ -6,8 +6,8 @@ type RedisInfo struct { | ||
| 6 | CacheTime int | 6 | CacheTime int |
| 7 | } | 7 | } |
| 8 | 8 | ||
| 9 | -// IRedisInfo redis存储信息 | ||
| 10 | -type IRedisInfo interface { | 9 | +// IRedisData redis存储对象 |
| 10 | +type IRedisData interface { | ||
| 11 | // RedisInfo redis存储信息 | 11 | // RedisInfo redis存储信息 |
| 12 | RedisInfo(gameId string) *RedisInfo | 12 | RedisInfo(gameId string) *RedisInfo |
| 13 | } | 13 | } |
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/svmysql" | ||
| 6 | "apigame/common/svredis" | 7 | "apigame/common/svredis" |
| 7 | "apigame/util/utdto" | 8 | "apigame/util/utdto" |
| 8 | "fmt" | 9 | "fmt" |
| @@ -36,3 +37,11 @@ func (c *ApiGameConfig) RedisInfo(gameId string) *svredis.RedisInfo { | @@ -36,3 +37,11 @@ func (c *ApiGameConfig) RedisInfo(gameId string) *svredis.RedisInfo { | ||
| 36 | CacheTime: 300, | 37 | CacheTime: 300, |
| 37 | } | 38 | } |
| 38 | } | 39 | } |
| 40 | + | ||
| 41 | +func (c *ApiGameConfig) MysqlInfo(gameId string) *svmysql.MysqlInfo { | ||
| 42 | + tableName := "s_game_config" | ||
| 43 | + return &svmysql.MysqlInfo{ | ||
| 44 | + DbMysql: svconst.DbApi.Where("gameid = ?", gameId), | ||
| 45 | + TableName: tableName, | ||
| 46 | + } | ||
| 47 | +} |
configs/registry.go
| 1 | package configs | 1 | package configs |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | + "apigame/common/svmysql" | ||
| 4 | "apigame/common/svredis" | 5 | "apigame/common/svredis" |
| 5 | - "apigame/util/utdto" | ||
| 6 | - "apigame/util/util-lx/lxalilog" | ||
| 7 | "fmt" | 6 | "fmt" |
| 8 | ) | 7 | ) |
| 9 | 8 | ||
| @@ -15,14 +14,19 @@ func GetApiGame(gameId string) (conf *ApiGameConfig, err error) { | @@ -15,14 +14,19 @@ func GetApiGame(gameId string) (conf *ApiGameConfig, err error) { | ||
| 15 | fmt.Println("dwjw GetApiGame use cache") | 14 | fmt.Println("dwjw GetApiGame use cache") |
| 16 | return | 15 | return |
| 17 | } | 16 | } |
| 18 | - rule := conf.GetRule(gameId) | ||
| 19 | - result := utdto.First(rule.DbMysql, conf, rule.TableName) | ||
| 20 | - has = result.RowsAffected != 0 | ||
| 21 | - err = result.Error | 17 | + has, err = svmysql.First(conf, gameId) |
| 22 | if err != nil { | 18 | if err != nil { |
| 23 | - lxalilog.Errors(err, gameId) | ||
| 24 | return | 19 | return |
| 25 | } | 20 | } |
| 21 | + | ||
| 22 | + //rule := conf.GetRule(gameId) | ||
| 23 | + //result := utdto.First(rule.DbMysql, conf, rule.TableName) | ||
| 24 | + //has = result.RowsAffected != 0 | ||
| 25 | + //err = result.Error | ||
| 26 | + //if err != nil { | ||
| 27 | + // lxalilog.Errors(err, gameId) | ||
| 28 | + // return | ||
| 29 | + //} | ||
| 26 | fmt.Println("dwjw GetApiGame save cache") | 30 | fmt.Println("dwjw GetApiGame save cache") |
| 27 | svredis.SaveData(gameId, conf) | 31 | svredis.SaveData(gameId, conf) |
| 28 | 32 |