key 优化减少查询次数

This commit is contained in:
meixiongfeng 2023-01-10 14:11:27 +08:00
parent ebb47ea8d9
commit 5bcccf25d2
11 changed files with 124 additions and 105 deletions

View File

@ -88,5 +88,17 @@
"open": true,
"routrules": "~/worker",
"describe": "维京排行榜数据"
},
{
"msgid": "hunting.ranklist",
"open": true,
"routrules": "~/worker",
"describe": "狩猎排行榜数据"
},
{
"msgid": "enchant.ranklist",
"open": true,
"routrules": "~/worker",
"describe": "附魔副本排行榜数据"
}
]

View File

@ -41,7 +41,7 @@ func (this *apiComp) Challenge(session comm.IUserSession, req *pb.EnchantChallen
}
if code = this.module.CheckRes(session, cfgData[0].PsConsume); code != pb.ErrorCode_Success {
code = pb.ErrorCode_VikingMaxChallengeCount
code = pb.ErrorCode_ItemsNoEnough
return
}
_, ok := enchant.Boss[req.BossType]

View File

@ -58,9 +58,14 @@ func (this *apiComp) ChallengeOver(session comm.IUserSession, req *pb.EnchantCha
if code = this.module.ConsumeRes(session, cfgEnchant[0].PsConsume, true); code != pb.ErrorCode_Success {
return
}
if bWin {
this.module.CheckRank(session.GetUserId(), req.BossType, enchant, req.Report, req.Score)
key := req.BossType
if enchant.BossTime[key] > req.Report.Costtime || enchant.BossTime[key] == 0 {
enchant.BossTime[key] = req.Report.Costtime
mapData["bossTime"] = enchant.BossTime // 更新时间
userinfo := this.module.ModuleUser.GetUser(session.GetUserId())
this.module.CheckRank(session.GetUserId(), req.BossType, req.Report, userinfo, req.Score)
}
//this.module.CheckRank(session.GetUserId(), req.BossType, enchant, req.Report, req.Score)
enchant.Boss[req.BossType] = req.Score // 获得的积分
// 发放通关随机奖励
for _, v := range cfgEnchant {

View File

@ -17,11 +17,6 @@ func (this *apiComp) GetListCheck(session comm.IUserSession, req *pb.EnchantGetL
func (this *apiComp) GetList(session comm.IUserSession, req *pb.EnchantGetListReq) (code pb.ErrorCode, data proto.Message) {
code = this.GetListCheck(session, req)
if code != pb.ErrorCode_Success {
return // 参数校验失败直接返回
}
// 刷新挑战卷
if code = this.module.ModuleItems.RecoverTicket(session); code != pb.ErrorCode_Success {
return
@ -39,9 +34,7 @@ func (this *apiComp) GetList(session comm.IUserSession, req *pb.EnchantGetListRe
for k := range _cfg {
list.BossTime[k] = 0
}
this.module.modelEnchant.Add(session.GetUserId(), list)
}
if session.GetUserId() != "" { // 恢复时间
if userexpand, err := this.module.ModuleUser.GetUserExpand(session.GetUserId()); err == nil {

View File

@ -5,6 +5,7 @@ import (
"go_dreamfactory/comm"
"go_dreamfactory/lego/sys/redis/pipe"
"go_dreamfactory/pb"
"go_dreamfactory/sys/db"
"strconv"
"github.com/go-redis/redis/v8"
@ -26,11 +27,13 @@ func (this *apiComp) RankList(session comm.IUserSession, req *pb.EnchantRankList
if code != pb.ErrorCode_Success {
return // 参数校验失败直接返回
}
conn, _ := db.Local()
dbModel := db.NewDBModel(comm.TableEnchantRank, 0, conn)
if !req.Friend {
var (
pipe *pipe.RedisPipe = this.module.modelEnchant.Redis.RedisPipe(context.TODO())
)
rd = pipe.ZRange("enchantRank"+strconv.Itoa(int(req.BoosType)), 0, -1)
rd = pipe.ZRange("enchantRank"+strconv.Itoa(int(req.BoosType)), 0, comm.MaxRankList)
if _, err := pipe.Exec(); err != nil {
this.module.Errorln(err)
@ -39,7 +42,8 @@ func (this *apiComp) RankList(session comm.IUserSession, req *pb.EnchantRankList
_dataList := rd.Val()
for _, v := range _dataList {
result := &pb.DBEnchantRank{}
if err := this.module.modulerank.GetListObj(session.GetUserId(), v, result); err == nil {
if err := dbModel.Redis.HGetAll(v, result); err == nil {
szRank = append(szRank, result)
}
}

View File

@ -1,14 +1,16 @@
package enchant
import (
"context"
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/redis/pipe"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"go_dreamfactory/sys/db"
"strconv"
"time"
"github.com/go-redis/redis/v8"
"go.mongodb.org/mongo-driver/bson/primitive"
)
@ -76,79 +78,80 @@ func (this *Enchant) CheckUserBaseEnchantInfo(uid string) (data []*pb.DBEnchantR
}
return
}
func (this *Enchant) CheckRank(uid string, boosID int32, Enchant *pb.DBEnchant, report *pb.BattleReport, score int64) {
func (this *Enchant) CheckRank(uid string, boosID int32, report *pb.BattleReport, userinfo *pb.DBUser, score int64) {
conn_, _ := db.Cross() // 获取跨服数据库对象
model := db.NewDBModel(comm.TableEnchantRank, 0, conn_)
costTime := report.Costtime
if Enchant.BossTime[boosID] > costTime || Enchant.BossTime[boosID] == 0 { // 刷新记录
Enchant.BossTime[boosID] = costTime
szLine := make([]*pb.LineUp, 0)
Leadpos := 0
if report != nil && report.Info != nil && len(report.Info.Redflist) > 0 {
costTime = report.Costtime
Leadpos = int(report.Info.Redflist[0].Leadpos)
for _, v := range report.Info.Redflist[0].Team {
if v != nil {
szLine = append(szLine, &pb.LineUp{
Cid: v.HeroID,
Star: v.Star,
Lv: v.Lv,
})
}
szLine := make([]*pb.LineUp, 5)
Leadpos := 0
if report != nil && report.Info != nil && len(report.Info.Redflist) > 0 {
costTime = report.Costtime
Leadpos = int(report.Info.Redflist[0].Leadpos)
for _, v := range report.Info.Redflist[0].Team {
if v != nil {
szLine = append(szLine, &pb.LineUp{
Cid: v.HeroID,
Star: v.Star,
Lv: v.Lv,
})
}
}
// 写入排行榜
objID := ""
bFind := false
ranks := this.modulerank.getEnchantRankList(uid)
for _, v := range ranks {
if v.Bosstype == boosID {
mapRankData := make(map[string]interface{}, 0)
mapRankData["bosstype"] = boosID
mapRankData["Leadpos"] = Leadpos
mapRankData["line"] = szLine
mapRankData["costTime"] = costTime
mapRankData["score"] = score
conn_, _ := db.Cross()
dbModel := db.NewDBModel(comm.TableEnchantRank, time.Hour, conn_)
dbModel.ChangeList(uid, v.Id, mapRankData)
objID = v.Id
bFind = true
break
}
}
if !bFind {
userinfo := this.ModuleUser.GetUser(uid)
new := &pb.DBEnchantRank{
Id: primitive.NewObjectID().Hex(),
Uid: uid,
Bosstype: boosID,
Nickname: userinfo.Name,
Lv: userinfo.Lv,
Leadpos: int32(Leadpos),
Line: szLine,
CostTime: costTime,
Score: score,
}
objID = new.Id
conn_, _ := db.Cross()
dbModel := db.NewDBModel(comm.TableEnchantRank, time.Hour, conn_)
dbModel.AddList(uid, new.Id, new)
}
this.modulerank.SetRankListData("EnchantRank"+strconv.Itoa(int(boosID)), score, objID)
}
}
// 写入排行榜
objID := ""
bFind := false
ranks := this.modulerank.getEnchantRankList(uid)
for _, v := range ranks {
if v.Bosstype == boosID {
mapRankData := make(map[string]interface{}, 0)
mapRankData["bosstype"] = boosID
mapRankData["Leadpos"] = Leadpos
mapRankData["line"] = szLine
mapRankData["costTime"] = costTime
mapRankData["score"] = score
//红点查询
func (this *Enchant) Reddot(session comm.IUserSession, rid ...comm.ReddotType) (reddot map[comm.ReddotType]bool) {
reddot = make(map[comm.ReddotType]bool)
for _, v := range rid {
switch v {
case comm.Reddot32:
reddot[comm.Reddot32] = this.modelEnchant.checkReddot32(session.GetUserId())
model.ChangeList(uid, v.Id, mapRankData)
objID = v.Id
bFind = true
break
}
}
return
if !bFind {
userinfo := this.ModuleUser.GetUser(uid)
new := &pb.DBEnchantRank{
Id: primitive.NewObjectID().Hex(),
Uid: uid,
Bosstype: boosID,
Nickname: userinfo.Name,
Lv: userinfo.Lv,
Leadpos: int32(Leadpos),
Line: szLine,
CostTime: costTime,
Score: score,
}
objID = new.Id
model.AddList(uid, new.Id, new)
}
var (
pipe *pipe.RedisPipe = conn_.Redis.RedisPipe(context.TODO())
menbers *redis.Z
tableName string
)
tableName = "enchantRank" + strconv.Itoa(int(boosID))
strKey := "enchantRank:" + uid + "-" + objID
menbers = &redis.Z{Score: float64(costTime), Member: strKey}
if cmd := pipe.ZAdd(tableName, menbers); cmd != nil {
dock, err1 := cmd.Result()
if err1 != nil {
this.Errorln(dock, err1)
}
}
if _, err := pipe.Exec(); err != nil {
this.Errorln(err)
return
}
}

View File

@ -5,6 +5,7 @@ import (
"go_dreamfactory/comm"
"go_dreamfactory/lego/sys/redis/pipe"
"go_dreamfactory/pb"
"go_dreamfactory/sys/db"
"strconv"
"github.com/go-redis/redis/v8"
@ -26,11 +27,13 @@ func (this *apiComp) RankList(session comm.IUserSession, req *pb.HuntingRankList
if code != pb.ErrorCode_Success {
return // 参数校验失败直接返回
}
conn, _ := db.Local()
dbModel := db.NewDBModel(comm.TableHuntingRank, 0, conn)
if !req.Friend {
var (
pipe *pipe.RedisPipe = this.module.modelHunting.Redis.RedisPipe(context.TODO())
pipe *pipe.RedisPipe = conn.Redis.RedisPipe(context.TODO())
)
rd = pipe.ZRange("huntingRank"+strconv.Itoa(int(req.BoosType)), 0, -1)
rd = pipe.ZRevRange("huntingRank"+strconv.Itoa(int(req.BoosType)), 0, comm.MaxRankList)
if _, err := pipe.Exec(); err != nil {
this.module.Errorln(err)
@ -39,11 +42,11 @@ func (this *apiComp) RankList(session comm.IUserSession, req *pb.HuntingRankList
_dataList := rd.Val()
for _, v := range _dataList {
result := &pb.DBHuntingRank{}
if err := this.module.modulerank.GetListObj(session.GetUserId(), v, result); err == nil {
if err := dbModel.Redis.HGetAll(v, result); err == nil {
szRank = append(szRank, result)
}
}
} else {
uids := this.friend.GetFriendList(session.GetUserId())
for _, id := range uids {

View File

@ -147,7 +147,8 @@ func (this *Hunting) CheckRank(uid string, boosID int32, difficulty int32, repor
)
score = int64(difficulty)<<31 + int64(math.MaxInt32-costTime)
tableName = "huntingRank" + strconv.Itoa(int(boosID))
menbers = &redis.Z{Score: float64(score), Member: objID}
strKey := "huntingRank:" + uid + "-" + objID
menbers = &redis.Z{Score: float64(score), Member: strKey}
if cmd := pipe.ZAdd(tableName, menbers); cmd != nil {

View File

@ -197,7 +197,7 @@ func (this *SeasonPagoda) TimerSeasonStar() {
func (this *SeasonPagoda) DbTest() {
conn, _ := db.Cross()
model1 := db.NewDBModel(comm.TableVikingRank, time.Hour, conn)
model1 := db.NewDBModel(comm.TableVikingRank, 0, conn)
model1.Redis.Delete("vikingRank2")
_d, err := model1.Redis.Keys("vikingrank:*")
if err == nil {
@ -221,8 +221,8 @@ func (this *SeasonPagoda) DbTest() {
Line: make([]*pb.LineUp, 5),
CostTime: 12000 + int32(i),
}
//model1.AddList(new.Uid, new.Id, new)
model1.Add(new.Uid, new)
model1.AddList(new.Uid, new.Id, new)
//model1.Add(new.Uid, new)
var (
pipe *pipe.RedisPipe = conn.Redis.RedisPipe(context.TODO())
menbers *redis.Z
@ -232,7 +232,7 @@ func (this *SeasonPagoda) DbTest() {
score = int64(i)<<31 + int64(math.MaxInt32-new.CostTime)
tableName = "vikingRank" + strconv.Itoa(int(new.Bosstype))
//vikingrank:mmmxxx1-63bbb137b96efbd321222ce7
strKey := new.Uid //"vikingrank:" + new.Uid + "-" + new.Id // 自定义key
strKey := "vikingrank:" + new.Uid + "-" + new.Id // 自定义key new.Uid //
menbers = &redis.Z{Score: float64(score), Member: strKey}
if cmd := pipe.ZAdd(tableName, menbers); cmd != nil {
@ -262,19 +262,19 @@ func (this *SeasonPagoda) DbTest() {
_dataList := rd.Val()
for _, v := range _dataList {
result := &pb.DBVikingRank{}
if err := model1.Get(v, result); err == nil {
//for _, v2 := range result {
szRank = append(szRank, result)
//}
if err := model1.Redis.HGetAll(v, result); err == nil {
szRank = append(szRank, result)
}
// result := make([]*pb.DBVikingRank, 0)
// if err := model1.GetList(v, &result); err == nil {
// for _, v2 := range result {
// szRank = append(szRank, v2)
// }
// }
}
//this.module.Debugf("%v", szRank)
this.module.Debugf("%v", szRank)
}
}

View File

@ -30,11 +30,12 @@ func (this *apiComp) RankList(session comm.IUserSession, req *pb.VikingRankListR
return // 参数校验失败直接返回
}
conn, _ := db.Local()
dbModel := db.NewDBModel(comm.TableVikingRank, 0, conn)
if !req.Friend {
var (
pipe *pipe.RedisPipe = conn.Redis.RedisPipe(context.TODO())
)
rd = pipe.ZRange("vikingRank"+strconv.Itoa(int(req.BoosType)), 0, -1)
rd = pipe.ZRevRange("vikingRank"+strconv.Itoa(int(req.BoosType)), 0, comm.MaxRankList)
if _, err := pipe.Exec(); err != nil {
this.module.Errorln(err)
@ -42,13 +43,10 @@ func (this *apiComp) RankList(session comm.IUserSession, req *pb.VikingRankListR
}
_dataList := rd.Val()
for _, v := range _dataList {
result := make([]*pb.DBVikingRank, 0)
if err := this.module.modulerank.GetList(v, &result); err == nil {
for _, v2 := range result {
if v2.Bosstype == req.BoosType {
szRank = append(szRank, v2)
}
}
result := &pb.DBVikingRank{}
if err := dbModel.Redis.HGetAll(v, result); err == nil {
szRank = append(szRank, result)
}
}

View File

@ -16,7 +16,6 @@ import (
"go_dreamfactory/sys/db"
"math"
"strconv"
"time"
"github.com/go-redis/redis/v8"
"go.mongodb.org/mongo-driver/bson/primitive"
@ -90,7 +89,7 @@ func (this *Viking) CheckUserBaseVikingInfo(uid string) (data []*pb.DBVikingRank
func (this *Viking) CheckRank(uid string, boosID int32, difficulty int32, report *pb.BattleReport, userinfo *pb.DBUser) {
conn_, _ := db.Cross() // 获取跨服数据库对象
model := db.NewDBModel(comm.TableVikingRank, time.Hour, conn_)
model := db.NewDBModel(comm.TableVikingRank, 0, conn_)
costTime := report.Costtime
szLine := make([]*pb.LineUp, 5)
@ -154,7 +153,8 @@ func (this *Viking) CheckRank(uid string, boosID int32, difficulty int32, report
)
score = int64(difficulty)<<31 + int64(math.MaxInt32-costTime)
tableName = "vikingRank" + strconv.Itoa(int(boosID))
menbers = &redis.Z{Score: float64(score), Member: objID}
strKey := "vikingrank:" + uid + "-" + objID // 自定义key
menbers = &redis.Z{Score: float64(score), Member: strKey}
if cmd := pipe.ZAdd(tableName, menbers); cmd != nil {