chore: 提交现有修改

This commit is contained in:
2025-12-02 12:11:25 +08:00
parent 541b75ecd8
commit 5332c87d9d
37 changed files with 429 additions and 677 deletions

View File

@@ -8,7 +8,12 @@ namespace TakeoutSaaS.Shared.Kernel.Ids;
/// <summary>
/// 基于雪花算法的长整型 ID 生成器。
/// </summary>
public sealed class SnowflakeIdGenerator : IIdGenerator
/// <remarks>
/// 初始化生成器。
/// </remarks>
/// <param name="workerId">工作节点 ID。</param>
/// <param name="datacenterId">机房 ID。</param>
public sealed class SnowflakeIdGenerator(long workerId = 0, long datacenterId = 0) : IIdGenerator
{
private const long Twepoch = 1577836800000L; // 2020-01-01 UTC
private const int WorkerIdBits = 5;
@@ -23,23 +28,12 @@ public sealed class SnowflakeIdGenerator : IIdGenerator
private const int TimestampLeftShift = SequenceBits + WorkerIdBits + DatacenterIdBits;
private const long SequenceMask = -1L ^ (-1L << SequenceBits);
private readonly long _workerId;
private readonly long _datacenterId;
private readonly long _workerId = Normalize(workerId, MaxWorkerId, nameof(workerId));
private readonly long _datacenterId = Normalize(datacenterId, MaxDatacenterId, nameof(datacenterId));
private long _lastTimestamp = -1L;
private long _sequence;
private long _sequence = RandomNumberGenerator.GetInt32(0, (int)SequenceMask);
private readonly object _syncRoot = new();
/// <summary>
/// 初始化生成器。
/// </summary>
/// <param name="workerId">工作节点 ID。</param>
/// <param name="datacenterId">机房 ID。</param>
public SnowflakeIdGenerator(long workerId = 0, long datacenterId = 0)
{
_workerId = Normalize(workerId, MaxWorkerId, nameof(workerId));
_datacenterId = Normalize(datacenterId, MaxDatacenterId, nameof(datacenterId));
_sequence = RandomNumberGenerator.GetInt32(0, (int)SequenceMask);
}
/// <inheritdoc />
public long NextId()