index.go
1.25 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
package svredis
import (
"apigame/common/svconst"
"apigame/util/utstring"
"apigame/util/zjson"
"apigame/util/zredis"
"fmt"
"github.com/astaxie/beego"
"github.com/mitchellh/mapstructure"
)
type RedisConfig struct {
Host string `json:"host"`
Port string `json:"port"`
Pwd string `json:"pwd"`
Db string `json:"Db"`
Prefix string `json:"prefix"`
}
func Init() bool {
//初始化Redis
confText, _ := beego.AppConfig.GetSection("redis")
var conf RedisConfig
err := mapstructure.Decode(confText, &conf)
if err != nil {
fmt.Println("svredis.Init mapstructure.Decode Error::" + err.Error())
}
zredis.Init(conf.Host+":"+conf.Port, conf.Pwd, utstring.StringToInt(conf.Db))
if !zredis.Check() {
return false
}
fmt.Println(svconst.AppName + " redis init success")
return true
}
func SaveData(gameId string, obj IRedisInfo) {
info := obj.RedisInfo(gameId)
_ = zredis.SetEx(zredis.GetConn(), info.CacheKey, zjson.Str(obj), info.CacheTime)
}
func LoadData[T IRedisInfo](gameId string, obj T) (has bool) {
has = true
info := obj.RedisInfo(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
}