You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

YitIdHelper.go 1.4 kB

4 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * 版权属于:yitter(yitter@126.com)
  3. * 代码编辑:guoyahao
  4. * 代码修订:yitter
  5. * 开源地址:https://gitee.com/yitter/idgenerator
  6. */
  7. package idgen
  8. import (
  9. "sync"
  10. "yitidgen/contract"
  11. )
  12. //var yitIdHelper *YitIdHelper
  13. //var once sync.Once
  14. var idGenerator *DefaultIdGenerator
  15. var singletonMutex sync.Mutex
  16. type YitIdHelper struct {
  17. idGenInstance interface {
  18. NewLong() uint64
  19. }
  20. }
  21. //
  22. //func GetIns() *YitIdHelper {
  23. // once.Do(func() {
  24. // yitIdHelper = &YitIdHelper{}
  25. // })
  26. // return yitIdHelper
  27. //}
  28. //
  29. //func (yih *YitIdHelper) GetIdGenInstance() interface{} {
  30. // return yih.idGenInstance
  31. //}
  32. //
  33. //func (yih *YitIdHelper) SetIdGenerator(options *contract.IdGeneratorOptions) {
  34. // yih.idGenInstance = NewDefaultIdGenerator(options)
  35. //}
  36. //
  37. //func (yih *YitIdHelper) NextId() uint64 {
  38. // once.Do(func() {
  39. // if yih.idGenInstance == nil {
  40. // options := contract.NewIdGeneratorOptions(1)
  41. // yih.idGenInstance = NewDefaultIdGenerator(options)
  42. // }
  43. // })
  44. //
  45. // return yih.idGenInstance.NewLong()
  46. //}
  47. func SetIdGenerator(options *contract.IdGeneratorOptions) {
  48. singletonMutex.Lock()
  49. idGenerator = NewDefaultIdGenerator(options)
  50. singletonMutex.Unlock()
  51. }
  52. func NextId() uint64 {
  53. if idGenerator == nil {
  54. singletonMutex.Lock()
  55. options := contract.NewIdGeneratorOptions(1)
  56. idGenerator = NewDefaultIdGenerator(options)
  57. singletonMutex.Unlock()
  58. }
  59. return idGenerator.NewLong()
  60. }

雪花算法中非常好用的数字ID生成器