C# 生成雪花ID和GUID

前言

雪花ID是用一个64位的整形数字来做ID,对应.net中的long,数据库中的bigint,雪花算法的原始版本是scala版,用于生成分布式主键ID(纯数字,时间顺序),订单编号等。

自增ID:对于数据敏感场景不宜使用,且不适合于分布式场景。
GUID:采用无意义字符串,数据量增大时造成访问过慢,且不宜排序。

生成GUID

var UUID = Guid.NewGuid().ToString("N").ToUpper(); 

生成雪花ID

var ids = new IDCreater();
    ids.NextID();

雪花ID类代码

  public class IDCreater
    {
        private const long Send = 1576492637564;//初始种子

        //时间戳41位+序列10位+机器码10位+随机3位

        private static int IndexBits = 10;//序列位
        private static int WorkerBits = 10;//机器位
        private static int RandomBits = 3;//随机位

        private static long Index = 0;
        private static long Worker = 0;

        private static long IndexMax = -1 ^ (-1 << IndexBits);
        public static long WorkerMax = -1 ^ (-1 << WorkerBits);
        private static long RandomMax = -1 ^ (-1 << RandomBits);

        // 机器码偏左移13位
        private static int WorkerShift = RandomBits;
        // 序列位偏左移3位
        private static int IndexShift = WorkerBits + RandomBits;
        // 时间毫秒左移23位
        private static int TimeStampShift = IndexBits + WorkerBits + RandomBits;

        private static Random r = new Random(Guid.NewGuid().GetHashCode());

        private static long LastTimeStamp = 0;

        / <summary>
        / 初始化自行分配机器码
        / </summary>
        //public IDCreater(long Worker = 0)
        //{
        //    if (Worker > WorkerMax || Worker < 0)
        //        throw new Exception(string.Format("机器位只能是 0 至 {0}之间", WorkerMax));

        //    IDCreater.Worker = Worker;
        //}




        /// <summary>
        /// 生成一个ID
        /// </summary>
        /// <returns></returns>
        public long NextID()
        {
            lock (this)
            {
                long ts = GetTimeStamp();
                if (ts == LastTimeStamp)
                {
                    Index = (Index + 1) & IndexMax;
                    if (Index == 0) ts = NextTimeStamp();
                }
                else
                {
                    Index = 0;
                }

                if (ts < LastTimeStamp)
                {
                    Thread.Sleep((int)(LastTimeStamp - ts));
                    //throw new Exception("NTP 回拨时间");
                }
                LastTimeStamp = ts;
                //long random = r.Next(0, (int)RandomMax);

                return ((ts - Send) << TimeStampShift) | (Index << IndexShift) | (Worker << WorkerShift);
            }
        }
        /// <summary>
        /// 阻塞到下一个毫秒,直到获得新的时间
        /// </summary>
        /// <returns></returns>
        private long NextTimeStamp()
        {
            long ts = GetTimeStamp();
            while (ts <= LastTimeStamp)
            {
                ts = GetTimeStamp();
            }
            return ts;
        }

        private static DateTime UTC0 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
        /// <summary>
        /// 获取当前时间
        /// </summary>
        /// <returns></returns>
        private static long GetTimeStamp()
        {
            return (long)(DateTime.UtcNow - UTC0).TotalMilliseconds;
        }
    }
  • 16
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

润小仙女

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值