sk-matrix-project/backend/golang/matrix-middle-service/pkg/utils/sonyflake/sonyflake.go

32 lines
592 B
Go
Raw Normal View History

2023-02-26 23:25:20 +08:00
package sonyflake
import (
2023-02-28 00:03:02 +08:00
"fmt"
2023-02-26 23:25:20 +08:00
"github.com/sony/sonyflake"
sf "matrix-middle-service/pkg/config/sonyflake"
"matrix-middle-service/pkg/logger"
2023-02-26 23:25:20 +08:00
"time"
)
var generator *sonyflake.Sonyflake
func Init(config *sf.Config) {
logger.Log().Debug("[sonyFlake] 初始化")
2023-02-26 23:25:20 +08:00
var s sonyflake.Settings
s.StartTime, _ = time.Parse("2006-01-02", config.StartTime)
2023-02-26 23:25:20 +08:00
generator = sonyflake.NewSonyflake(s)
}
func SonyFlake() *sonyflake.Sonyflake {
return generator
}
2023-02-28 00:03:02 +08:00
func NextId() uint64 {
id, _ := generator.NextID()
return id
}
func NextStrId() string {
return fmt.Sprintf("%d", NextId())
}