index.go 1.62 KB
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
}