```
feat(common): 为Service结构体添加缓存支持 在common/cool/service.go中引入gcache包,并在Service结构体中增加Cache字段。同时,在ModifyAfter方法中添加了缓存清理逻辑,确保数据变更后缓存能够及时更新。 该变更影响所有使用Service的模块,包括effect、pet_fusion_material_service和pet_fusion_service等,这些模块现在可以通过统一的缓存机制提升性能。 ```
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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(),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
21
modules/blazing/service/talkconfig.go
Normal file
21
modules/blazing/service/talkconfig.go
Normal file
@@ -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()},
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user