index.go
1.62 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
package confbase
import (
"apigame/service-common/svmysql"
"apigame/util/util-lx/lxalilog"
"apigame/util/util-lx/lxtime"
"apigame/util/zjson"
"apigame/util/zredis"
"fmt"
)
func SaveCache[T IConfData](gameId string, obj T) {
info := obj.ConfInfo(gameId)
_ = zredis.SetEx(zredis.GetConn(), 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(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
}
func LoadData[T1 IConfData, T2 IConfRawData](gameId string, id 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), id).First(confRaw)
has = result.RowsAffected != 0
err := result.Error
if err != nil {
lxalilog.Errors(err, "confbase.LoadData error", gameId)
return
}
if !has {
return
}
obj.Decode(gameId, confRaw)
return
}
func FindDuringTime(obj svmysql.IMysqlData, gameId string) (has bool) {
info := obj.MysqlInfo(gameId)
db := info.DbMysql
timeNow := lxtime.NowUninx()
result := db.Table(info.TableName).Where("start_time <= ? AND end_time >= ?", timeNow, timeNow).First(obj)
has = result.RowsAffected != 0
return
}
func GetConfig[T1 IConfData, T2 IConfRawData](gameId string, id any, obj T1) (has bool) {
has = LoadCache(gameId, obj)
if has {
return
}
has = LoadData[T1, T2](gameId, id, obj)
if !has {
return
}
SaveCache(gameId, obj)
return
}