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.cs 1.7 kB

4 years ago
4 years ago
4 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * 版权属于:yitter(yitter@126.com)
  3. * 开源地址:https://gitee.com/yitter/idgenerator
  4. * 版权协议:MIT
  5. * 版权说明:只要保留本版权,你可以免费使用、修改、分发本代码。
  6. * 免责条款:任何因为本代码产生的系统、法律、政治、宗教问题,均与版权所有者无关。
  7. *
  8. */
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Text;
  12. namespace Yitter.IdGenerator
  13. {
  14. /// <summary>
  15. /// 这是一个调用的例子,默认情况下,单机集成者可以直接使用 NextId()。
  16. /// </summary>
  17. public class YitIdHelper
  18. {
  19. private static IIdGenerator _IdGenInstance = null;
  20. public static IIdGenerator IdGenInstance => _IdGenInstance;
  21. /// <summary>
  22. /// 设置参数,建议程序初始化时执行一次
  23. /// </summary>
  24. /// <param name="options"></param>
  25. public static void SetIdGenerator(IdGeneratorOptions options)
  26. {
  27. _IdGenInstance = new DefaultIdGenerator(options);
  28. }
  29. /// <summary>
  30. /// 生成新的Id
  31. /// 调用本方法前,请确保调用了 SetIdGenerator 方法做初始化。
  32. /// 否则将会初始化一个WorkerId为1的对象。
  33. /// </summary>
  34. /// <returns></returns>
  35. public static long NextId()
  36. {
  37. if (_IdGenInstance == null)
  38. {
  39. _IdGenInstance = new DefaultIdGenerator(
  40. new IdGeneratorOptions() { WorkerId = 1 }
  41. );
  42. }
  43. return _IdGenInstance.NewLong();
  44. }
  45. }
  46. }

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