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 2.0 kB

4 years ago
4 years ago
4 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * 版权属于:yitter(yitter@126.com)
  3. * 开源地址:https://github.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. /// </summary>
  33. /// <returns></returns>
  34. public static long NextId()
  35. {
  36. //if (_IdGenInstance == null)
  37. //{
  38. // lock (_IdGenInstance)
  39. // {
  40. // if (_IdGenInstance == null)
  41. // {
  42. // _IdGenInstance = new DefaultIdGenerator(
  43. // new IdGeneratorOptions() { WorkerId = 0 }
  44. // );
  45. // }
  46. // }
  47. //}
  48. if (_IdGenInstance == null) throw new ArgumentException("Please initialize Yitter.IdGeneratorOptions first.");
  49. return _IdGenInstance.NewLong();
  50. }
  51. }
  52. }