Commit 5e54fb0e3927a7b8d83006802f711922bc0abfed

Authored by 王家文
1 parent c3dc4be0
Exists in master and in 1 other branch dev-wjw

feat✨:房间排行活动:配置接口

configs/confbase/external.go 0 → 100644
@@ -0,0 +1,103 @@ @@ -0,0 +1,103 @@
  1 +package confbase
  2 +
  3 +import (
  4 + "apigame/util/util-lx/lxalilog"
  5 + "apigame/util/zjson"
  6 + "apigame/util/zredis"
  7 + "fmt"
  8 + "runtime/debug"
  9 +)
  10 +
  11 +func SaveCache[T IConfData](gameId string, obj T) {
  12 + info := obj.ConfInfo(gameId)
  13 + _ = zredis.SetEx(zredis.GetConn(), info.CacheKey, zjson.Str(obj), info.CacheTime)
  14 +}
  15 +
  16 +func LoadCache[T IConfData](gameId string, obj T) (has bool) {
  17 + has = true
  18 + info := obj.ConfInfo(gameId)
  19 + text, err := zredis.Get(zredis.GetConn(), info.CacheKey)
  20 + if err != nil {
  21 + has = false
  22 + return
  23 + }
  24 + err = zjson.Obj(text, &obj)
  25 + if err != nil {
  26 + fmt.Println(err)
  27 + has = false
  28 + return
  29 + }
  30 + return
  31 +}
  32 +
  33 +func LoadData[T1 IConfData, T2 IConfRawData](gameId string, confId any, obj T1) (has bool) {
  34 + confRaw := new(T2)
  35 + info := obj.ConfInfo(gameId)
  36 + db := info.DbMysql
  37 + result := db.Table(info.TableName).Where(fmt.Sprintf("%s = ?", info.KeyName), confId).First(confRaw)
  38 +
  39 + has = result.RowsAffected != 0
  40 + err := result.Error
  41 + if err != nil {
  42 + lxalilog.Errors(err, "confbase.LoadData error", gameId, confId)
  43 + fmt.Printf("%s", debug.Stack())
  44 + return
  45 + }
  46 + if !has {
  47 + return
  48 + }
  49 +
  50 + obj.Decode(gameId, confRaw)
  51 + return
  52 +}
  53 +
  54 +func FindDuringTime[T1 IConfData, T2 IConfRawData](obj T1, raw *T2, gameId string) (has bool) {
  55 + info := obj.ConfInfo(gameId)
  56 + db := info.DbMysql
  57 + result := db.Table(info.TableName).Where(info.CurrentQuery, info.CurrentArgs...).First(raw)
  58 + has = result.RowsAffected != 0
  59 + return
  60 +}
  61 +
  62 +func GetConfig[T1 IConfData, T2 IConfRawData](gameId string, confId any, obj T1) (has bool) {
  63 + has = LoadCache(gameId, obj)
  64 + if has {
  65 + return
  66 + }
  67 + has = LoadData[T1, T2](gameId, confId, obj)
  68 + if !has {
  69 + return
  70 + }
  71 + SaveCache(gameId, obj)
  72 + return
  73 +}
  74 +
  75 +// GetCurrent 获取 当前配置
  76 +func GetCurrent[T1 IConfData, T2 IConfRawData](gameId string, obj T1) bool {
  77 + has := false
  78 + info := obj.ConfInfo(gameId)
  79 + currentKey := info.CacheCurrent
  80 + currentId := zredis.GetString(zredis.GetConn(), currentKey)
  81 + confRaw := new(T2)
  82 + hasFind := false
  83 + if currentId == "" {
  84 + hasFind = FindDuringTime[T1, T2](obj, confRaw, gameId)
  85 + } else {
  86 + has = GetConfig[T1, T2](gameId, currentId, obj)
  87 + if has {
  88 + if obj.CheckCurrent() {
  89 + hasFind = FindDuringTime[T1, T2](obj, confRaw, gameId)
  90 + }
  91 + } else {
  92 + hasFind = FindDuringTime[T1, T2](obj, confRaw, gameId)
  93 + }
  94 + }
  95 + if hasFind {
  96 + obj.Decode(gameId, confRaw)
  97 + SaveCache(gameId, obj)
  98 + has = true
  99 + currentId = obj.GetUid()
  100 + _ = zredis.Set(zredis.GetConn(), currentKey, currentId)
  101 + }
  102 + return has
  103 +}
