Cat.Util/Cat.IdGenerator/SnowWorkerM2.cs

(完整代码) 1KB

using System;
using System.Collections.Generic;
using System.Text;

namespace Cat.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;
         }
      }

   }
}

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。