AddGo
This commit is contained in:
@@ -1,29 +0,0 @@
|
||||
/*
|
||||
* 版权属于:yitter(yitter@126.com)
|
||||
* 开源地址:https://gitee.com/yitter/idgenerator
|
||||
* 版权协议:MIT
|
||||
* 版权说明:只要保留本版权,你可以免费使用、修改、分发本代码。
|
||||
* 免责条款:任何因为本代码产生的系统、法律、政治、宗教问题,均与版权所有者无关。
|
||||
*
|
||||
*/
|
||||
|
||||
using System;
|
||||
|
||||
namespace Yitter.IdGenerator
|
||||
{
|
||||
public interface IIdGenerator
|
||||
{
|
||||
/// <summary>
|
||||
/// 生成过程中产生的事件
|
||||
/// </summary>
|
||||
Action<OverCostActionArg> GenIdActionAsync { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 生成新的long型Id
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
long NewLong();
|
||||
|
||||
// Guid NewGuid();
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
/*
|
||||
* 版权属于:yitter(yitter@126.com)
|
||||
* 开源地址:https://gitee.com/yitter/idgenerator
|
||||
* 版权协议:MIT
|
||||
* 版权说明:只要保留本版权,你可以免费使用、修改、分发本代码。
|
||||
* 免责条款:任何因为本代码产生的系统、法律、政治、宗教问题,均与版权所有者无关。
|
||||
*
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Yitter.IdGenerator
|
||||
{
|
||||
internal interface ISnowWorker
|
||||
{
|
||||
Action<OverCostActionArg> GenAction { get; set; }
|
||||
|
||||
long NextId();
|
||||
}
|
||||
}
|
||||
@@ -1,80 +0,0 @@
|
||||
/*
|
||||
* 版权属于:yitter(yitter@126.com)
|
||||
* 开源地址:https://gitee.com/yitter/idgenerator
|
||||
* 版权协议:MIT
|
||||
* 版权说明:只要保留本版权,你可以免费使用、修改、分发本代码。
|
||||
* 免责条款:任何因为本代码产生的系统、法律、政治、宗教问题,均与版权所有者无关。
|
||||
*
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Yitter.IdGenerator
|
||||
{
|
||||
public class IdGeneratorOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// 雪花计算方法
|
||||
/// (1-漂移算法|2-传统算法),默认1
|
||||
/// </summary>
|
||||
public virtual short Method { get; set; } = 1;
|
||||
|
||||
/// <summary>
|
||||
/// 开始时间(UTC格式)
|
||||
/// 不能超过当前系统时间
|
||||
/// </summary>
|
||||
public virtual DateTime BaseTime { get; set; } = new DateTime(2020, 2, 20, 2, 20, 2, 20, DateTimeKind.Utc);
|
||||
|
||||
/// <summary>
|
||||
/// 机器码
|
||||
/// 与 WorkerIdBitLength 有关系
|
||||
/// (ushort类型,最大值65535,如果有更高要求,请修改数据类型,或联系作者)
|
||||
/// </summary>
|
||||
public virtual ushort WorkerId { get; set; } = 0;
|
||||
|
||||
/// <summary>
|
||||
/// 机器码位长
|
||||
/// 范围:1-21(要求:序列数位长+机器码位长不超过22)。
|
||||
/// 建议范围:6-12。
|
||||
/// </summary>
|
||||
public virtual byte WorkerIdBitLength { get; set; } = 6;//10;
|
||||
|
||||
/// <summary>
|
||||
/// 序列数位长
|
||||
/// 范围:2-21(要求:序列数位长+机器码位长不超过22)。
|
||||
/// 建议范围:6-14。
|
||||
/// </summary>
|
||||
public virtual byte SeqBitLength { get; set; } = 6;//10;
|
||||
|
||||
/// <summary>
|
||||
/// 最大序列数(含)
|
||||
/// (由SeqBitLength计算的最大值)
|
||||
/// </summary>
|
||||
public virtual int MaxSeqNumber { get; set; } = 0;
|
||||
|
||||
/// <summary>
|
||||
/// 最小序列数(含)
|
||||
/// 默认5,不小于1,不大于MaxSeqNumber
|
||||
/// </summary>
|
||||
public virtual ushort MinSeqNumber { get; set; } = 5;
|
||||
|
||||
/// <summary>
|
||||
/// 最大漂移次数(含),
|
||||
/// 默认2000,推荐范围500-10000(与计算能力有关)
|
||||
/// </summary>
|
||||
public virtual int TopOverCostCount { get; set; } = 2000;
|
||||
|
||||
|
||||
public IdGeneratorOptions()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public IdGeneratorOptions(ushort workerId)
|
||||
{
|
||||
WorkerId = workerId;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
/*
|
||||
* 版权属于:yitter(yitter@126.com)
|
||||
* 开源地址:https://gitee.com/yitter/idgenerator
|
||||
* 版权协议:MIT
|
||||
* 版权说明:只要保留本版权,你可以免费使用、修改、分发本代码。
|
||||
* 免责条款:任何因为本代码产生的系统、法律、政治、宗教问题,均与版权所有者无关。
|
||||
*
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Yitter.IdGenerator
|
||||
{
|
||||
/// <summary>
|
||||
/// Id生成时回调参数
|
||||
/// </summary>
|
||||
public class OverCostActionArg
|
||||
{
|
||||
/// <summary>
|
||||
/// 事件类型
|
||||
/// 1-开始,2-结束,8-漂移
|
||||
/// </summary>
|
||||
public virtual int ActionType { get; set; }
|
||||
/// <summary>
|
||||
/// 时间戳
|
||||
/// </summary>
|
||||
public virtual long TimeTick { get; set; }
|
||||
/// <summary>
|
||||
/// 机器码
|
||||
/// </summary>
|
||||
public virtual ushort WorkerId { get; set; }
|
||||
/// <summary>
|
||||
/// 漂移计算次数
|
||||
/// </summary>
|
||||
public virtual int OverCostCountInOneTerm { get; set; }
|
||||
/// <summary>
|
||||
/// 漂移期间生产ID个数
|
||||
/// </summary>
|
||||
public virtual int GenCountInOneTerm { get; set; }
|
||||
/// <summary>
|
||||
/// 漂移周期
|
||||
/// </summary>
|
||||
public virtual int TermIndex { get; set; }
|
||||
|
||||
public OverCostActionArg(ushort workerId, long timeTick, int actionType = 0, int overCostCountInOneTerm = 0, int genCountWhenOverCost = 0, int index = 0)
|
||||
{
|
||||
ActionType = actionType;
|
||||
TimeTick = timeTick;
|
||||
WorkerId = workerId;
|
||||
OverCostCountInOneTerm = overCostCountInOneTerm;
|
||||
GenCountInOneTerm = genCountWhenOverCost;
|
||||
TermIndex = index;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,334 +0,0 @@
|
||||
/*
|
||||
* 版权属于:yitter(yitter@126.com)
|
||||
* 开源地址:https://gitee.com/yitter/idgenerator
|
||||
* 版权协议:MIT
|
||||
* 版权说明:只要保留本版权,你可以免费使用、修改、分发本代码。
|
||||
* 免责条款:任何因为本代码产生的系统、法律、政治、宗教问题,均与版权所有者无关。
|
||||
*
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Yitter.IdGenerator
|
||||
{
|
||||
/// <summary>
|
||||
/// 雪花漂移算法
|
||||
/// </summary>
|
||||
internal class SnowWorkerM1 : ISnowWorker
|
||||
{
|
||||
/// <summary>
|
||||
/// 基础时间
|
||||
/// </summary>
|
||||
protected readonly DateTime BaseTime;
|
||||
|
||||
/// <summary>
|
||||
/// 机器码
|
||||
/// </summary>
|
||||
protected readonly ushort WorkerId = 0;
|
||||
|
||||
/// <summary>
|
||||
/// 机器码位长
|
||||
/// </summary>
|
||||
protected readonly byte WorkerIdBitLength = 0;
|
||||
|
||||
/// <summary>
|
||||
/// 自增序列数位长
|
||||
/// </summary>
|
||||
protected readonly byte SeqBitLength = 0;
|
||||
|
||||
/// <summary>
|
||||
/// 最大序列数(含)
|
||||
/// </summary>
|
||||
protected readonly int MaxSeqNumber = 0;
|
||||
|
||||
/// <summary>
|
||||
/// 最小序列数(含)
|
||||
/// </summary>
|
||||
protected readonly ushort MinSeqNumber = 0;
|
||||
|
||||
/// <summary>
|
||||
/// 最大漂移次数
|
||||
/// </summary>
|
||||
protected readonly int TopOverCostCount = 0;
|
||||
|
||||
protected readonly byte _TimestampShift = 0;
|
||||
protected static object _SyncLock = new object();
|
||||
|
||||
protected ushort _CurrentSeqNumber;
|
||||
protected long _LastTimeTick = -1L;
|
||||
protected long _TurnBackTimeTick = -1L;
|
||||
protected byte _TurnBackIndex = 0;
|
||||
|
||||
protected bool _IsOverCost = false;
|
||||
protected int _OverCostCountInOneTerm = 0;
|
||||
protected int _GenCountInOneTerm = 0;
|
||||
protected int _TermIndex = 0;
|
||||
|
||||
public Action<OverCostActionArg> GenAction { get; set; }
|
||||
|
||||
public SnowWorkerM1(IdGeneratorOptions options)
|
||||
{
|
||||
WorkerId = options.WorkerId;
|
||||
WorkerIdBitLength = options.WorkerIdBitLength;
|
||||
SeqBitLength = options.SeqBitLength;
|
||||
MaxSeqNumber = options.MaxSeqNumber;
|
||||
MinSeqNumber = options.MinSeqNumber;
|
||||
TopOverCostCount = options.TopOverCostCount;
|
||||
|
||||
if (options.BaseTime != DateTime.MinValue)
|
||||
{
|
||||
BaseTime = options.BaseTime;
|
||||
}
|
||||
|
||||
if (WorkerId < 1)
|
||||
{
|
||||
WorkerId = (ushort)DateTime.Now.Millisecond;
|
||||
}
|
||||
|
||||
if (SeqBitLength == 0)
|
||||
{
|
||||
SeqBitLength = 6;
|
||||
}
|
||||
|
||||
if (WorkerIdBitLength == 0)
|
||||
{
|
||||
WorkerIdBitLength = 6;
|
||||
}
|
||||
|
||||
if (MaxSeqNumber == 0)
|
||||
{
|
||||
MaxSeqNumber = (int)Math.Pow(2, SeqBitLength) - 1;
|
||||
}
|
||||
|
||||
_TimestampShift = (byte)(WorkerIdBitLength + SeqBitLength);
|
||||
_CurrentSeqNumber = options.MinSeqNumber;
|
||||
}
|
||||
|
||||
|
||||
private void DoGenIdAction(OverCostActionArg arg)
|
||||
{
|
||||
Task.Run(() =>
|
||||
{
|
||||
GenAction(arg);
|
||||
});
|
||||
}
|
||||
|
||||
private void BeginOverCostAction(in long useTimeTick)
|
||||
{
|
||||
if (GenAction == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
DoGenIdAction(new OverCostActionArg(
|
||||
WorkerId,
|
||||
useTimeTick,
|
||||
1,
|
||||
_OverCostCountInOneTerm,
|
||||
_GenCountInOneTerm,
|
||||
_TermIndex));
|
||||
}
|
||||
|
||||
private void EndOverCostAction(in long useTimeTick)
|
||||
{
|
||||
if (_TermIndex > 10000)
|
||||
{
|
||||
_TermIndex = 0;
|
||||
}
|
||||
|
||||
if (GenAction == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
DoGenIdAction(new OverCostActionArg(
|
||||
WorkerId,
|
||||
useTimeTick,
|
||||
2,
|
||||
_OverCostCountInOneTerm,
|
||||
_GenCountInOneTerm,
|
||||
_TermIndex));
|
||||
}
|
||||
|
||||
private void BeginTurnBackAction(in long useTimeTick)
|
||||
{
|
||||
if (GenAction == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
DoGenIdAction(new OverCostActionArg(
|
||||
WorkerId,
|
||||
useTimeTick,
|
||||
8,
|
||||
0,
|
||||
0,
|
||||
_TurnBackIndex));
|
||||
}
|
||||
|
||||
private void EndTurnBackAction(in long useTimeTick)
|
||||
{
|
||||
if (GenAction == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
DoGenIdAction(new OverCostActionArg(
|
||||
WorkerId,
|
||||
useTimeTick,
|
||||
9,
|
||||
0,
|
||||
0,
|
||||
_TurnBackIndex));
|
||||
}
|
||||
|
||||
private long NextOverCostId()
|
||||
{
|
||||
long currentTimeTick = GetCurrentTimeTick();
|
||||
|
||||
if (currentTimeTick > _LastTimeTick)
|
||||
{
|
||||
EndOverCostAction(currentTimeTick);
|
||||
|
||||
_LastTimeTick = currentTimeTick;
|
||||
_CurrentSeqNumber = MinSeqNumber;
|
||||
_IsOverCost = false;
|
||||
_OverCostCountInOneTerm = 0;
|
||||
_GenCountInOneTerm = 0;
|
||||
|
||||
return CalcId(_LastTimeTick);
|
||||
}
|
||||
|
||||
if (_OverCostCountInOneTerm >= TopOverCostCount)
|
||||
{
|
||||
EndOverCostAction(currentTimeTick);
|
||||
|
||||
_LastTimeTick = GetNextTimeTick();
|
||||
_CurrentSeqNumber = MinSeqNumber;
|
||||
_IsOverCost = false;
|
||||
_OverCostCountInOneTerm = 0;
|
||||
_GenCountInOneTerm = 0;
|
||||
|
||||
return CalcId(_LastTimeTick);
|
||||
}
|
||||
|
||||
if (_CurrentSeqNumber > MaxSeqNumber)
|
||||
{
|
||||
_LastTimeTick++;
|
||||
_CurrentSeqNumber = MinSeqNumber;
|
||||
_IsOverCost = true;
|
||||
_OverCostCountInOneTerm++;
|
||||
_GenCountInOneTerm++;
|
||||
|
||||
return CalcId(_LastTimeTick);
|
||||
}
|
||||
|
||||
_GenCountInOneTerm++;
|
||||
return CalcId(_LastTimeTick);
|
||||
}
|
||||
|
||||
private long NextNormalId()
|
||||
{
|
||||
long currentTimeTick = GetCurrentTimeTick();
|
||||
|
||||
if (currentTimeTick < _LastTimeTick)
|
||||
{
|
||||
if (_TurnBackTimeTick < 1)
|
||||
{
|
||||
_TurnBackTimeTick = _LastTimeTick - 1;
|
||||
_TurnBackIndex++;
|
||||
|
||||
// 每毫秒序列数的前5位是预留位,0用于手工新值,1-4是时间回拨次序
|
||||
// 最多4次回拨(防止回拨重叠)
|
||||
if (_TurnBackIndex > 4)
|
||||
{
|
||||
_TurnBackIndex = 1;
|
||||
}
|
||||
|
||||
BeginTurnBackAction(_TurnBackTimeTick);
|
||||
}
|
||||
|
||||
Thread.Sleep(10);
|
||||
return CalcTurnBackId(_TurnBackTimeTick);
|
||||
}
|
||||
|
||||
// 时间追平时,_TurnBackTimeTick清零
|
||||
if (_TurnBackTimeTick > 0)
|
||||
{
|
||||
EndTurnBackAction(_TurnBackTimeTick);
|
||||
_TurnBackTimeTick = 0;
|
||||
}
|
||||
|
||||
if (currentTimeTick > _LastTimeTick)
|
||||
{
|
||||
_LastTimeTick = currentTimeTick;
|
||||
_CurrentSeqNumber = MinSeqNumber;
|
||||
|
||||
return CalcId(_LastTimeTick);
|
||||
}
|
||||
|
||||
if (_CurrentSeqNumber > MaxSeqNumber)
|
||||
{
|
||||
BeginOverCostAction(currentTimeTick);
|
||||
|
||||
_TermIndex++;
|
||||
_LastTimeTick++;
|
||||
_CurrentSeqNumber = MinSeqNumber;
|
||||
_IsOverCost = true;
|
||||
_OverCostCountInOneTerm = 1;
|
||||
_GenCountInOneTerm = 1;
|
||||
|
||||
return CalcId(_LastTimeTick);
|
||||
}
|
||||
|
||||
return CalcId(_LastTimeTick);
|
||||
}
|
||||
|
||||
private long CalcId(in long useTimeTick)
|
||||
{
|
||||
var result = ((useTimeTick << _TimestampShift) +
|
||||
((long)WorkerId << SeqBitLength) +
|
||||
(uint)_CurrentSeqNumber);
|
||||
|
||||
_CurrentSeqNumber++;
|
||||
return result;
|
||||
}
|
||||
|
||||
private long CalcTurnBackId(in long useTimeTick)
|
||||
{
|
||||
var result = ((useTimeTick << _TimestampShift) +
|
||||
((long)WorkerId << SeqBitLength) + _TurnBackIndex);
|
||||
|
||||
_TurnBackTimeTick--;
|
||||
return result;
|
||||
}
|
||||
|
||||
protected virtual long GetCurrentTimeTick()
|
||||
{
|
||||
return (long)(DateTime.UtcNow - BaseTime).TotalMilliseconds;
|
||||
}
|
||||
|
||||
protected virtual long GetNextTimeTick()
|
||||
{
|
||||
long tempTimeTicker = GetCurrentTimeTick();
|
||||
|
||||
while (tempTimeTicker <= _LastTimeTick)
|
||||
{
|
||||
tempTimeTicker = GetCurrentTimeTick();
|
||||
}
|
||||
|
||||
return tempTimeTicker;
|
||||
}
|
||||
|
||||
|
||||
public virtual long NextId()
|
||||
{
|
||||
lock (_SyncLock)
|
||||
{
|
||||
return _IsOverCost ? NextOverCostId() : NextNormalId();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
/*
|
||||
* 版权属于:yitter(yitter@126.com)
|
||||
* 开源地址:https://gitee.com/yitter/idgenerator
|
||||
* 版权协议:MIT
|
||||
* 版权说明:只要保留本版权,你可以免费使用、修改、分发本代码。
|
||||
* 免责条款:任何因为本代码产生的系统、法律、政治、宗教问题,均与版权所有者无关。
|
||||
*
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Yitter.IdGenerator
|
||||
{
|
||||
/// <summary>
|
||||
/// 常规雪花算法
|
||||
/// </summary>
|
||||
internal class SnowWorkerM2 : SnowWorkerM1
|
||||
{
|
||||
public SnowWorkerM2(IdGeneratorOptions options) : base(options)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override long NextId()
|
||||
{
|
||||
lock (_SyncLock)
|
||||
{
|
||||
long currentTimeTick = GetCurrentTimeTick();
|
||||
|
||||
if (_LastTimeTick == currentTimeTick)
|
||||
{
|
||||
if (_CurrentSeqNumber++ > MaxSeqNumber)
|
||||
{
|
||||
_CurrentSeqNumber = MinSeqNumber;
|
||||
currentTimeTick = GetNextTimeTick();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_CurrentSeqNumber = MinSeqNumber;
|
||||
}
|
||||
|
||||
if (currentTimeTick < _LastTimeTick)
|
||||
{
|
||||
throw new Exception(string.Format("Time error for {0} milliseconds", _LastTimeTick - currentTimeTick));
|
||||
}
|
||||
|
||||
_LastTimeTick = currentTimeTick;
|
||||
var result = ((currentTimeTick << _TimestampShift) + ((long)WorkerId << SeqBitLength) + (uint)_CurrentSeqNumber);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,96 +0,0 @@
|
||||
/*
|
||||
* 版权属于:yitter(yitter@126.com)
|
||||
* 开源地址:https://gitee.com/yitter/idgenerator
|
||||
* 版权协议:MIT
|
||||
* 版权说明:只要保留本版权,你可以免费使用、修改、分发本代码。
|
||||
* 免责条款:任何因为本代码产生的系统、法律、政治、宗教问题,均与版权所有者无关。
|
||||
*
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
|
||||
namespace Yitter.IdGenerator
|
||||
{
|
||||
/// <summary>
|
||||
/// 默认实现
|
||||
/// </summary>
|
||||
public class DefaultIdGenerator : IIdGenerator
|
||||
{
|
||||
private ISnowWorker _SnowWorker { get; set; }
|
||||
|
||||
public Action<OverCostActionArg> GenIdActionAsync
|
||||
{
|
||||
get => _SnowWorker.GenAction;
|
||||
set => _SnowWorker.GenAction = value;
|
||||
}
|
||||
|
||||
|
||||
public DefaultIdGenerator(IdGeneratorOptions options)
|
||||
{
|
||||
if (options == null)
|
||||
{
|
||||
throw new ApplicationException("options error.");
|
||||
}
|
||||
|
||||
if (options.BaseTime < DateTime.Now.AddYears(-50) || options.BaseTime > DateTime.Now)
|
||||
{
|
||||
throw new ApplicationException("BaseTime error.");
|
||||
}
|
||||
|
||||
if (options.SeqBitLength + options.WorkerIdBitLength > 22)
|
||||
{
|
||||
throw new ApplicationException("error:WorkerIdBitLength + SeqBitLength <= 22");
|
||||
}
|
||||
|
||||
var maxWorkerIdNumber = Math.Pow(2, options.WorkerIdBitLength) - 1;
|
||||
if (options.WorkerId < 0 || options.WorkerId > maxWorkerIdNumber)
|
||||
{
|
||||
throw new ApplicationException("WorkerId error. (range:[1, " + maxWorkerIdNumber + "]");
|
||||
}
|
||||
|
||||
if (options.SeqBitLength < 2 || options.SeqBitLength > 21)
|
||||
{
|
||||
throw new ApplicationException("SeqBitLength error. (range:[2, 21])");
|
||||
}
|
||||
|
||||
var maxSeqNumber = Math.Pow(2, options.SeqBitLength) - 1;
|
||||
if (options.MaxSeqNumber < 0 || options.MaxSeqNumber > maxSeqNumber)
|
||||
{
|
||||
throw new ApplicationException("MaxSeqNumber error. (range:[1, " + maxSeqNumber + "]");
|
||||
}
|
||||
|
||||
var maxValue = maxSeqNumber; // maxSeqNumber - 1;
|
||||
if (options.MinSeqNumber < 1 || options.MinSeqNumber > maxValue)
|
||||
{
|
||||
throw new ApplicationException("MinSeqNumber error. (range:[1, " + maxValue + "]");
|
||||
}
|
||||
|
||||
switch (options.Method)
|
||||
{
|
||||
case 1:
|
||||
_SnowWorker = new SnowWorkerM1(options);
|
||||
break;
|
||||
case 2:
|
||||
_SnowWorker = new SnowWorkerM2(options);
|
||||
break;
|
||||
default:
|
||||
_SnowWorker = new SnowWorkerM1(options);
|
||||
break;
|
||||
}
|
||||
|
||||
if (options.Method == 1)
|
||||
{
|
||||
Thread.Sleep(500);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public long NewLong()
|
||||
{
|
||||
return _SnowWorker.NextId();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
/*
|
||||
* 版权属于:yitter(yitter@126.com)
|
||||
* 开源地址:https://gitee.com/yitter/idgenerator
|
||||
* 版权协议:MIT
|
||||
* 版权说明:只要保留本版权,你可以免费使用、修改、分发本代码。
|
||||
* 免责条款:任何因为本代码产生的系统、法律、政治、宗教问题,均与版权所有者无关。
|
||||
*
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Yitter.IdGenerator
|
||||
{
|
||||
/// <summary>
|
||||
/// 这是一个调用的例子,默认情况下,单机集成者可以直接使用 NextId()。
|
||||
/// </summary>
|
||||
public class YitIdHelper
|
||||
{
|
||||
private static IIdGenerator _IdGenInstance = null;
|
||||
|
||||
public static IIdGenerator IdGenInstance => _IdGenInstance;
|
||||
|
||||
/// <summary>
|
||||
/// 设置参数,建议程序初始化时执行一次
|
||||
/// </summary>
|
||||
/// <param name="options"></param>
|
||||
public static void SetIdGenerator(IdGeneratorOptions options)
|
||||
{
|
||||
_IdGenInstance = new DefaultIdGenerator(options);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 生成新的Id
|
||||
/// 调用本方法前,请确保调用了 SetIdGenerator 方法做初始化。
|
||||
/// 否则将会初始化一个WorkerId为1的对象。
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static long NextId()
|
||||
{
|
||||
if (_IdGenInstance == null)
|
||||
{
|
||||
_IdGenInstance = new DefaultIdGenerator(
|
||||
new IdGeneratorOptions() { WorkerId = 1 }
|
||||
);
|
||||
}
|
||||
|
||||
return _IdGenInstance.NewLong();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<LangVersion>latest</LangVersion>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<Description>Shorter ID and faster generation with a new snowflake drift algorithm. The core is to shorten the ID length, but also can have a very high instantaneous concurrent processing capacity (50W/0.1s), and powerful configuration capacity.
|
||||
|
||||
一种全新的雪花漂移算法,让ID更短、生成速度更快。 核心在于缩短ID长度的同时,还能拥有极高瞬时并发处理量(50W/0.1s),及强大的配置能力。</Description>
|
||||
<AssemblyName>Yitter.IdGenerator</AssemblyName>
|
||||
<RootNamespace>Yitter.IdGenerator</RootNamespace>
|
||||
<Product>IdGenerator</Product>
|
||||
<Company>Yitter</Company>
|
||||
<Authors>Yitter</Authors>
|
||||
<Copyright>Yitter</Copyright>
|
||||
<PackageProjectUrl>https://gitee.com/yitter/idgenerator</PackageProjectUrl>
|
||||
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||
<Version>1.0.8</Version>
|
||||
<PackageReleaseNotes></PackageReleaseNotes>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
||||
<WarningLevel>5</WarningLevel>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user