refactor(common): 移除冗余缓存逻辑并统一数据库缓存适配器配置

将项目中多处手动管理的 gcache 缓存替换为数据库内置缓存机制,
提升缓存使用一致性与可维护性。同时,在初始化时增加对数据库
缓存适配器的设置,确保 Redis 模式下缓存生效。

涉及模块:
- common/cool 包下的缓存初始化逻辑调整
- 多个 service 文件中移除 gcache 实例及相关调用
- 使用 gdb.CacheOption 替代原有缓存方法实现数据查询缓存
```
This commit is contained in:
2025-12-15 05:39:11 +08:00
parent af7cdddcb5
commit 56af8951c7
6 changed files with 70 additions and 78 deletions

View File

@@ -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)

View File

@@ -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
}