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.d 1.1 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /*
  2. * 版权属于:yitter(yitter@126.com)
  3. * 开源地址:https://gitee.com/yitter/idgenerator
  4. */
  5. module yitter.idgen.YitIdHelper;
  6. import yitter.idgen.DefaultIdGenerator;
  7. import yitter.contract.IIdGenerator;
  8. import yitter.contract.IdGeneratorException;
  9. import yitter.contract.IdGeneratorOptions;
  10. import std.concurrency : initOnce;
  11. /**
  12. * 这是一个调用的例子,默认情况下,单机集成者可以直接使用 nextId()。
  13. */
  14. class YitIdHelper {
  15. private __gshared IIdGenerator idGenInstance = null;
  16. static IIdGenerator getIdGenInstance() {
  17. return initOnce!idGenInstance(new DefaultIdGenerator(new IdGeneratorOptions(1)));
  18. }
  19. /**
  20. * 设置参数,建议程序初始化时执行一次
  21. */
  22. static void setIdGenerator(IdGeneratorOptions options) {
  23. idGenInstance = new DefaultIdGenerator(options);
  24. }
  25. /**
  26. * 生成新的Id
  27. * 调用本方法前,请确保调用了 SetIdGenerator 方法做初始化。
  28. *
  29. * @return
  30. */
  31. static long nextId() {
  32. return getIdGenInstance().newLong();
  33. }
  34. }