refactor: 移除 eino 相关代码及依赖
This commit is contained in:
@@ -53,7 +53,7 @@ func getFromCache(ctx context.Context, key string) ([]byte, bool) {
|
||||
if err == nil && !result.IsEmpty() {
|
||||
data := result.Bytes()
|
||||
// 写入本地缓存
|
||||
err = getLocalCache().Set(ctx, key, data, time.Duration(g.Cfg().MustGet(ctx, "cache.localTTL").Int64())*time.Second)
|
||||
err = getLocalCache().Set(ctx, key, data, time.Duration(g.Cfg().MustGet(ctx, "cache.localTTL", 60).Int64())*time.Second)
|
||||
if err != nil {
|
||||
return nil, false
|
||||
}
|
||||
@@ -71,7 +71,7 @@ func setToCache(ctx context.Context, key string, data []byte) (err error) {
|
||||
}
|
||||
|
||||
// 1. 写入本地缓存
|
||||
if err = getLocalCache().Set(ctx, key, data, time.Duration(g.Cfg().MustGet(ctx, "cache.localTTL").Int64())*time.Second); err != nil {
|
||||
if err = getLocalCache().Set(ctx, key, data, time.Duration(g.Cfg().MustGet(ctx, "cache.localTTL", 60).Int64())*time.Second); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -79,7 +79,7 @@ func setToCache(ctx context.Context, key string, data []byte) (err error) {
|
||||
if g.Redis() != nil {
|
||||
_, err = g.Redis().Set(ctx, key, data, gredis.SetOption{
|
||||
TTLOption: gredis.TTLOption{
|
||||
EX: gconv.PtrInt64(g.Cfg().MustGet(ctx, "cache.redisTTL")),
|
||||
EX: gconv.PtrInt64(g.Cfg().MustGet(ctx, "cache.redisTTL", 300)),
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
@@ -405,7 +405,7 @@ func getSelectTypeString(selectType gdb.SelectType) string {
|
||||
}
|
||||
|
||||
// ==================== 调用方法 ====================
|
||||
|
||||
var TablePrefix string
|
||||
var (
|
||||
schemaPrefix = "tenant-"
|
||||
cacheKeyPrefix = "cache-"
|
||||
@@ -452,51 +452,26 @@ func checkSchemaConfig(ctx context.Context) (uint64, bool) {
|
||||
return user.TenantId, false
|
||||
}
|
||||
|
||||
func DB(ctx context.Context) Gfdb {
|
||||
tenantId, config := checkSchemaConfig(ctx)
|
||||
|
||||
var schema = fmt.Sprintf("%s%v", schemaPrefix, tenantId)
|
||||
|
||||
var dbName []string
|
||||
if config {
|
||||
dbName = append(dbName, schema)
|
||||
func DB(ctx context.Context, name ...string) Gfdb {
|
||||
var groupName = gdb.DefaultGroupName
|
||||
if len(name) > 0 && name[0] != "" {
|
||||
groupName = name[0]
|
||||
} else {
|
||||
dbName = append(dbName, "default")
|
||||
// 配置文件中 default 是数组格式,需要通过索引 0 访问
|
||||
defaultConfig := g.Cfg().MustGet(ctx, "database.default")
|
||||
if defaultConfig.IsSlice() {
|
||||
schema = g.Cfg().MustGet(ctx, "database.default.0.name").String()
|
||||
} else {
|
||||
schema = g.Cfg().MustGet(ctx, "database.default.name").String()
|
||||
tenantId, config := checkSchemaConfig(ctx)
|
||||
if config {
|
||||
groupName = fmt.Sprintf("%s%v", schemaPrefix, tenantId)
|
||||
}
|
||||
}
|
||||
|
||||
db := g.DB(groupName)
|
||||
TablePrefix = db.GetConfig().Prefix
|
||||
return &dataBase{
|
||||
DB: g.DB(dbName...).Schema(schema),
|
||||
DB: db,
|
||||
}
|
||||
}
|
||||
|
||||
func (d *dataBase) Model(ctx context.Context, tableNameOrStruct ...any) *model {
|
||||
|
||||
m := d.DB.Model(tableNameOrStruct...).Ctx(ctx)
|
||||
|
||||
tenantId, config := checkSchemaConfig(ctx)
|
||||
|
||||
if config {
|
||||
// 创建按地区分库的配置
|
||||
shardingConfig := gdb.ShardingConfig{
|
||||
Schema: gdb.ShardingSchemaConfig{
|
||||
Enable: true, // 启用分库
|
||||
Prefix: schemaPrefix, // 分库前缀
|
||||
Rule: &RegionShardingRule{RegionMapping: tenantId}, // 自定义分库规则
|
||||
},
|
||||
}
|
||||
m.Sharding(shardingConfig).ShardingValue(tenantId)
|
||||
}
|
||||
|
||||
m.OmitNil().Hook(catchSQLHook())
|
||||
return &model{
|
||||
Model: m,
|
||||
Model: d.DB.Model(tableNameOrStruct...).Ctx(ctx).OmitNil().Hook(catchSQLHook()),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -542,42 +517,3 @@ func getTraceID(ctx context.Context, prefix string) string {
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type RegionShardingRule struct {
|
||||
RegionMapping uint64
|
||||
}
|
||||
|
||||
func (r *RegionShardingRule) SchemaName(ctx context.Context, config gdb.ShardingSchemaConfig, value any) (string, error) {
|
||||
region, ok := value.(uint64)
|
||||
if !ok {
|
||||
return "", fmt.Errorf("sharding value must be string for RegionShardingRule")
|
||||
}
|
||||
|
||||
if r.RegionMapping == region {
|
||||
return config.Prefix + gconv.String(region), nil
|
||||
}
|
||||
|
||||
return "default", nil
|
||||
}
|
||||
|
||||
// TableName 实现分表规则接口
|
||||
func (r *RegionShardingRule) TableName(ctx context.Context, config gdb.ShardingTableConfig, value any) (string, error) {
|
||||
// 这里不实现分表,返回空字符串
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func GetTablePrefix(ctx context.Context) (prefix string, err error) {
|
||||
tenantId, config := checkSchemaConfig(ctx)
|
||||
if config {
|
||||
sprintf := fmt.Sprintf("database.%s%v.0.prefix", schemaPrefix, tenantId)
|
||||
prefix = g.Cfg().MustGet(ctx, sprintf).String()
|
||||
return
|
||||
}
|
||||
defaultConfig := g.Cfg().MustGet(ctx, "database.default")
|
||||
if defaultConfig.IsSlice() {
|
||||
prefix = g.Cfg().MustGet(ctx, "database.default.0.prefix").String()
|
||||
} else {
|
||||
prefix = g.Cfg().MustGet(ctx, "database.default.prefix").String()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user