All checks were successful
ci/woodpecker/push/my-first-workflow Pipeline was successful
refactor(common): 统一Redis连接方式并优化代码结构
- 将 g.Redis("cool").Conn(ctx) 统一改为 Redis.Conn(ctx) 的调用方式
- 在coolconfig中添加ServerList.GetID()方法用于生成服务器唯一标识
- 引入gconv包用于类型转换操作
feat(rpc): 完善ListenFight函数实现集群消息监听
- 新增ListenFight函数,完全对齐ListenFunc
112 lines
2.9 KiB
Go
112 lines
2.9 KiB
Go
package cool
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/gogf/gf/v2/database/gredis"
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"github.com/gogf/gf/v2/i18n/gi18n"
|
|
"github.com/gogf/gf/v2/os/gcache"
|
|
"github.com/gogf/gf/v2/os/gctx"
|
|
"github.com/gogf/gf/v2/os/gtime"
|
|
"github.com/gogf/gf/v2/util/guid"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
var (
|
|
GormDBS = make(map[string]*gorm.DB) // 定义全局gorm.DB对象集合 仅供内部使用
|
|
CacheEPS = gcache.New() // 定义全局缓存对象 供EPS使用
|
|
CacheManager = gcache.New() // 定义全局缓存对象 供其他业务使用
|
|
ProcessFlag = guid.S() // 定义全局进程标识
|
|
// RunMode = "dev" // 定义全局运行模式
|
|
IsRedisMode = false // 定义全局是否为redis模式
|
|
I18n = gi18n.New() // 定义全局国际化对象
|
|
Redis *gredis.Redis
|
|
)
|
|
|
|
func init() {
|
|
err := gtime.SetTimeZone("Asia/Shanghai")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
var (
|
|
ctx = gctx.GetInitCtx()
|
|
redisConfig = &gredis.Config{}
|
|
)
|
|
Logger.Debug(ctx, "module cool init start ...", gtime.Now())
|
|
// buildData := gbuild.Data()
|
|
// if _, ok := buildData["mode"]; ok {
|
|
// RunMode = buildData["mode"].(string)
|
|
// }
|
|
// if RunMode == "cool-tools" {
|
|
// return
|
|
// }
|
|
redisVar, err := g.Cfg().Get(ctx, "redis.cool")
|
|
if err != nil {
|
|
Logger.Error(ctx, "初始化缓存失败,请检查配置文件")
|
|
panic(err)
|
|
}
|
|
if !redisVar.IsEmpty() {
|
|
redisVar.Struct(redisConfig)
|
|
Redis, err = gredis.New(redisConfig)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
CacheManager.SetAdapter(gcache.NewAdapterRedis(Redis))
|
|
IsRedisMode = true
|
|
// g.DB().GetCache().SetAdapter(gcache.NewAdapterRedis(redis)) //设置数据库
|
|
}
|
|
//Logerebug(ctx, "当前运行模式", RunMode)
|
|
Logger.Debug(ctx, "当前实例ID:", ProcessFlag)
|
|
Logger.Debug(ctx, "是否缓存模式:", IsRedisMode)
|
|
Logger.Debug(ctx, "module cool init finished ...")
|
|
|
|
}
|
|
|
|
// cool.OK 正常返回
|
|
type BaseRes struct {
|
|
Code int `json:"code"`
|
|
Message string `json:"message"`
|
|
Data interface{} `json:"data,omitempty"`
|
|
}
|
|
|
|
// 返回正常结果
|
|
func Ok(data interface{}) *BaseRes {
|
|
|
|
return &BaseRes{
|
|
Code: 1000,
|
|
Message: I18n.Translate(context.TODO(), "BaseResMessage"),
|
|
Data: data,
|
|
}
|
|
}
|
|
|
|
// 失败返回结果
|
|
func Fail(message string) *BaseRes {
|
|
return &BaseRes{
|
|
Code: 1001,
|
|
Message: message,
|
|
}
|
|
}
|
|
|
|
// 分布式函数
|
|
// func DistributedFunc(ctx g.Ctx, f func(ctx g.Ctx) (interface{}, error)) (interface{}, error) {
|
|
// if ProcessFlag == ctx.Request.Header.Get("processFlag") {
|
|
// return f(ctx)
|
|
// }
|
|
// return nil, nil
|
|
// }
|
|
|
|
func RedisDo(ctx context.Context, funcstring string, a ...any) {
|
|
|
|
conn, err := Redis.Conn(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
defer conn.Close(ctx)
|
|
_, err = conn.Do(ctx, "publish", funcstring, a)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|