1
0
mirror of synced 2026-04-22 08:18:40 +08:00

code_editreadme

This commit is contained in:
zhouzj
2021-03-12 22:21:43 +08:00
parent acc40c7969
commit 761020f3eb
12 changed files with 54 additions and 29 deletions

View File

@@ -58,7 +58,7 @@
1.js Number 类型最大数值9007199254740992本算法在保持并发性能5W+/0.01s和最大64个 WorkerId6bit的同时能用70年才到 js Number Max 值。
2.增加WorkerId位数到8bit128节点15年达到 js Number Max 值。
2.增加WorkerId位数到8bit256节点15年达到 js Number Max 值。
3.极致性能500W/1s。
@@ -167,14 +167,14 @@
```
// 全局初始化设置WorkerId默认最大2^16-1。初始化过程全局只需一次且必须最先设置
var options = new IdGeneratorOptions(){ WorkerId = 1};
YidHelper.SetIdGenerator(options);
IdHelper.SetIdGenerator(options);
// 初始化以后就可以在需要的地方调用方法生成ID。
var newId = YidHelper.NextId();
var newId = IdHelper.NextId();
// 可通过 YidHelper.IdGenInstance 订阅 GenIdActionAsync 事件。
// 可通过 IdHelper.IdGenInstance 订阅 GenIdActionAsync 事件。
```
如果基于DI框架集成可以参考 YidHelper 去管理 IdGenerator 对象,必须使用**单例**模式。
如果基于DI框架集成可以参考 IdHelper 去管理 IdGenerator 对象,必须使用**单例**模式。
#### options说明
```

View File

@@ -44,13 +44,13 @@ namespace Yitter.OrgSystem.TestA
WorkerId = 1,
TopOverCostCount = 2000,
//WorkerIdBitLength = 6,
//SeqBitLength = 6,
WorkerIdBitLength = 10,
SeqBitLength = 6,
//MinSeqNumber = 11,
//MaxSeqNumber = 200,
//StartTime = DateTime.Now.AddYears(-1),
StartTime = DateTime.Now.AddYears(-5),
};
// ++++++++++++++++++++++++++++++++
@@ -59,7 +59,7 @@ namespace Yitter.OrgSystem.TestA
IdGeneratorOptions options1 = (newConfig);
if (IdGen == null)
{
IdGen = new YidGenerator(options1);
IdGen = new DefaultIdGenerator(options1);
}
if (outputLog)
@@ -105,7 +105,7 @@ namespace Yitter.OrgSystem.TestA
};
Console.WriteLine("Gen" + i);
var idGen2 = new YidGenerator(options);
var idGen2 = new DefaultIdGenerator(options);
var test = new GenTest(idGen2, genIdCount, i);
if (outputLog)

View File

@@ -13,8 +13,15 @@ namespace Yitter.IdGenerator
{
public interface IIdGenerator
{
/// <summary>
/// 生成过程中产生的事件
/// </summary>
Action<OverCostActionArg> GenIdActionAsync { get; set; }
/// <summary>
/// 生成新的long型Id
/// </summary>
/// <returns></returns>
long NewLong();
// Guid NewGuid();

View File

@@ -13,13 +13,35 @@ using System.Text;
namespace Yitter.IdGenerator
{
/// <summary>
/// Id生成时回调参数
/// </summary>
public class OverCostActionArg
{
/// <summary>
/// 事件类型
/// 1-开始2-结束8-漂移
/// </summary>
public int ActionType { get; set; }
/// <summary>
/// 时间戳
/// </summary>
public long TimeTick { get; set; }
/// <summary>
/// 机器码
/// </summary>
public ushort WorkerId { get; set; }
/// <summary>
/// 漂移计算次数
/// </summary>
public int OverCostCountInOneTerm { get; set; }
/// <summary>
/// 漂移期间生产ID个数
/// </summary>
public int GenCountInOneTerm { get; set; }
/// <summary>
/// 漂移周期
/// </summary>
public int TermIndex { get; set; }
public OverCostActionArg(ushort workerId, long timeTick, int actionType = 0, int overCostCountInOneTerm = 0, int genCountWhenOverCost = 0,int index=0)

View File

@@ -14,7 +14,10 @@ using System.Threading;
namespace Yitter.IdGenerator
{
public class YidGenerator : IIdGenerator
/// <summary>
/// 默认实现
/// </summary>
public class DefaultIdGenerator : IIdGenerator
{
private ISnowWorker _SnowWorker { get; set; }
@@ -25,7 +28,7 @@ namespace Yitter.IdGenerator
}
public YidGenerator(IdGeneratorOptions options)
public DefaultIdGenerator(IdGeneratorOptions options)
{
if (options == null)
{
@@ -45,24 +48,24 @@ namespace Yitter.IdGenerator
var maxWorkerIdNumber = Math.Pow(2, options.WorkerIdBitLength) - 1;
if (options.WorkerId < 1 || options.WorkerId > maxWorkerIdNumber)
{
throw new ApplicationException("WorkerId is error. (range:[1, " + maxWorkerIdNumber + "]");
throw new ApplicationException("WorkerId error. (range:[1, " + maxWorkerIdNumber + "]");
}
if (options.SeqBitLength < 2 || options.SeqBitLength > 21)
{
throw new ApplicationException("SeqBitLength is error. (range:[2, 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 is error. (range:[1, " + maxSeqNumber + "]");
throw new ApplicationException("MaxSeqNumber error. (range:[1, " + maxSeqNumber + "]");
}
var maxValue = maxSeqNumber - 2;
if (options.MinSeqNumber < 5 || options.MinSeqNumber > maxValue)
{
throw new ApplicationException("MinSeqNumber is error. (range:[5, " + maxValue + "]");
throw new ApplicationException("MinSeqNumber error. (range:[5, " + maxValue + "]");
}
switch (options.Method)

View File

@@ -16,17 +16,11 @@ namespace Yitter.IdGenerator
/// <summary>
/// 这是一个调用的例子,默认情况下,单机集成者可以直接使用 NewId()。
/// </summary>
public class YidHelper
public class IdHelper
{
private static IIdGenerator _IdGenInstance = null;
public static IIdGenerator IdGenInstance
{
get
{
return _IdGenInstance;
}
}
public static IIdGenerator IdGenInstance => _IdGenInstance;
/// <summary>
/// 设置参数,建议程序初始化时执行一次
@@ -34,7 +28,7 @@ namespace Yitter.IdGenerator
/// <param name="options"></param>
public static void SetIdGenerator(IdGeneratorOptions options)
{
_IdGenInstance = new YidGenerator(options);
_IdGenInstance = new DefaultIdGenerator(options);
}
/// <summary>
@@ -47,7 +41,9 @@ namespace Yitter.IdGenerator
{
if (_IdGenInstance == null)
{
_IdGenInstance = new YidGenerator(new IdGeneratorOptions() { WorkerId = 1 });
_IdGenInstance = new DefaultIdGenerator(
new IdGeneratorOptions() { WorkerId = 1 }
);
}
return _IdGenInstance.NewLong();

View File

@@ -12,7 +12,4 @@
<WarningLevel>5</WarningLevel>
</PropertyGroup>
<ItemGroup>
</ItemGroup>
</Project>