From 7c905310344308c8ed3ea714be616ce11e667d33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=87=92=E5=BE=97=E5=8B=A4=E5=BF=AB?= Date: Thu, 31 Aug 2023 23:31:36 +0800 Subject: [PATCH] release --- .../Masuit.Tools.Abstractions.csproj | 2 +- .../Systems/SnowFlake.cs | 289 +++++++++--------- .../Systems/SnowFlakeNew.cs | 211 ++++++------- .../Masuit.Tools.AspNetCore.csproj | 2 +- Masuit.Tools.Core/Masuit.Tools.Core.csproj | 2 +- Masuit.Tools.Net45/Masuit.Tools.Net45.csproj | 73 ++++- Masuit.Tools.Net45/package.nuspec | 2 +- Masuit.Tools/package.nuspec | 2 +- .../Masuit.Tools.Abstractions.Test.csproj | 2 +- .../Masuit.Tools.Core.Test.csproj | 2 +- 10 files changed, 333 insertions(+), 254 deletions(-) diff --git a/Masuit.Tools.Abstractions/Masuit.Tools.Abstractions.csproj b/Masuit.Tools.Abstractions/Masuit.Tools.Abstractions.csproj index 5cfe4926..b9d3277c 100644 --- a/Masuit.Tools.Abstractions/Masuit.Tools.Abstractions.csproj +++ b/Masuit.Tools.Abstractions/Masuit.Tools.Abstractions.csproj @@ -3,7 +3,7 @@ netstandard2.0;netstandard2.1;net461;net5;net6;net7 latest true - 2.6.6.2 + 2.6.6.3 懒得勤快 新手友好的C#万能工具库,码数吐司库,Masuit.Tools基础公共库(适用于.NET4.6.1/.NET Standard2.0及以上项目),包含一些常用的操作类,大都是静态类,加密解密,反射操作,Excel简单导出,权重随机筛选算法,分布式短id,表达式树,linq扩展,文件压缩,多线程下载和FTP客户端,硬件信息,字符串扩展方法,日期时间扩展操作,中国农历,大文件拷贝,图像裁剪,验证码,断点续传,集合扩展等常用封装。 官网教程:https://tools.masuit.org diff --git a/Masuit.Tools.Abstractions/Systems/SnowFlake.cs b/Masuit.Tools.Abstractions/Systems/SnowFlake.cs index 5aecb1e9..f3c1f6c1 100644 --- a/Masuit.Tools.Abstractions/Systems/SnowFlake.cs +++ b/Masuit.Tools.Abstractions/Systems/SnowFlake.cs @@ -6,146 +6,151 @@ namespace Masuit.Tools.Systems { - /// - /// 动态生产有规律的分布式ID - /// - public class SnowFlake - { - #region 私有字段 - - private static long _machineId; //机器码 - private static long _sequence; //计数从零开始 - private static long _lastTimestamp = -1L; //最后时间戳 - - private const long Twepoch = 687888001020L; //唯一时间随机量 - - private const int MachineBits = 10; //机器码字节数 - - private const int SequenceBits = 12; //计数器字节数,12个字节用来保存计数码 - private const int MachineLeft = SequenceBits; //机器码数据左移位数,就是后面计数器占用的位数 - private const long TimestampLeft = MachineBits + SequenceBits; //时间戳左移动位数就是机器码+计数器总字节数+数据字节数 - private const long SequenceMask = -1L ^ -1L << SequenceBits; //一毫秒内可以产生计数,如果达到该值则等到下一毫秒在进行生成 - - private static readonly object SyncRoot = new object(); //加锁对象 - private static NumberFormater _numberFormater = new NumberFormater(36); - private static SnowFlake _snowFlake; - - #endregion 私有字段 - - /// - /// 获取一个新的id - /// - public static string NewId => GetInstance().GetUniqueId(); - - /// - /// 创建一个实例 - /// - /// - public static SnowFlake GetInstance() - { - return _snowFlake ??= new SnowFlake(); - } - - /// - /// 默认构造函数 - /// - public SnowFlake() - { - var bytes = NetworkInterface.GetAllNetworkInterfaces().FirstOrDefault().GetPhysicalAddress().GetAddressBytes(); - Snowflakes(bytes[4] << 4 | bytes[5]); - } - - /// - /// 构造函数 - /// - /// 机器码 - public SnowFlake(long machineId) - { - Snowflakes(machineId); - } - - private void Snowflakes(long machineId) - { - if (machineId >= 0) - { - if (machineId > 1024) - { - throw new Exception("机器码ID非法"); - } - - _machineId = machineId; - } - } - - /// - /// 设置数制格式化器 - /// - /// - public static void SetNumberFormater(NumberFormater nf) - { - _numberFormater = nf; - } - - /// - /// 获取长整形的ID - /// - /// - public long GetLongId() - { - lock (SyncRoot) - { - var timestamp = DateTime.UtcNow.GetTotalMilliseconds(); - if (_lastTimestamp == timestamp) - { - //同一毫秒中生成ID - _sequence = (_sequence + 1) & SequenceMask; //用&运算计算该毫秒内产生的计数是否已经到达上限 - if (_sequence == 0) - { - //一毫秒内产生的ID计数已达上限,等待下一毫秒 - timestamp = DateTime.UtcNow.GetTotalMilliseconds(); - } - } - else - { - //不同毫秒生成ID - _sequence = 0L; - } - - _lastTimestamp = timestamp; //把当前时间戳保存为最后生成ID的时间戳 - long id = ((timestamp - Twepoch) << (int)TimestampLeft) | (_machineId << MachineLeft) | _sequence; - return id; - } - } - - /// - /// 获取一个字符串表示形式的id - /// - /// - public string GetUniqueId() - { - return _numberFormater.ToString(GetLongId()); - } - - /// - /// 获取一个字符串表示形式的id - /// - /// 最大长度,至少6位 - /// - public string GetUniqueShortId(int maxLength = 8) - { - if (maxLength < 6) - { - throw new ArgumentException("最大长度至少需要6位"); - } - - string id = GetUniqueId(); - int index = id.Length - maxLength; - if (index < 0) - { - index = 0; - } - - return id.Substring(index); - } - } + /// + /// 动态生产有规律的分布式ID + /// + public class SnowFlake + { + #region 私有字段 + + private static long _machineId; //机器码 + private static long _sequence; //计数从零开始 + private static long _lastTimestamp = -1L; //最后时间戳 + + private const long Twepoch = 687888001020L; //唯一时间随机量 + + private const int MachineBits = 10; //机器码字节数 + + private const int SequenceBits = 12; //计数器字节数,12个字节用来保存计数码 + private const int MachineLeft = SequenceBits; //机器码数据左移位数,就是后面计数器占用的位数 + private const long TimestampLeft = MachineBits + SequenceBits; //时间戳左移动位数就是机器码+计数器总字节数+数据字节数 + private const long SequenceMask = -1L ^ -1L << SequenceBits; //一毫秒内可以产生计数,如果达到该值则等到下一毫秒在进行生成 + + private static readonly object SyncRoot = new object(); //加锁对象 + private static NumberFormater _numberFormater = new NumberFormater(36); + private static SnowFlake _snowFlake; + + #endregion 私有字段 + + /// + /// 获取一个新的id + /// + public static string NewId => GetInstance().GetUniqueId(); + + /// + /// 获取一个新的id + /// + public static long LongId => GetInstance().GetLongId(); + + /// + /// 创建一个实例 + /// + /// + public static SnowFlake GetInstance() + { + return _snowFlake ??= new SnowFlake(); + } + + /// + /// 默认构造函数 + /// + public SnowFlake() + { + var bytes = NetworkInterface.GetAllNetworkInterfaces().FirstOrDefault().GetPhysicalAddress().GetAddressBytes(); + Snowflakes(bytes[4] << 4 | bytes[5]); + } + + /// + /// 构造函数 + /// + /// 机器码 + public SnowFlake(long machineId) + { + Snowflakes(machineId); + } + + private void Snowflakes(long machineId) + { + if (machineId >= 0) + { + if (machineId > 1024) + { + throw new Exception("机器码ID非法"); + } + + _machineId = machineId; + } + } + + /// + /// 设置数制格式化器 + /// + /// + public static void SetNumberFormater(NumberFormater nf) + { + _numberFormater = nf; + } + + /// + /// 获取长整形的ID + /// + /// + public long GetLongId() + { + lock (SyncRoot) + { + var timestamp = DateTime.UtcNow.GetTotalMilliseconds(); + if (_lastTimestamp == timestamp) + { + //同一毫秒中生成ID + _sequence = (_sequence + 1) & SequenceMask; //用&运算计算该毫秒内产生的计数是否已经到达上限 + if (_sequence == 0) + { + //一毫秒内产生的ID计数已达上限,等待下一毫秒 + timestamp = DateTime.UtcNow.GetTotalMilliseconds(); + } + } + else + { + //不同毫秒生成ID + _sequence = 0L; + } + + _lastTimestamp = timestamp; //把当前时间戳保存为最后生成ID的时间戳 + long id = ((timestamp - Twepoch) << (int)TimestampLeft) | (_machineId << MachineLeft) | _sequence; + return id; + } + } + + /// + /// 获取一个字符串表示形式的id + /// + /// + public string GetUniqueId() + { + return _numberFormater.ToString(GetLongId()); + } + + /// + /// 获取一个字符串表示形式的id + /// + /// 最大长度,至少6位 + /// + public string GetUniqueShortId(int maxLength = 8) + { + if (maxLength < 6) + { + throw new ArgumentException("最大长度至少需要6位"); + } + + string id = GetUniqueId(); + int index = id.Length - maxLength; + if (index < 0) + { + index = 0; + } + + return id.Substring(index); + } + } } diff --git a/Masuit.Tools.Abstractions/Systems/SnowFlakeNew.cs b/Masuit.Tools.Abstractions/Systems/SnowFlakeNew.cs index a400099c..1ecd6225 100644 --- a/Masuit.Tools.Abstractions/Systems/SnowFlakeNew.cs +++ b/Masuit.Tools.Abstractions/Systems/SnowFlakeNew.cs @@ -11,117 +11,122 @@ namespace Masuit.Tools.Systems; /// public class SnowFlakeNew { - private readonly long _workerId; //机器ID - private const long Twepoch = 1692079923000L; //唯一时间随机量 - private static long _sequence; - private const int SequenceBits = 12; //计数器字节数,10个字节用来保存计数码 - private const long SequenceMask = -1L ^ -1L << SequenceBits; //一微秒内可以产生计数,如果达到该值则等到下一微妙在进行生成 - private static long _lastTimestamp = -1L; - private static readonly object LockObj = new object(); - private static NumberFormater _numberFormater = new NumberFormater(36); - private static SnowFlakeNew _snowFlake; + private readonly long _workerId; //机器ID + private const long Twepoch = 1692079923000L; //唯一时间随机量 + private static long _sequence; + private const int SequenceBits = 12; //计数器字节数,10个字节用来保存计数码 + private const long SequenceMask = -1L ^ -1L << SequenceBits; //一微秒内可以产生计数,如果达到该值则等到下一微妙在进行生成 + private static long _lastTimestamp = -1L; + private static readonly object LockObj = new object(); + private static NumberFormater _numberFormater = new NumberFormater(36); + private static SnowFlakeNew _snowFlake; - /// - /// 获取一个新的id - /// - public static string NewId => GetInstance().GetUniqueId(); + /// + /// 获取一个新的id + /// + public static string NewId => GetInstance().GetUniqueId(); - /// - /// 创建一个实例 - /// - /// - public static SnowFlakeNew GetInstance() - { - return _snowFlake ??= new SnowFlakeNew(); - } + /// + /// 获取一个新的id + /// + public static long LongId => GetInstance().GetLongId(); - /// - /// 默认构造函数 - /// - public SnowFlakeNew() - { - var bytes = NetworkInterface.GetAllNetworkInterfaces().FirstOrDefault().GetPhysicalAddress().GetAddressBytes(); - _workerId = bytes[4] << 4 | bytes[5]; - } + /// + /// 创建一个实例 + /// + /// + public static SnowFlakeNew GetInstance() + { + return _snowFlake ??= new SnowFlakeNew(); + } - /// - /// 构造函数 - /// - /// 机器码 - public SnowFlakeNew(int machineId) - { - _workerId = machineId; - } + /// + /// 默认构造函数 + /// + public SnowFlakeNew() + { + var bytes = NetworkInterface.GetAllNetworkInterfaces().FirstOrDefault().GetPhysicalAddress().GetAddressBytes(); + _workerId = bytes[4] << 4 | bytes[5]; + } - /// - /// 设置数制格式化器 - /// - /// - public static void SetNumberFormater(NumberFormater nf) - { - _numberFormater = nf; - } + /// + /// 构造函数 + /// + /// 机器码 + public SnowFlakeNew(int machineId) + { + _workerId = machineId; + } - public long GetLongId() - { - lock (LockObj) - { - long timestamp = DateTime.Now.GetTotalMilliseconds(); - if (_lastTimestamp == timestamp) - { //同一微妙中生成ID - _sequence = (_sequence + 1) & SequenceMask; //用&运算计算该微秒内产生的计数是否已经到达上限 - if (_sequence == 0) - { - //一微妙内产生的ID计数已达上限,等待下一微妙 - timestamp = DateTime.Now.GetTotalMilliseconds(); - while (timestamp <= _lastTimestamp) - { - timestamp = DateTime.Now.GetTotalMilliseconds(); - } - return timestamp; - } - } - else - { //不同微秒生成ID - _sequence = 0; //计数清0 - } - if (timestamp < _lastTimestamp) - { //如果当前时间戳比上一次生成ID时时间戳还小,抛出异常,因为不能保证现在生成的ID之前没有生成过 - throw new Exception($"Clock moved backwards. Refusing to generate id for {_lastTimestamp - timestamp} milliseconds"); - } - _lastTimestamp = timestamp; //把当前时间戳保存为最后生成ID的时间戳 - return _workerId << 52 | (timestamp - Twepoch << 12) | _sequence; - } - } + /// + /// 设置数制格式化器 + /// + /// + public static void SetNumberFormater(NumberFormater nf) + { + _numberFormater = nf; + } - /// - /// 获取一个字符串表示形式的id - /// - /// - public string GetUniqueId() - { - return _numberFormater.ToString(GetLongId()); - } + public long GetLongId() + { + lock (LockObj) + { + long timestamp = DateTime.Now.GetTotalMilliseconds(); + if (_lastTimestamp == timestamp) + { //同一微妙中生成ID + _sequence = (_sequence + 1) & SequenceMask; //用&运算计算该微秒内产生的计数是否已经到达上限 + if (_sequence == 0) + { + //一微妙内产生的ID计数已达上限,等待下一微妙 + timestamp = DateTime.Now.GetTotalMilliseconds(); + while (timestamp <= _lastTimestamp) + { + timestamp = DateTime.Now.GetTotalMilliseconds(); + } + return timestamp; + } + } + else + { //不同微秒生成ID + _sequence = 0; //计数清0 + } + if (timestamp < _lastTimestamp) + { //如果当前时间戳比上一次生成ID时时间戳还小,抛出异常,因为不能保证现在生成的ID之前没有生成过 + throw new Exception($"Clock moved backwards. Refusing to generate id for {_lastTimestamp - timestamp} milliseconds"); + } + _lastTimestamp = timestamp; //把当前时间戳保存为最后生成ID的时间戳 + return _workerId << 52 | (timestamp - Twepoch << 12) | _sequence; + } + } - /// - /// 获取一个字符串表示形式的id - /// - /// 最大长度,至少6位 - /// - public string GetUniqueShortId(int maxLength = 8) - { - if (maxLength < 6) - { - throw new ArgumentException("最大长度至少需要6位"); - } + /// + /// 获取一个字符串表示形式的id + /// + /// + public string GetUniqueId() + { + return _numberFormater.ToString(GetLongId()); + } - string id = GetUniqueId(); - int index = id.Length - maxLength; - if (index < 0) - { - index = 0; - } + /// + /// 获取一个字符串表示形式的id + /// + /// 最大长度,至少6位 + /// + public string GetUniqueShortId(int maxLength = 8) + { + if (maxLength < 6) + { + throw new ArgumentException("最大长度至少需要6位"); + } - return id.Substring(index); - } + string id = GetUniqueId(); + int index = id.Length - maxLength; + if (index < 0) + { + index = 0; + } + + return id.Substring(index); + } } diff --git a/Masuit.Tools.AspNetCore/Masuit.Tools.AspNetCore.csproj b/Masuit.Tools.AspNetCore/Masuit.Tools.AspNetCore.csproj index b309616b..8a6edbe9 100644 --- a/Masuit.Tools.AspNetCore/Masuit.Tools.AspNetCore.csproj +++ b/Masuit.Tools.AspNetCore/Masuit.Tools.AspNetCore.csproj @@ -18,7 +18,7 @@ Masuit.Tools.AspNetCore Masuit.Tools.AspNetCore latest - 1.2.6.4 + 1.2.6.5 True 1.1.9 diff --git a/Masuit.Tools.Core/Masuit.Tools.Core.csproj b/Masuit.Tools.Core/Masuit.Tools.Core.csproj index cfd72f1a..9a127970 100644 --- a/Masuit.Tools.Core/Masuit.Tools.Core.csproj +++ b/Masuit.Tools.Core/Masuit.Tools.Core.csproj @@ -6,7 +6,7 @@ 官网教程:https://tools.masuit.org github:https://github.com/ldqk/Masuit.Tools - 2.6.6.2 + 2.6.6.3 Copyright © 懒得勤快 https://github.com/ldqk/Masuit.Tools Masuit.Tools,工具库,Utility,Crypt,Extensions diff --git a/Masuit.Tools.Net45/Masuit.Tools.Net45.csproj b/Masuit.Tools.Net45/Masuit.Tools.Net45.csproj index ac9e440a..b7a35501 100644 --- a/Masuit.Tools.Net45/Masuit.Tools.Net45.csproj +++ b/Masuit.Tools.Net45/Masuit.Tools.Net45.csproj @@ -195,8 +195,77 @@ Strings\UnicodeFormater.cs - - Systems\%(RecursiveDir)%(FileName)%(Extension) + + Systems\CompositeContractResolver.cs + + + Systems\ConcurrentHashSet.cs + + + Systems\ConcurrentLimitedQueue.cs + + + Systems\DeserializeOnlyContractResolver.cs + + + Systems\DeserializeOnlyJsonPropertyAttribute.cs + + + Systems\Disposable.cs + + + Systems\DisposableConcurrentDictionary.cs + + + Systems\DisposableDictionary.cs + + + Systems\EnumExt.cs + + + Systems\FallbackJsonProperty.cs + + + Systems\FallbackJsonPropertyResolver.cs + + + Systems\HiPerfTimer.cs + + + Systems\LargeMemoryStream.cs + + + Systems\LimitedQueue.cs + + + Systems\MaskConverter.cs + + + Systems\MaskEmailConverter.cs + + + Systems\NullableConcurrentDictionary.cs + + + Systems\NullableDictionary.cs + + + Systems\NullObject.cs + + + Systems\PooledMemoryStream.cs + + + Systems\SerializeIgnoreAttribute.cs + + + Systems\SnowFlake.cs + + + Systems\SnowFlakeNew.cs + + + Systems\StopwatchHelper.cs Validator\ComplexPassword.cs diff --git a/Masuit.Tools.Net45/package.nuspec b/Masuit.Tools.Net45/package.nuspec index 8a16a455..6919ec65 100644 --- a/Masuit.Tools.Net45/package.nuspec +++ b/Masuit.Tools.Net45/package.nuspec @@ -2,7 +2,7 @@ Masuit.Tools.Net45 - 2.6.6 + 2.6.6.3 Masuit.Tools 懒得勤快 masuit.com diff --git a/Masuit.Tools/package.nuspec b/Masuit.Tools/package.nuspec index e87c19c7..d2dd9974 100644 --- a/Masuit.Tools/package.nuspec +++ b/Masuit.Tools/package.nuspec @@ -2,7 +2,7 @@ Masuit.Tools.Net - 2.6.6.1 + 2.6.6.3 Masuit.Tools 懒得勤快 masuit.com diff --git a/Test/Masuit.Tools.Abstractions.Test/Masuit.Tools.Abstractions.Test.csproj b/Test/Masuit.Tools.Abstractions.Test/Masuit.Tools.Abstractions.Test.csproj index 9192751a..45c33b99 100644 --- a/Test/Masuit.Tools.Abstractions.Test/Masuit.Tools.Abstractions.Test.csproj +++ b/Test/Masuit.Tools.Abstractions.Test/Masuit.Tools.Abstractions.Test.csproj @@ -13,7 +13,7 @@ - + all diff --git a/Test/Masuit.Tools.Core.Test/Masuit.Tools.Core.Test.csproj b/Test/Masuit.Tools.Core.Test/Masuit.Tools.Core.Test.csproj index fa72788a..30f09bf0 100644 --- a/Test/Masuit.Tools.Core.Test/Masuit.Tools.Core.Test.csproj +++ b/Test/Masuit.Tools.Core.Test/Masuit.Tools.Core.Test.csproj @@ -11,7 +11,7 @@ - + all