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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. )
  11. //var yitIdHelper *YitIdHelper
  12. //var once sync.Once
  13. var idGenerator *DefaultIdGenerator
  14. var singletonMutex sync.Mutex
  15. type YitIdHelper struct {
  16. idGenInstance interface {
  17. NewLong() uint64
  18. }
  19. }
  20. //
  21. //func GetIns() *YitIdHelper {
  22. // once.Do(func() {
  23. // yitIdHelper = &YitIdHelper{}
  24. // })
  25. // return yitIdHelper
  26. //}
  27. //
  28. //func (yih *YitIdHelper) GetIdGenInstance() interface{} {
  29. // return yih.idGenInstance
  30. //}
  31. //
  32. //func (yih *YitIdHelper) SetIdGenerator(options *contract.IdGeneratorOptions) {
  33. // yih.idGenInstance = NewDefaultIdGenerator(options)
  34. //}
  35. //
  36. //func (yih *YitIdHelper) NextId() uint64 {
  37. // once.Do(func() {
  38. // if yih.idGenInstance == nil {
  39. // options := contract.NewIdGeneratorOptions(1)
  40. // yih.idGenInstance = NewDefaultIdGenerator(options)
  41. // }
  42. // })
  43. //
  44. // return yih.idGenInstance.NewLong()
  45. //}
  46. func SetIdGenerator(options *IdGeneratorOptions) {
  47. singletonMutex.Lock()
  48. idGenerator = NewDefaultIdGenerator(options)
  49. singletonMutex.Unlock()
  50. }
  51. func NextId() uint64 {
  52. if idGenerator == nil {
  53. singletonMutex.Lock()
  54. if idGenerator == nil {
  55. options := NewIdGeneratorOptions(1)
  56. idGenerator = NewDefaultIdGenerator(options)
  57. }
  58. singletonMutex.Unlock()
  59. }
  60. return idGenerator.NewLong()
  61. }

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