```
refactor(common): 移除冗余缓存逻辑并统一数据库缓存适配器配置 将项目中多处手动管理的 gcache 缓存替换为数据库内置缓存机制, 提升缓存使用一致性与可维护性。同时,在初始化时增加对数据库 缓存适配器的设置,确保 Redis 模式下缓存生效。 涉及模块: - common/cool 包下的缓存初始化逻辑调整 - 多个 service 文件中移除 gcache 实例及相关调用 - 使用 gdb.CacheOption 替代原有缓存方法实现数据查询缓存 ```
This commit is contained in:
@@ -54,6 +54,7 @@ func init() {
|
||||
}
|
||||
CacheManager.SetAdapter(gcache.NewAdapterRedis(redis))
|
||||
IsRedisMode = true
|
||||
g.DB().GetCache().SetAdapter(gcache.NewAdapterRedis(redis)) //设置数据库
|
||||
}
|
||||
//Logerebug(ctx, "当前运行模式", RunMode)
|
||||
Loger.Debug(ctx, "当前实例ID:", ProcessFlag)
|
||||
|
||||
@@ -7,7 +7,6 @@ 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"
|
||||
)
|
||||
|
||||
@@ -31,7 +30,7 @@ type Service struct {
|
||||
InfoIgnoreProperty string // Info时忽略的字段,多个字段用逗号隔开
|
||||
UniqueKey g.MapStrStr // 唯一键 key:字段名 value:错误信息
|
||||
NotNullKey g.MapStrStr // 非空键 key:字段名 value:错误信息
|
||||
Cache *gcache.Cache
|
||||
//Cache *gcache.Cache
|
||||
}
|
||||
|
||||
// List/Add接口条件配置
|
||||
@@ -415,10 +414,13 @@ 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())
|
||||
// if s.Cache != nil {
|
||||
// s.Cache.Clear(context.Background())
|
||||
|
||||
// }
|
||||
|
||||
g.DB().GetCore().ClearCache(context.TODO(), s.Model.TableName())
|
||||
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -3,9 +3,8 @@ package service
|
||||
import (
|
||||
"blazing/cool"
|
||||
"blazing/modules/blazing/model"
|
||||
"context"
|
||||
|
||||
"github.com/gogf/gf/v2/os/gcache"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
)
|
||||
|
||||
type EffectService struct {
|
||||
@@ -13,15 +12,13 @@ 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).Cache(gdb.CacheOption{
|
||||
// Duration: time.Hour,
|
||||
|
||||
m := cool.DBM(s.Model).Where("se_idx", id)
|
||||
var tt model.PlayerPetSpecialEffect
|
||||
m.Scan(&tt)
|
||||
|
||||
return tt, nil
|
||||
}, 0)
|
||||
tt := ret.Interface().(model.PlayerPetSpecialEffect)
|
||||
Force: false,
|
||||
})
|
||||
var tt model.PlayerPetSpecialEffect
|
||||
m.Scan(&tt)
|
||||
|
||||
return int(tt.Eid), tt.Args
|
||||
|
||||
@@ -29,7 +26,7 @@ func (s *EffectService) Args(id uint32) (int, []int) {
|
||||
func NewEffectService() *EffectService {
|
||||
return &EffectService{
|
||||
&cool.Service{
|
||||
Cache: gcache.New(),
|
||||
|
||||
Model: model.NewPlayerPetSpecialEffect(),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -4,11 +4,10 @@ import (
|
||||
"blazing/cool"
|
||||
"blazing/modules/blazing/model"
|
||||
"blazing/modules/dict/service"
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gcache"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"github.com/gogf/gf/v2/util/grand"
|
||||
)
|
||||
@@ -25,7 +24,7 @@ func NewPetFusionMaterialService() *PetFusionMaterialService {
|
||||
return &PetFusionMaterialService{
|
||||
&cool.Service{
|
||||
Model: model.NewPetFusionMaterial(), // 绑定PetFusionMaterial模型(默认参数占位)
|
||||
Cache: gcache.New(),
|
||||
//Cache: gcache.New(),
|
||||
// PageQueryOp: &cool.QueryOp{KeyWordField: []string{"material1", "material2", "material3", "material4"}},
|
||||
},
|
||||
}
|
||||
@@ -46,26 +45,23 @@ func (s *PetFusionMaterialService) Data(Material1 [4]uint32) uint32 {
|
||||
}
|
||||
|
||||
}
|
||||
ret, _ := s.Cache.GetOrSetFuncLock(context.Background(), cacheKey, func(context.Context) (interface{}, error) {
|
||||
m := cool.DBM(s.Model)
|
||||
|
||||
m := cool.DBM(s.Model)
|
||||
var effect *model.PetFusionMaterial //一个特性应该是唯一的,但是我们要获取默认随机特性
|
||||
condition := g.Map{
|
||||
"material1": fusions[Material1[0]].ID,
|
||||
"material2": fusions[Material1[1]].ID,
|
||||
"material3": fusions[Material1[2]].ID,
|
||||
"material4": fusions[Material1[3]].ID,
|
||||
"is_enable": 1,
|
||||
}
|
||||
m.Where(condition).Cache(gdb.CacheOption{
|
||||
// Duration: time.Hour,
|
||||
|
||||
var effect *model.PetFusionMaterial //一个特性应该是唯一的,但是我们要获取默认随机特性
|
||||
condition := g.Map{
|
||||
"material1": fusions[Material1[0]].ID,
|
||||
"material2": fusions[Material1[1]].ID,
|
||||
"material3": fusions[Material1[2]].ID,
|
||||
"material4": fusions[Material1[3]].ID,
|
||||
"is_enable": 1,
|
||||
}
|
||||
m.Where(condition).Scan(&effect)
|
||||
//这时候有可能效果是空的,那么这时候就再次查询默认的特性,保证每次必会生成一个数据库有的特性
|
||||
//也许这个时候的特性配方就是随机从数据库中查找一个特性
|
||||
|
||||
return effect, nil
|
||||
}, 0)
|
||||
|
||||
effect := ret.Interface().(*model.PetFusionMaterial)
|
||||
Force: false,
|
||||
}).Scan(&effect)
|
||||
//这时候有可能效果是空的,那么这时候就再次查询默认的特性,保证每次必会生成一个数据库有的特性
|
||||
//也许这个时候的特性配方就是随机从数据库中查找一个特性
|
||||
|
||||
if effect == nil {
|
||||
effect2s := service.DictInfoServiceS.GetData("effect")
|
||||
|
||||
@@ -3,13 +3,10 @@ package service
|
||||
import (
|
||||
"blazing/cool"
|
||||
"blazing/modules/blazing/model"
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/alpacahq/alpacadecimal"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gcache"
|
||||
"github.com/gogf/gf/v2/util/grand"
|
||||
)
|
||||
|
||||
@@ -24,8 +21,8 @@ var PetFusionServiceS = NewPetFusionService()
|
||||
func NewPetFusionService() *PetFusionService {
|
||||
return &PetFusionService{
|
||||
&cool.Service{
|
||||
Model: model.NewPetFusion(), // 绑定PetFusion模型
|
||||
Cache: gcache.New(),
|
||||
Model: model.NewPetFusion(), // 绑定PetFusion模型
|
||||
//Cache: gcache.New(),
|
||||
PageQueryOp: &cool.QueryOp{FieldEQ: []string{"is_enable", "main_pet_id", "sub_pet_id", "result_pet_id"}},
|
||||
},
|
||||
}
|
||||
@@ -59,38 +56,37 @@ 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, _ := s.Cache.GetOrSetFuncLock(context.Background(), cacheKey, func(context.Context) (interface{}, error) {
|
||||
m := cool.DBM(s.Model)
|
||||
//cacheKey := strings.Join([]string{fmt.Sprintf("%d", p1), fmt.Sprintf("%d", p2)}, ":")
|
||||
m := cool.DBM(s.Model)
|
||||
|
||||
var pet []model.PetFusion //一个特性应该是唯一的,但是我们要获取默认随机特性
|
||||
condition := g.Map{
|
||||
"main_pet_id": p1,
|
||||
"sub_pet_id": p2,
|
||||
"is_enable": 1,
|
||||
// "hits between ? and ?" : g.Slice{1, 10},
|
||||
// "exp > 0" : nil,
|
||||
// "category" : g.Slice{100, 200},
|
||||
}
|
||||
m.Where(condition).Scan(&pet)
|
||||
return pet, nil
|
||||
var pet []model.PetFusion //一个特性应该是唯一的,但是我们要获取默认随机特性
|
||||
condition := g.Map{
|
||||
"main_pet_id": p1,
|
||||
"sub_pet_id": p2,
|
||||
"is_enable": 1,
|
||||
// "hits between ? and ?" : g.Slice{1, 10},
|
||||
// "exp > 0" : nil,
|
||||
// "category" : g.Slice{100, 200},
|
||||
}
|
||||
m.Where(condition).Cache(gdb.CacheOption{
|
||||
// Duration: time.Hour,
|
||||
|
||||
}, 0)
|
||||
return ret.Interface().([]model.PetFusion)
|
||||
Force: false,
|
||||
}).Scan(&pet)
|
||||
return pet
|
||||
|
||||
}
|
||||
func (s *PetFusionService) def() []model.PetFusion {
|
||||
|
||||
//println(cacheKey, "获取融合id")
|
||||
ret, _ := s.Cache.GetOrSetFuncLock(context.Background(), "-1", func(context.Context) (interface{}, error) {
|
||||
var pets []model.PetFusion
|
||||
m := cool.DBM(s.Model)
|
||||
m.Where("is_enable", 1).Where("is_default", 1).Cache(gdb.CacheOption{
|
||||
// Duration: time.Hour,
|
||||
|
||||
var pets []model.PetFusion
|
||||
m := cool.DBM(s.Model)
|
||||
m.Where("is_enable", 1).Where("is_default", 1).Scan(&pets)
|
||||
Force: false,
|
||||
}).Scan(&pets)
|
||||
|
||||
return pets, nil
|
||||
}, 0)
|
||||
return ret.Interface().([]model.PetFusion)
|
||||
return pets
|
||||
// return ret.Interface().([]model.PetFusion)
|
||||
|
||||
}
|
||||
|
||||
@@ -3,9 +3,8 @@ package service
|
||||
import (
|
||||
"blazing/cool"
|
||||
"blazing/modules/blazing/model"
|
||||
"context"
|
||||
|
||||
"github.com/gogf/gf/v2/os/gcache"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
)
|
||||
|
||||
type TalkConfigService struct {
|
||||
@@ -17,19 +16,20 @@ var TalkConfigServiceS = NewTalkConfigService()
|
||||
func NewTalkConfigService() *TalkConfigService {
|
||||
return &TalkConfigService{
|
||||
|
||||
Service: &cool.Service{Model: model.NewMineralCollectionConfig(),
|
||||
Cache: gcache.New()},
|
||||
Service: &cool.Service{Model: model.NewMineralCollectionConfig()},
|
||||
// Cache: gcache.New()},
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (s *TalkConfigService) GetCache(flag int) []model.MineralCollectionConfig {
|
||||
|
||||
ret, _ := s.Cache.GetOrSetFuncLock(context.Background(), flag, func(context.Context) (interface{}, error) {
|
||||
var config []model.MineralCollectionConfig
|
||||
cool.DBM(s.Model).Where("type", flag).Scan(&config)
|
||||
return config, nil
|
||||
}, 0)
|
||||
return ret.Interface().([]model.MineralCollectionConfig)
|
||||
var config []model.MineralCollectionConfig
|
||||
cool.DBM(s.Model).Where("type", flag).Cache(gdb.CacheOption{
|
||||
// Duration: time.Hour,
|
||||
|
||||
Force: false,
|
||||
}).Scan(&config)
|
||||
return config
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user