This commit is contained in:
hahwu 2025-12-24 11:50:23 +08:00
parent 68c8cfe620
commit 7e58fd3f4b
5 changed files with 61 additions and 7 deletions

View File

@ -164,6 +164,17 @@ func RedisGetKey(key string) (string, error) {
return val, nil
}
func RedisGetKeyBytes(key string) ([]byte, error) {
if RdbRead == nil {
return nil, nil
}
val, err := RdbRead.Get(ctx, key).Bytes()
if err != nil {
return nil, err
}
return val, nil
}
func RedisDelKey(key string) {
if RdbWrite == nil {
log.Debug("redis write client is nil")

View File

@ -986,7 +986,7 @@ func (p *Player) UpdateUserInfo() {
//TODO 存储到redis 在新版本中将优化成gob进行压缩
value, _ := json.Marshal(simple)
IdStr := strconv.Itoa(int(p.M_DwUin))
IdStr := GoUtil.String(p.M_DwUin)
go db.RedisSetKeyBytes(IdStr, value, 0)
}

View File

@ -1,6 +1,8 @@
package game
import (
"fmt"
"server/db"
"server/game/mod/msg"
GoUtil "server/game_util"
"server/pkg/github.com/name5566/leaf/log"
@ -17,15 +19,27 @@ const (
)
func (p *Player) GetVarData(key string) interface{} {
data, err := GetUserData(p.M_DwUin, key)
cache := &VarExpireData{}
err := LoadCacheVarData(key, cache)
if err != nil {
log.Error("GetVarData err : %s", err)
return nil
}
if data == nil {
return cache.D
}
func (p *Player) GetUserVarData(key string) interface{} {
cache := map[string]*VarExpireData{}
err := LoadCacheVarData(GoUtil.GetVarKey(int(p.M_DwUin)), cache)
if err != nil {
log.Error("GetVarData err : %s", err)
return nil
}
return data.Extra
data, ok := cache[key]
if !ok {
return nil
}
return data.D
}
func GetServerVarData(key string) interface{} {
@ -100,7 +114,7 @@ func (p *Player) SubPlayroomChip(PlayerId int) {
}
func (p *Player) GetPlayroomUpvote() int {
data := p.GetVarData(VAR_PLAYROOM_UPVOTE)
data := p.GetUserVarData(VAR_PLAYROOM_UPVOTE)
if data == nil {
return 0
}
@ -108,7 +122,7 @@ func (p *Player) GetPlayroomUpvote() int {
}
func (p *Player) GetPlayroomChip() int {
data := p.GetVarData(VAR_PLAYROOM_CHIP)
data := p.GetUserVarData(VAR_PLAYROOM_CHIP)
if data == nil {
return 0
}
@ -120,7 +134,7 @@ func (p *Player) SetPlayroomKiss(Kiss int) {
}
func (p *Player) GetPlayroomKiss() int {
data := p.GetVarData(VAR_PLAYROOM_KISS)
data := p.GetUserVarData(VAR_PLAYROOM_KISS)
if data == nil {
return 0
}
@ -146,3 +160,23 @@ func (p *Player) GetCatnipPartner(Uid int) []int {
}
return GoUtil.IntSlice(data.Extra)
}
func SaveCacheVarData(key string, value interface{}) {
data, err := GoUtil.GobMarshal(value)
if err != nil {
log.Error("SaveCacheVarData GobMarshal err : %s", err)
return
}
db.RedisSetKeyBytes(key, data, 0)
}
func LoadCacheVarData(key string, value interface{}) error {
data, err := db.RedisGetKeyBytes(key)
if err != nil {
return err
}
if data == nil {
return fmt.Errorf("no data")
}
return GoUtil.GobUnmarshal(data, value)
}

View File

@ -140,6 +140,7 @@ func (f *VarMgr) SetUserVar(uid int, key string, value interface{}) {
}
ved.D = value
varData[key] = ved
SaveCacheVarData(GoUtil.GetVarKey(uid), varData)
}
func (f *VarMgr) GetUserVar(uid int, key string) *VarExpireData {
@ -160,6 +161,7 @@ func (f *VarMgr) SetVar(key string, value interface{}) {
f.getData().NewVar[key] = &VarExpireData{
D: value,
}
SaveCacheVarData(key, f.getData().NewVar[key])
}
func (f *VarMgr) GetVar(key string) *VarExpireData {
@ -217,6 +219,7 @@ func SetVarDataHandler(m *msg.Msg) (interface{}, error) {
ved.T = m.End
}
data.Var[v.Key] = ved
SaveCacheVarData(v.Key, ved)
}
return nil, nil
}
@ -277,6 +280,8 @@ func SetUserVarDataHandler(m *msg.Msg) (interface{}, error) {
}
data.Var[v.Key] = ved
}
// 保存到缓存中
SaveCacheVarData(GoUtil.GetVarKey(m.From), data.NewUseVar[m.From])
return nil, nil
}

View File

@ -531,3 +531,7 @@ func GetISOCodeByIP(ip string) (string, error) {
}
return GetISOCodeByCountry(country)
}
func GetVarKey(Uid int) string {
return fmt.Sprintf("var_%d", Uid)
}