c# 雪花唯一id 生成器 代码

 /// <summary>
 /// 生成数据库主键Id
 /// </summary>
 public class IdGeneratorHelper
 {
     private int SnowFlakeWorkerId = GlobalContext.SystemConfig.SnowFlakeWorkerId;

     private Snowflake snowflake;

     private static readonly IdGeneratorHelper instance = new IdGeneratorHelper();

     private IdGeneratorHelper()
     {
         snowflake = new Snowflake(SnowFlakeWorkerId, 0, 0);
     }
     public static IdGeneratorHelper Instance
     {
         get
         {
             return instance;
         }
     }
     public long GetId()
     {
         return snowflake.NextId();
     }
 }



  public class Snowflake
  {
      private const long TwEpoch = 1546272000000L;//2019-01-01 00:00:00

      private const int WorkerIdBits = 5;
      private const int DatacenterIdBits = 5;
      private const int SequenceBits = 12;
      private const long MaxWorkerId = -1L ^ (-1L << WorkerIdBits);
      private const long MaxDatacenterId = -1L ^ (-1L << DatacenterIdBits);

      private const int WorkerIdShift = SequenceBits;
      private const int DatacenterIdShift = SequenceBits + WorkerIdBits;
      private const int TimestampLeftShift = SequenceBits + WorkerIdBits + DatacenterIdBits;
      private const long SequenceMask = -1L ^ (-1L << SequenceBits);

      private long _sequence = 0L;
      private long _lastTimestamp = -1L;
      /// <summary>
      ///10位的数据机器位中的高位
      /// </summary>
      public long WorkerId { get; protected set; }
      /// <summary>
      /// 10位的数据机器位中的低位
      /// </summary>
      public long DatacenterId { get; protected set; }

      private readonly object _lock = new object();
      /// <summary>
      /// 基于Twitter的snowflake算法
      /// </summary>
      /// <param name="workerId">10位的数据机器位中的高位,默认不应该超过5位(5byte)</param>
      /// <param name="datacenterId"> 10位的数据机器位中的低位,默认不应该超过5位(5byte)</param>
      /// <param name="sequence">初始序列</param>
      public Snowflake(long workerId, long datacenterId, long sequence = 0L)
      {
          WorkerId = workerId;
          DatacenterId = datacenterId;
          _sequence = sequence;

          if (workerId > MaxWorkerId || workerId < 0)
          {
              throw new ArgumentException($"worker Id can't be greater than {MaxWorkerId} or less than 0");
          }

          if (datacenterId > MaxDatacenterId || datacenterId < 0)
          {
              throw new ArgumentException($"datacenter Id can't be greater than {MaxDatacenterId} or less than 0");
          }
      }

      public long CurrentId { get; private set; }

      /// <summary>
      /// 获取下一个Id,该方法线程安全
      /// </summary>
      /// <returns></returns>
      public long NextId()
      {
          lock (_lock)
          {
              var timestamp = DateTimeHelper.GetUnixTimeStamp(DateTime.Now);
              if (timestamp < _lastTimestamp)
              {
                  //TODO 是否可以考虑直接等待?
                  throw new Exception(
                      $"Clock moved backwards or wrapped around. Refusing to generate id for {_lastTimestamp - timestamp} ticks");
              }

              if (_lastTimestamp == timestamp)
              {
                  _sequence = (_sequence + 1) & SequenceMask;
                  if (_sequence == 0)
                  {
                      timestamp = TilNextMillis(_lastTimestamp);
                  }
              }
              else
              {
                  _sequence = 0;
              }
              _lastTimestamp = timestamp;
              CurrentId = ((timestamp - TwEpoch) << TimestampLeftShift) |
                       (DatacenterId << DatacenterIdShift) |
                       (WorkerId << WorkerIdShift) | _sequence;

              return CurrentId;
          }
      }

      private long TilNextMillis(long lastTimestamp)
      {
          var timestamp = DateTimeHelper.GetUnixTimeStamp(DateTime.Now);
          while (timestamp <= lastTimestamp)
          {
              timestamp = DateTimeHelper.GetUnixTimeStamp(DateTime.Now);
          }
          return timestamp;
      }
  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值