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 797 B

3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /*
  2. * 版权属于:yitter(yitter@126.com)
  3. * 代码编辑:guoyahao
  4. * 代码修订:yitter
  5. * 开源地址:https://github.com/yitter/idgenerator
  6. */
  7. package idgen
  8. import (
  9. "sync"
  10. "time"
  11. )
  12. var singletonMutex sync.Mutex
  13. var idGenerator *DefaultIdGenerator
  14. // SetIdGenerator .
  15. func SetIdGenerator(options *IdGeneratorOptions) {
  16. singletonMutex.Lock()
  17. idGenerator = NewDefaultIdGenerator(options)
  18. singletonMutex.Unlock()
  19. }
  20. // NextId .
  21. func NextId() int64 {
  22. //if idGenerator == nil {
  23. // singletonMutex.Lock()
  24. // defer singletonMutex.Unlock()
  25. // if idGenerator == nil {
  26. // options := NewIdGeneratorOptions(1)
  27. // idGenerator = NewDefaultIdGenerator(options)
  28. // }
  29. //}
  30. return idGenerator.NewLong()
  31. }
  32. func ExtractTime(id int64) time.Time {
  33. return idGenerator.ExtractTime(id)
  34. }