提交 acaefa52 编写于 作者: yitter's avatar yitter

auto commit

上级 e9c6ad02
/*
* 版权属于:yitter(yitter@126.com)
* 代码编辑:guoyahao
* 代码修订:yitter
* 开源地址:https://gitee.com/yitter/idgenerator
*/
package idgen package idgen
import ( import (
"sync" "sync"
) )
//var yitIdHelper *YitIdHelper
//var once sync.Once
var idGenerator *DefaultIdGenerator
var singletonMutex sync.Mutex var singletonMutex sync.Mutex
var idGenerator *DefaultIdGenerator
type YitIdHelper struct { // SetIdGenerator .
idGenInstance interface {
NewLong() uint64
}
}
//
//func GetIns() *YitIdHelper {
// once.Do(func() {
// yitIdHelper = &YitIdHelper{}
// })
// return yitIdHelper
//}
//
//func (yih *YitIdHelper) GetIdGenInstance() interface{} {
// return yih.idGenInstance
//}
//
//func (yih *YitIdHelper) SetIdGenerator(options *contract.IdGeneratorOptions) {
// yih.idGenInstance = NewDefaultIdGenerator(options)
//}
//
//func (yih *YitIdHelper) NextId() uint64 {
// once.Do(func() {
// if yih.idGenInstance == nil {
// options := contract.NewIdGeneratorOptions(1)
// yih.idGenInstance = NewDefaultIdGenerator(options)
// }
// })
//
// return yih.idGenInstance.NewLong()
//}
func SetIdGenerator(options *IdGeneratorOptions) { func SetIdGenerator(options *IdGeneratorOptions) {
singletonMutex.Lock() singletonMutex.Lock()
idGenerator = NewDefaultIdGenerator(options) idGenerator = NewDefaultIdGenerator(options)
singletonMutex.Unlock() singletonMutex.Unlock()
} }
// NextId .
func NextId() uint64 { func NextId() uint64 {
if idGenerator == nil { if idGenerator == nil {
singletonMutex.Lock() singletonMutex.Lock()
defer singletonMutex.Unlock()
if idGenerator == nil { if idGenerator == nil {
options := NewIdGeneratorOptions(1) options := NewIdGeneratorOptions(1)
idGenerator = NewDefaultIdGenerator(options) idGenerator = NewDefaultIdGenerator(options)
} }
singletonMutex.Unlock()
} }
return idGenerator.NewLong() return idGenerator.NewLong()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册