feat: 优化雪花ID生成及添加本地地址工具函数
This commit is contained in:
@@ -27,7 +27,8 @@ import (
|
||||
// ==================== 缓存管理器(单例) ====================
|
||||
|
||||
var (
|
||||
localCache *gcache.Cache
|
||||
localCache *gcache.Cache
|
||||
snowflakeNode *snowflake.Node
|
||||
)
|
||||
|
||||
// getLocalCache 获取本地缓存实例
|
||||
@@ -165,18 +166,22 @@ func insertHook(ctx context.Context, in *gdb.HookInsertInput) (result sql.Result
|
||||
return nil, err
|
||||
}
|
||||
|
||||
nodeId := genv.Get("APP_NODE", "").Int64()
|
||||
if g.IsEmpty(nodeId) {
|
||||
nodeId = 1
|
||||
// 懒加载初始化全局snowflake节点,只创建一次
|
||||
if snowflakeNode == nil {
|
||||
nodeId := genv.Get("APP_NODE", "").Int64()
|
||||
if g.IsEmpty(nodeId) {
|
||||
nodeId = 1
|
||||
}
|
||||
node, err := snowflake.NewNode(nodeId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
snowflakeNode = node
|
||||
}
|
||||
|
||||
node, err := snowflake.NewNode(nodeId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for i := range in.Data {
|
||||
if _, ok := in.Data[i]["id"]; ok {
|
||||
in.Data[i]["id"] = node.Generate().Int64()
|
||||
in.Data[i]["id"] = snowflakeNode.Generate().Int64()
|
||||
}
|
||||
if _, ok := in.Data[i]["tenant_id"]; ok {
|
||||
if g.IsEmpty(in.Data[i]["tenant_id"]) {
|
||||
|
||||
Reference in New Issue
Block a user