diff --git a/common/cool/service.go b/common/cool/service.go index 325f4fc8..0ef3e466 100644 --- a/common/cool/service.go +++ b/common/cool/service.go @@ -7,6 +7,7 @@ import ( "github.com/gogf/gf/v2/database/gdb" "github.com/gogf/gf/v2/errors/gerror" "github.com/gogf/gf/v2/frame/g" + "github.com/gogf/gf/v2/os/gcache" "github.com/gogf/gf/v2/util/gconv" ) @@ -30,6 +31,7 @@ type Service struct { InfoIgnoreProperty string // Info时忽略的字段,多个字段用逗号隔开 UniqueKey g.MapStrStr // 唯一键 key:字段名 value:错误信息 NotNullKey g.MapStrStr // 非空键 key:字段名 value:错误信息 + Cache *gcache.Cache } // List/Add接口条件配置 @@ -413,6 +415,10 @@ func (s *Service) ModifyBefore(ctx context.Context, method string, param g.MapSt // ModifyAfter 新增|删除|修改后的操作 func (s *Service) ModifyAfter(ctx context.Context, method string, param g.MapStrAny) (err error) { + if s.Cache != nil { + s.Cache.Clear(context.Background()) + + } return } diff --git a/modules/blazing/service/effect.go b/modules/blazing/service/effect.go index 214f6729..97753f65 100644 --- a/modules/blazing/service/effect.go +++ b/modules/blazing/service/effect.go @@ -3,6 +3,9 @@ package service import ( "blazing/cool" "blazing/modules/blazing/model" + "context" + + "github.com/gogf/gf/v2/os/gcache" ) type EffectService struct { @@ -10,30 +13,25 @@ type EffectService struct { } func (s *EffectService) Args(id uint32) (int, []int) { + ret, _ := s.Cache.GetOrSetFuncLock(context.Background(), id, func(context.Context) (interface{}, error) { - m := cool.DBM(s.Model).Where("se_idx", id) - var tt model.PlayerPetSpecialEffect - err := m.Scan(&tt) - if err != nil { - return 0, nil - } + m := cool.DBM(s.Model).Where("se_idx", id) + var tt model.PlayerPetSpecialEffect + err := m.Scan(&tt) + if err != nil { + return 0, nil + } + return tt, nil + }, 0) + tt := ret.Interface().(model.PlayerPetSpecialEffect) - ret := tt.Args - - return int(tt.Eid), ret + return int(tt.Eid), tt.Args } func NewEffectService() *EffectService { return &EffectService{ &cool.Service{ - // PageQueryOp: &cool.QueryOp{ - // ModifyResult: func(ctx g.Ctx, data interface{}) interface{} { - - // // t, _ := json.Marshal(data) - // // gjson.GetBytes(t, "list") - // return data - // }, - // }, + Cache: gcache.New(), Model: model.NewPlayerPetSpecialEffect(), }, } diff --git a/modules/blazing/service/pet_fusion_material_service.go b/modules/blazing/service/pet_fusion_material_service.go index 0932b538..724f5a55 100644 --- a/modules/blazing/service/pet_fusion_material_service.go +++ b/modules/blazing/service/pet_fusion_material_service.go @@ -25,20 +25,12 @@ func NewPetFusionMaterialService() *PetFusionMaterialService { return &PetFusionMaterialService{ &cool.Service{ Model: model.NewPetFusionMaterial(), // 绑定PetFusionMaterial模型(默认参数占位) + Cache: gcache.New(), // PageQueryOp: &cool.QueryOp{KeyWordField: []string{"material1", "material2", "material3", "material4"}}, }, } } -var ( - cachePetFusionMaterialService = gcache.New() -) - -func (s *PetFusionMaterialService) ModifyBefore(ctx context.Context, method string, param g.MapStrAny) (err error) { - cachePetFusionMaterialService.Clear(context.Background()) - return nil -} - // 获取融合材料的特性,返回两个值,一个是指定的特性,另一个是如果配方没找到的情况下,默认的配置 func (s *PetFusionMaterialService) Data(Material1 [4]uint32) uint32 { @@ -57,7 +49,7 @@ func (s *PetFusionMaterialService) Data(Material1 [4]uint32) uint32 { } } - ret, err := cachePetFusionMaterialService.GetOrSetFuncLock(context.Background(), cacheKey, func(context.Context) (interface{}, error) { + ret, err := s.Cache.GetOrSetFuncLock(context.Background(), cacheKey, func(context.Context) (interface{}, error) { m := cool.DBM(s.Model) diff --git a/modules/blazing/service/pet_fusion_service.go b/modules/blazing/service/pet_fusion_service.go index 84940c12..a5249035 100644 --- a/modules/blazing/service/pet_fusion_service.go +++ b/modules/blazing/service/pet_fusion_service.go @@ -23,20 +23,12 @@ func NewPetFusionService() *PetFusionService { return &PetFusionService{ &cool.Service{ Model: model.NewPetFusion(), // 绑定PetFusion模型 + Cache: gcache.New(), PageQueryOp: &cool.QueryOp{FieldEQ: []string{"is_enable", "main_pet_id", "sub_pet_id", "result_pet_id"}}, }, } } -var ( - cachePetFusionService = gcache.New() -) - -func (s *PetFusionService) ModifyBefore(ctx context.Context, method string, param g.MapStrAny) (err error) { - cachePetFusionService.Clear(context.Background()) - return nil -} - //获取主副精灵融合的id,如果不存在,那就给一个保底的id func (s *PetFusionService) Data(p1, p2, rand uint32) uint32 { @@ -67,7 +59,7 @@ func (s *PetFusionService) Data(p1, p2, rand uint32) uint32 { func (s *PetFusionService) getData(p1, p2 uint32) []model.PetFusion { cacheKey := strings.Join([]string{fmt.Sprintf("%d", p1), fmt.Sprintf("%d", p2)}, ":") //println(cacheKey, "获取融合id") - ret, _ := cachePetFusionService.GetOrSetFuncLock(context.Background(), cacheKey, func(context.Context) (interface{}, error) { + ret, _ := s.Cache.GetOrSetFuncLock(context.Background(), cacheKey, func(context.Context) (interface{}, error) { m := cool.DBM(s.Model) var pet []model.PetFusion //一个特性应该是唯一的,但是我们要获取默认随机特性 @@ -89,7 +81,7 @@ func (s *PetFusionService) getData(p1, p2 uint32) []model.PetFusion { func (s *PetFusionService) def() []model.PetFusion { //println(cacheKey, "获取融合id") - ret, _ := cachePetFusionService.GetOrSetFuncLock(context.Background(), "-1", func(context.Context) (interface{}, error) { + ret, _ := s.Cache.GetOrSetFuncLock(context.Background(), "-1", func(context.Context) (interface{}, error) { var pets []model.PetFusion m := cool.DBM(s.Model) diff --git a/modules/blazing/service/talk.go b/modules/blazing/service/talk.go index bfd1e515..e1a72fb9 100644 --- a/modules/blazing/service/talk.go +++ b/modules/blazing/service/talk.go @@ -5,18 +5,6 @@ import ( "blazing/modules/blazing/model" ) -type TalkConfigService struct { - *cool.Service -} - -func NewTalkConfigService() *TalkConfigService { - return &TalkConfigService{ - - Service: &cool.Service{Model: model.NewMineralCollectionConfig()}, - } - -} - type TalkService struct { BaseService } diff --git a/modules/blazing/service/talkconfig.go b/modules/blazing/service/talkconfig.go new file mode 100644 index 00000000..34dbd0ed --- /dev/null +++ b/modules/blazing/service/talkconfig.go @@ -0,0 +1,21 @@ +package service + +import ( + "blazing/cool" + "blazing/modules/blazing/model" + + "github.com/gogf/gf/v2/os/gcache" +) + +type TalkConfigService struct { + *cool.Service +} + +func NewTalkConfigService() *TalkConfigService { + return &TalkConfigService{ + + Service: &cool.Service{Model: model.NewMineralCollectionConfig(), + Cache: gcache.New()}, + } + +}