namespace IStation
{
///
/// 雪花Id辅助类
///
public class SnowflakeIdHelper
{
SnowflakeIdHelper()
{
var options = new IdGeneratorOptions(1); //构造方法初始化雪花Id
YitIdHelper.SetIdGenerator(options);
}
private static IIdGenerator _IdGenInstance = null;
public static IIdGenerator IdGenInstance => _IdGenInstance;
///
/// 设置参数,建议程序初始化时执行一次
///
/// workerId
public static void SetIdGenerator(ushort workerId)
{
var options = new IdGeneratorOptions() { WorkerId = workerId };
_IdGenInstance = new DefaultIdGenerator(options);
}
///
/// 生成新的Id
/// 调用本方法前,请确保调用了 SetIdGenerator 方法做初始化。
/// 否则将会初始化一个WorkerId为1的对象。
///
///
public static long NextId()
{
if (_IdGenInstance == null)
{
_IdGenInstance = new DefaultIdGenerator(
new IdGeneratorOptions() { WorkerId = 0 }
);
}
return _IdGenInstance.NewLong();
}
}
}