2026-05-08 07:27:06 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"apigo.cc/go/id"
|
2026-05-08 22:41:01 +08:00
|
|
|
"apigo.cc/go/log"
|
|
|
|
|
"apigo.cc/go/redis"
|
|
|
|
|
"sync"
|
2026-05-08 07:27:06 +08:00
|
|
|
)
|
|
|
|
|
|
2026-05-08 22:41:01 +08:00
|
|
|
var (
|
|
|
|
|
idMaker IDMakerInterface
|
|
|
|
|
idMakerLock sync.Mutex
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// IDMakerInterface ID 生成器接口
|
|
|
|
|
type IDMakerInterface interface {
|
|
|
|
|
Get(size int) string
|
|
|
|
|
GetForMysql(size int) string
|
|
|
|
|
GetForPostgreSQL(size int) string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getIDMaker() IDMakerInterface {
|
|
|
|
|
if idMaker != nil {
|
|
|
|
|
return idMaker
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
idMakerLock.Lock()
|
|
|
|
|
defer idMakerLock.Unlock()
|
|
|
|
|
|
|
|
|
|
if idMaker != nil {
|
|
|
|
|
return idMaker
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if Config.IdServer != "" {
|
|
|
|
|
rd := redis.GetRedis(Config.IdServer, log.DefaultLogger)
|
|
|
|
|
if rd.Error == nil {
|
|
|
|
|
idMaker = redis.NewIDMaker(rd)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if idMaker == nil {
|
|
|
|
|
idMaker = id.DefaultIDMaker
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return idMaker
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-08 07:27:06 +08:00
|
|
|
// MakeId 生成指定长度的 ID
|
|
|
|
|
func MakeId(size int) string {
|
2026-05-08 22:41:01 +08:00
|
|
|
return getIDMaker().Get(size)
|
2026-05-08 07:27:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MakeIdForMysql 生成适用于 MySQL 的有序 ID
|
|
|
|
|
func MakeIdForMysql(size int) string {
|
2026-05-08 22:41:01 +08:00
|
|
|
return getIDMaker().GetForMysql(size)
|
2026-05-08 07:27:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MakeIdForPostgreSQL 生成适用于 PostgreSQL 的有序 ID
|
|
|
|
|
func MakeIdForPostgreSQL(size int) string {
|
2026-05-08 22:41:01 +08:00
|
|
|
return getIDMaker().GetForPostgreSQL(size)
|
2026-05-08 07:27:06 +08:00
|
|
|
}
|