configs/confbase/index.go
@@ -1,103 +0,0 @@ @@ -1,103 +0,0 @@
1 -package confbase  
2 -  
3 -import (  
4 - "apigame/util/util-lx/lxalilog"  
5 - "apigame/util/zjson"  
6 - "apigame/util/zredis"  
7 - "fmt"  
8 - "runtime/debug"  
9 -)  
10 -  
11 -func SaveCache[T IConfData](gameId string, obj T) {  
12 - info := obj.ConfInfo(gameId)  
13 - _ = zredis.SetEx(zredis.GetConn(), info.CacheKey, zjson.Str(obj), info.CacheTime)  
14 -}  
15 -  
16 -func LoadCache[T IConfData](gameId string, obj T) (has bool) {  
17 - has = true  
18 - info := obj.ConfInfo(gameId)  
19 - text, err := zredis.Get(zredis.GetConn(), info.CacheKey)  
20 - if err != nil {  
21 - has = false  
22 - return  
23 - }  
24 - err = zjson.Obj(text, &obj)  
25 - if err != nil {  
26 - fmt.Println(err)  
27 - has = false  
28 - return  
29 - }  
30 - return  
31 -}  
32 -  
33 -func LoadData[T1 IConfData, T2 IConfRawData](gameId string, confId any, obj T1) (has bool) {  
34 - confRaw := new(T2)  
35 - info := obj.ConfInfo(gameId)  
36 - db := info.DbMysql  
37 - result := db.Table(info.TableName).Where(fmt.Sprintf("%s = ?", info.KeyName), confId).First(confRaw)  
38 -  
39 - has = result.RowsAffected != 0  
40 - err := result.Error  
41 - if err != nil {  
42 - lxalilog.Errors(err, "confbase.LoadData error", gameId, confId)  
43 - fmt.Printf("%s", debug.Stack())  
44 - return  
45 - }  
46 - if !has {  
47 - return  
48 - }  
49 -  
50 - obj.Decode(gameId, confRaw)  
51 - return  
52 -}  
53 -  
54 -func FindDuringTime[T1 IConfData, T2 IConfRawData](obj T1, raw *T2, gameId string) (has bool) {  
55 - info := obj.ConfInfo(gameId)  
56 - db := info.DbMysql  
57 - result := db.Table(info.TableName).Where(info.CurrentQuery, info.CurrentArgs...).First(raw)  
58 - has = result.RowsAffected != 0  
59 - return  
60 -}  
61 -  
62 -func GetConfig[T1 IConfData, T2 IConfRawData](gameId string, confId any, obj T1) (has bool) {  
63 - has = LoadCache(gameId, obj)  
64 - if has {  
65 - return  
66 - }  
67 - has = LoadData[T1, T2](gameId, confId, obj)  
68 - if !has {  
69 - return  
70 - }  
71 - SaveCache(gameId, obj)  
72 - return  
73 -}  
74 -  
75 -// GetCurrent 获取 当前配置  
76 -func GetCurrent[T1 IConfData, T2 IConfRawData](gameId string, obj T1) bool {  
77 - has := false  
78 - info := obj.ConfInfo(gameId)  
79 - currentKey := info.CacheCurrent  
80 - currentId := zredis.GetString(zredis.GetConn(), currentKey)  
81 - confRaw := new(T2)  
82 - hasFind := false  
83 - if currentId == "" {  
84 - hasFind = FindDuringTime[T1, T2](obj, confRaw, gameId)  
85 - } else {  
86 - has = GetConfig[T1, T2](gameId, currentId, obj)  
87 - if has {  
88 - if obj.CheckCurrent() {  
89 - hasFind = FindDuringTime[T1, T2](obj, confRaw, gameId)  
90 - }  
91 - } else {  
92 - hasFind = FindDuringTime[T1, T2](obj, confRaw, gameId)  
93 - }  
94 - }  
95 - if hasFind {  
96 - obj.Decode(gameId, confRaw)  
97 - SaveCache(gameId, obj)  
98 - has = true  
99 - currentId = obj.GetUid()  
100 - _ = zredis.Set(zredis.GetConn(), currentKey, currentId)  
101 - }  
102 - return has  
103 -}