ID 生成器
using System;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Threading; /// <summary>
/// ID 生成器
/// </summary>
public class IDFactory
{
#region Static Fields /// <summary>
/// The factory.
/// </summary>
private static IDFactory factory; #endregion #region Fields /// <summary>
/// The init millisecond.
/// </summary>
private readonly ulong InitMillisecond; /// <summary>
/// The m watch.
/// </summary>
private readonly Stopwatch mWatch = new Stopwatch(); /// <summary>
/// The m current millisecond.
/// </summary>
private ulong mCurrentMillisecond; /// <summary>
/// The m seed.
/// </summary>
private byte mSeed; #endregion #region Constructors and Destructors /// <summary>
/// Initializes a new instance of the <see cref="IDFactory" /> class.
/// </summary>
public IDFactory()
{
this.InitMillisecond = (ulong)(DateTime.Now - DateTime.Parse("2015-1-1")).TotalMilliseconds;
this.mWatch.Restart();
} static IDFactory()
{
LoadIPGroup();
} #endregion #region Public Properties /// <summary>
/// Gets or sets the group.
/// </summary>
public static byte Group { get; set; } #endregion #region Public Methods and Operators /// <summary>
/// 生成 ID 种子
/// </summary>
/// <returns>返回ID种子。参见<see cref="long" /></returns>
public static long Create()
{
if (factory == null)
{
factory = new IDFactory();
} return (long)factory.Next();
} /// <summary>
/// 获取IP
/// </summary>
public static void LoadIPGroup()
{
string hostName = Dns.GetHostName();
IPAddress[] addressList = Dns.GetHostAddresses(hostName);
foreach (IPAddress ip in addressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{ byte[] data = ip.GetAddressBytes();
uint value = BitConverter.ToUInt32(data, );
value = value << ;
value = value >> ;
Group = (byte)value;
break;
}
}
} /// <summary>
/// 生成ID种子
/// </summary>
/// <returns>
/// The <see cref="ulong" />.
/// </returns>
public ulong Next()
{
ulong result = ;
var slock = new SpinLock();
bool gotLock = false;
try
{
while (!gotLock)
{
slock.Enter(ref gotLock);
if (gotLock)
{
ulong cms = (ulong)this.mWatch.Elapsed.TotalMilliseconds + this.InitMillisecond;
if (cms != this.mCurrentMillisecond)
{
this.mSeed = ;
this.mCurrentMillisecond = cms;
} //result = ((ulong)this.Group << 58) | (this.mCurrentMillisecond << 8) | this.mSeed;
//result = ((ulong)Group << 9) | (this.mCurrentMillisecond << 17) | this.mSeed;
//result = this.mCurrentMillisecond * 1000000 + (ulong)Group * 1000 + this.mSeed;
result = this.mCurrentMillisecond * + (ulong)Group * + this.mSeed;
this.mSeed++;
}
}
}
finally
{
if (gotLock)
{
slock.Exit();
}
} return result;
} #endregion
}
ID 生成器的更多相关文章
- 分布式的Id生成器
项目中需要一个分布式的Id生成器,twitter的Snowflake中这个既简单又高效,网上找的Java版本 package com.cqfc.id; import org.slf4j.Logger; ...
- 分布式ID生成器 zz
简介 这个是根据twitter的snowflake来写的.这里有中文的介绍. 如上图所示,一个64位ID,除了最左边的符号位不用(固定为0,以保证生成的ID都是正数),还剩余63位可用. 下面的代码与 ...
- IOS ID生成器
// // IdGenerator.m // Copyright (c) 2014年 青岛拓宇网络科技有限公司. All rights reserved. // #import "IdGen ...
- 分布式ID生成器
最近会写一篇分布式的ID生成器的文章,先占位.借鉴Mongodb的ObjectId的生成: 4byte时间戳 + 3byte机器标识 + 2byte PID + 3byte自增id 简单代码: imp ...
- 游戏服务器ID生成器组件
游戏服务器程序中,经常需要生成全局的唯一ID号,这个功能很常用,本文将介绍一种通用ID生成组件.游戏服务器程序中使用此组件的场景有: 创建角色时,为其分配唯一ID 创建物品时,每个物品需要唯一ID 创 ...
- ID生成器的一种可扩展实现方案
ID生成器主要为了解决业务程序生成记录ID的场景,而一个好的ID生成器肯定要满足扩展性好.并发性好的特点,本文下面介绍一种满足上述特点的实现方案. 此方案的核心思想是:每次需要扩容机器时,将每个节点维 ...
- ID生成器详解
概述 ID 生成器也叫发号器,它的主要目的就是"为一个分布式系统的数据object产生一个唯一的标识",但其实在一个真实的系统里可能也可以承担更多的作用.概括起来主要有以下几点: ...
- 业务系统需要什么样的ID生成器
业务系统需要什么样的ID生成器 ID 生成器在微博我们一直叫发号器,微博就是用这样的号来存储,而我微博里讨论的时候也都是以发号器为标签.它的主要目的确如平常大家理解的“为一个分布式系统的数据objec ...
- twitter的ID生成器的snowFlake算法的自造版
snowFlake算法在生成ID时特别高效,可参考:https://segmentfault.com/a/1190000011282426 SnowFlake算法生成id的结果是一个64bit大小的整 ...
- snowflake 分布式唯一ID生成器
本文来自我的github pages博客http://galengao.github.io/ 即www.gaohuirong.cn 摘要: 原文参考运维生存和开源中国上的代码整理 我的环境是pytho ...
随机推荐
- .net 数据类型转换int.Parse()与int.TryParse
int.Parse()是一种类容转换:表示将数字内容的字符串转为int类型. 如果字符串为空,则抛出ArgumentNullException异常: 如果字符串内容不是数字,则抛出FormatExce ...
- [转]MySQL修改时区的方法小结
本文转自:https://www.cnblogs.com/mracale/p/6064447.html 这篇文章主要介绍了MySQL修改时区的方法,总结分析了三种常见的MySQL时区修改技巧,包括命令 ...
- HangFire循环作业中作业因执行时间太长未完成新作业开启导致重复数据的问题
解决方法:在执行的任务方法前加上Mutex特性即可,如果作业未完成,新作业开启的话,新作业会放入计划中的作业队列中,直到前面的作业完成. 必须使用Hangfire.Pro.Redis 和 Hangfi ...
- 线程 线程池 Task
首先声明 这是读了 愉悦的绅士 文章 <菜鸟之旅——学习线程(线程和线程池)> <Task与线程> 的一些个人总结,还是那句话,如有不对,欢迎指正 文章以代码加注释的方法展示. ...
- js 数据类型具体分析
复习 点运算符 xxx.sss xxx是对象 sss是属性和方法.任何数据类型都是拥有属性和方法的.字符串 String var st=“hello world”.字符串的定义 ...
- 2018年最值得关注的30个Vue开源项目
译者按: 学习优秀的开源项目是提高代码水平最有效的方式. 原文: 30 Amazing Vue.js Open Source Projects for the Past Year (v.2018) 译 ...
- 2018-10-19 Chrome插件实现GitHub代码离线翻译v0.0.4
续前文Chrome插件实现GitHub代码翻译v0.0.3. 添加了对驼峰命名的支持. 由于调用浏览器插件-离线英汉词典进行词汇翻译, 因此也不依赖于任何在线翻译服务. Chrome插件: 官网链接 ...
- AI从业者需要应用的10种深度学习方法
https://zhuanlan.zhihu.com/p/43636528 https://zhuanlan.zhihu.com/p/43734896 摘要:想要了解人工智能,不知道这十种深度学习方法 ...
- 基于Log4j完成定时创建和删除日志的方法
文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.背景 Log4j作为常用的日志生成工具,其清除日志的策略却十分有限. ...
- LVS + nginx实现高性能精准负载均衡