上传月之秘境分布式锁

This commit is contained in:
liwei1dao 2022-10-12 17:39:29 +08:00
parent a2858fda57
commit 8ce067043c
4 changed files with 29 additions and 1 deletions

View File

@ -55,6 +55,11 @@ func (this *MCompModel) UpdateModelLogs(table string, uID string, where bson.M,
return this.DBModel.UpdateModelLogs(table, uID, where, target)
}
//创建分布式锁
func (this *MCompModel) NewRedisMutex(key string, outtime int) (result *redis.RedisMutex, err error) {
return this.DBModel.NewRedisMutex(key, outtime)
}
//添加新的数据
func (this *MCompModel) Add(uid string, data interface{}, opt ...db.DBOption) (err error) {
return this.DBModel.Add(uid, data)

View File

@ -2,6 +2,7 @@ package moonfantasy
import (
"go_dreamfactory/comm"
"go_dreamfactory/lego/sys/redis"
"go_dreamfactory/pb"
cfg "go_dreamfactory/sys/configure/structs"
"time"
@ -19,6 +20,7 @@ func (this *apiComp) Ask(session comm.IUserSession, req *pb.MoonfantasyAskReq) (
var (
globalconf *cfg.GameGlobalData
mdata *pb.DBMoonFantasy
lock *redis.RedisMutex
umfantasy *pb.DBUserMFantasy
user *pb.DBUser
cd pb.ErrorCode
@ -30,7 +32,17 @@ func (this *apiComp) Ask(session comm.IUserSession, req *pb.MoonfantasyAskReq) (
if cd = this.AskCheck(session, req); cd != pb.ErrorCode_Success {
return
}
//月之秘境需要加分布式锁 防止多人同时操作
if lock, err = this.module.modelDream.newDreamLock(req.Mid); err != nil {
cd = pb.ErrorCode_DBError
return
}
defer lock.Unlock()
if err = lock.Lock(); err != nil {
cd = pb.ErrorCode_DBError
this.module.Errorln(err)
return
}
if mdata, err = this.module.modelDream.querymfantasy(req.Mid); err != nil {
cd = pb.ErrorCode_DBError
return

View File

@ -1,9 +1,11 @@
package moonfantasy
import (
"fmt"
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/mgo"
"go_dreamfactory/lego/sys/redis"
"go_dreamfactory/lego/sys/timewheel"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
@ -33,6 +35,9 @@ func (this *modelDreamComp) Init(service core.IService, module core.IModule, com
})
return
}
func (this *modelDreamComp) newDreamLock(mid string) (result *redis.RedisMutex, err error) {
return this.module.modelDream.NewRedisMutex(fmt.Sprintf("%s-%s_lock", this.TableName, mid), 5)
}
///添加月之秘境记录
func (this *modelDreamComp) querymfantasy(mid string) (result *pb.DBMoonFantasy, err error) {

View File

@ -149,6 +149,12 @@ func (this *DBModel) UpdateModelLogs(table string, uID string, where bson.M, tar
return err
}
///创建锁对象
func (this *DBModel) NewRedisMutex(key string, outtime int) (result *lgredis.RedisMutex, err error) {
result, err = this.Redis.NewRedisMutex(key, lgredis.SetExpiry(outtime))
return
}
//添加新的数据
func (this *DBModel) Add(uid string, data interface{}, opt ...DBOption) (err error) {
//defer log.Debug("DBModel Add", log.Field{Key: "TableName", Value: this.TableName}, log.Field{Key: "uid", Value: uid}, log.Field{Key: "data", Value: data})