refactor(dict): 调整缓存逻辑并优化数据获取方式

将原先基于 gcache 的缓存机制移除,改为使用 gdb 内置的
This commit is contained in:
2025-12-15 06:15:55 +08:00
parent 56af8951c7
commit 78bb2bd148
2 changed files with 25 additions and 25 deletions

View File

@@ -13,7 +13,7 @@ type EffectService struct {
func (s *EffectService) Args(id uint32) (int, []int) {
m := cool.DBM(s.Model).Where("se_idx", id).Cache(gdb.CacheOption{
// Duration: time.Hour,
//Duration: time.Hour,
Force: false,
})

View File

@@ -8,8 +8,8 @@ import (
"blazing/modules/dict/model"
"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"
)
@@ -61,37 +61,37 @@ func (s *DictInfoService) GetData(types string) (data map[uint32]model.DictInfo)
dictInfoModel = model.NewDictInfo()
dictTypeModel = model.NewDictType()
)
ret, _ := cacheDict.GetOrSetFunc(context.Background(), types, func(context.Context) (interface{}, error) {
// 如果typeData为空, 则返回空
var ty *model.DictType
mType := cool.DBM(dictTypeModel)
mType.Where("key in (?)", types).Scan(&ty)
// 如果typeData为空, 则返回空
var ty *model.DictType
mType := cool.DBM(dictTypeModel)
mType.Where("key in (?)", types).Cache(gdb.CacheOption{
// Duration: time.Hour,
// 如果typeData为空, 则返回空
if ty == nil {
return []model.DictInfo{}, nil
}
m := cool.DBM(dictInfoModel)
var ress []model.DictInfo
m.Where("typeId", ty.ID).Scan(&ress)
fusions := utils.ToMap(ress, func(t model.DictInfo) uint32 {
Force: false,
}).Scan(&ty)
return gconv.Uint32(t.Value) //物品id转id
})
return fusions, nil
}, 0)
// 如果typeData为空, 则返回空
if ty == nil {
return nil
}
m := cool.DBM(dictInfoModel)
var ress []model.DictInfo
m.Where("typeId", ty.ID).Cache(gdb.CacheOption{
// Duration: time.Hour,
return ret.Interface().(map[uint32]model.DictInfo)
Force: false,
}).Scan(&ress)
fusions := utils.ToMap(ress, func(t model.DictInfo) uint32 {
return gconv.Uint32(t.Value) //物品id转id
})
return fusions
}
var (
cacheDict = gcache.New()
)
// ModifyAfter 修改后
func (s *DictInfoService) ModifyAfter(ctx context.Context, method string, param map[string]interface{}) (err error) {
cacheDict.Clear(context.Background())
if method == "Delete" {
// 删除后,同时删除子节点
ids, ok := param["ids"]