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

2 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
12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. if idGenerator == nil {
  31. panic("Please initialize Yitter.IdGeneratorOptions first.")
  32. }
  33. return idGenerator.NewLong()
  34. }
  35. func ExtractTime(id int64) time.Time {
  36. return idGenerator.ExtractTime(id)
  37. }