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