添加项目文件。
This commit is contained in:
@ -0,0 +1,40 @@
|
||||
// -----------------------------------------------------------------------------
|
||||
// 让 .NET 开发更简单,更通用,更流行。
|
||||
// Copyright © 2020-2021 Furion, 百小僧, Baiqian Co.,Ltd.
|
||||
//
|
||||
// 框架名称:Furion
|
||||
// 框架作者:百小僧
|
||||
// 框架版本:2.7.9
|
||||
// 源码地址:Gitee: https://gitee.com/dotnetchina/Furion
|
||||
// Github:https://github.com/monksoul/Furion
|
||||
// 开源协议:Apache-2.0(https://gitee.com/dotnetchina/Furion/blob/master/LICENSE)
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace HT.Cloud.Code
|
||||
{
|
||||
/// <summary>
|
||||
/// 连续 GUID 类型选项
|
||||
/// </summary>
|
||||
public enum SequentialGuidType
|
||||
{
|
||||
/// <summary>
|
||||
/// 标准连续 GUID 字符串
|
||||
/// </summary>
|
||||
[Description("标准连续 GUID 字符串")]
|
||||
SequentialAsString,
|
||||
|
||||
/// <summary>
|
||||
/// Byte 数组类型的连续 `GUID` 字符串
|
||||
/// </summary>
|
||||
[Description("Byte 数组类型的连续 `GUID` 字符串")]
|
||||
SequentialAsBinary,
|
||||
|
||||
/// <summary>
|
||||
/// 连续部分在末尾展示
|
||||
/// </summary>
|
||||
[Description("连续部分在末尾展示")]
|
||||
SequentialAtEnd
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
// -----------------------------------------------------------------------------
|
||||
// 让 .NET 开发更简单,更通用,更流行。
|
||||
// Copyright © 2020-2021 Furion, 百小僧, Baiqian Co.,Ltd.
|
||||
//
|
||||
// 框架名称:Furion
|
||||
// 框架作者:百小僧
|
||||
// 框架版本:2.7.9
|
||||
// 源码地址:Gitee: https://gitee.com/dotnetchina/Furion
|
||||
// Github:https://github.com/monksoul/Furion
|
||||
// 开源协议:Apache-2.0(https://gitee.com/dotnetchina/Furion/blob/master/LICENSE)
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
namespace HT.Cloud.Code
|
||||
{
|
||||
/// <summary>
|
||||
/// 分布式 ID 生成器
|
||||
/// </summary>
|
||||
public interface IDistributedIDGenerator
|
||||
{
|
||||
/// <summary>
|
||||
/// 生成逻辑
|
||||
/// </summary>
|
||||
/// <param name="idGeneratorOptions"></param>
|
||||
/// <returns></returns>
|
||||
object Create(object idGeneratorOptions = default);
|
||||
}
|
||||
}
|
@ -0,0 +1,86 @@
|
||||
// -----------------------------------------------------------------------------
|
||||
// 让 .NET 开发更简单,更通用,更流行。
|
||||
// Copyright © 2020-2021 Furion, 百小僧, Baiqian Co.,Ltd.
|
||||
//
|
||||
// 框架名称:Furion
|
||||
// 框架作者:百小僧
|
||||
// 框架版本:2.7.9
|
||||
// 源码地址:Gitee: https://gitee.com/dotnetchina/Furion
|
||||
// Github:https://github.com/monksoul/Furion
|
||||
// 开源协议:Apache-2.0(https://gitee.com/dotnetchina/Furion/blob/master/LICENSE)
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
using System;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace HT.Cloud.Code
|
||||
{
|
||||
/// <summary>
|
||||
/// 连续 GUID ID 生成器
|
||||
/// <para>代码参考自:https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql/blob/ebe011a6f1b2a2a9709fe558cfc7ed3215b55c37/src/EFCore.MySql/ValueGeneration/Internal/MySqlSequentialGuidValueGenerator.cs </para>
|
||||
/// </summary>
|
||||
public class SequentialGuidIDGenerator : IDistributedIDGenerator
|
||||
{
|
||||
/// <summary>
|
||||
/// 随机数生成器
|
||||
/// </summary>
|
||||
private static readonly RandomNumberGenerator _rng = RandomNumberGenerator.Create();
|
||||
|
||||
/// <summary>
|
||||
/// 生成逻辑
|
||||
/// </summary>
|
||||
/// <param name="idGeneratorOptions"></param>
|
||||
/// <returns></returns>
|
||||
public object Create(object idGeneratorOptions = null)
|
||||
{
|
||||
// According to RFC 4122:
|
||||
// dddddddd-dddd-Mddd-Ndrr-rrrrrrrrrrrr
|
||||
// - M = RFC version, in this case '4' for random UUID
|
||||
// - N = RFC variant (plus other bits), in this case 0b1000 for variant 1
|
||||
// - d = nibbles based on UTC date/time in ticks
|
||||
// - r = nibbles based on random bytes
|
||||
|
||||
var options = (idGeneratorOptions ?? new SequentialGuidSettings()) as SequentialGuidSettings;
|
||||
|
||||
var randomBytes = new byte[7];
|
||||
_rng.GetBytes(randomBytes);
|
||||
var ticks = (ulong)options.TimeNow.Ticks;
|
||||
|
||||
var uuidVersion = (ushort)4;
|
||||
var uuidVariant = (ushort)0b1000;
|
||||
|
||||
var ticksAndVersion = (ushort)((ticks << 48 >> 52) | (ushort)(uuidVersion << 12));
|
||||
var ticksAndVariant = (byte)((ticks << 60 >> 60) | (byte)(uuidVariant << 4));
|
||||
|
||||
if (options.LittleEndianBinary16Format)
|
||||
{
|
||||
var guidBytes = new byte[16];
|
||||
var tickBytes = BitConverter.GetBytes(ticks);
|
||||
if (BitConverter.IsLittleEndian)
|
||||
{
|
||||
Array.Reverse(tickBytes);
|
||||
}
|
||||
|
||||
Buffer.BlockCopy(tickBytes, 0, guidBytes, 0, 6);
|
||||
guidBytes[6] = (byte)(ticksAndVersion << 8 >> 8);
|
||||
guidBytes[7] = (byte)(ticksAndVersion >> 8);
|
||||
guidBytes[8] = ticksAndVariant;
|
||||
Buffer.BlockCopy(randomBytes, 0, guidBytes, 9, 7);
|
||||
|
||||
return new Guid(guidBytes);
|
||||
}
|
||||
|
||||
var guid = new Guid((uint)(ticks >> 32), (ushort)(ticks << 32 >> 48), ticksAndVersion,
|
||||
ticksAndVariant,
|
||||
randomBytes[0],
|
||||
randomBytes[1],
|
||||
randomBytes[2],
|
||||
randomBytes[3],
|
||||
randomBytes[4],
|
||||
randomBytes[5],
|
||||
randomBytes[6]);
|
||||
|
||||
return guid;
|
||||
}
|
||||
}
|
||||
}
|
45
HT.Cloud.Code/DistributedIDGenerator/IDGen.cs
Normal file
45
HT.Cloud.Code/DistributedIDGenerator/IDGen.cs
Normal file
@ -0,0 +1,45 @@
|
||||
// -----------------------------------------------------------------------------
|
||||
// 让 .NET 开发更简单,更通用,更流行。
|
||||
// Copyright © 2020-2021 Furion, 百小僧, Baiqian Co.,Ltd.
|
||||
//
|
||||
// 框架名称:Furion
|
||||
// 框架作者:百小僧
|
||||
// 框架版本:2.7.9
|
||||
// 源码地址:Gitee: https://gitee.com/dotnetchina/Furion
|
||||
// Github:https://github.com/monksoul/Furion
|
||||
// 开源协议:Apache-2.0(https://gitee.com/dotnetchina/Furion/blob/master/LICENSE)
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
using System;
|
||||
|
||||
namespace HT.Cloud.Code
|
||||
{
|
||||
/// <summary>
|
||||
/// ID 生成器
|
||||
/// </summary>
|
||||
public static class IDGen
|
||||
{
|
||||
/// <summary>
|
||||
/// 生成唯一 ID
|
||||
/// </summary>
|
||||
/// <param name="idGeneratorOptions"></param>
|
||||
/// <param name="serviceProvider"></param>
|
||||
/// <returns></returns>
|
||||
public static object NextID(object idGeneratorOptions)
|
||||
{
|
||||
return ((IDistributedIDGenerator)GlobalContext.RootServices.GetService(typeof(IDistributedIDGenerator))).Create(idGeneratorOptions);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 生成连续 GUID
|
||||
/// </summary>
|
||||
/// <param name="guidType"></param>
|
||||
/// <param name="serviceProvider"></param>
|
||||
/// <returns></returns>
|
||||
public static Guid NextID(SequentialGuidType guidType = SequentialGuidType.SequentialAsString)
|
||||
{
|
||||
var sequentialGuid = GlobalContext.RootServices.GetService(typeof(IDistributedIDGenerator)) as IDistributedIDGenerator;
|
||||
return (Guid)sequentialGuid.Create();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
// -----------------------------------------------------------------------------
|
||||
// 让 .NET 开发更简单,更通用,更流行。
|
||||
// Copyright © 2020-2021 Furion, 百小僧, Baiqian Co.,Ltd.
|
||||
//
|
||||
// 框架名称:Furion
|
||||
// 框架作者:百小僧
|
||||
// 框架版本:2.7.9
|
||||
// 源码地址:Gitee: https://gitee.com/dotnetchina/Furion
|
||||
// Github:https://github.com/monksoul/Furion
|
||||
// 开源协议:Apache-2.0(https://gitee.com/dotnetchina/Furion/blob/master/LICENSE)
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
using System;
|
||||
|
||||
namespace HT.Cloud.Code
|
||||
{
|
||||
/// <summary>
|
||||
/// 连续 GUID 配置
|
||||
/// </summary>
|
||||
public sealed class SequentialGuidSettings
|
||||
{
|
||||
/// <summary>
|
||||
/// 当前时间
|
||||
/// </summary>
|
||||
public DateTimeOffset TimeNow { get; set; } = DateTimeOffset.UtcNow;
|
||||
|
||||
/// <summary>
|
||||
/// LittleEndianBinary 16 格式化
|
||||
/// </summary>
|
||||
public bool LittleEndianBinary16Format { get; set; } = false;
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
// -----------------------------------------------------------------------------
|
||||
// 让 .NET 开发更简单,更通用,更流行。
|
||||
// Copyright © 2020-2021 Furion, 百小僧, Baiqian Co.,Ltd.
|
||||
//
|
||||
// 框架名称:Furion
|
||||
// 框架作者:百小僧
|
||||
// 框架版本:2.7.9
|
||||
// 源码地址:Gitee: https://gitee.com/dotnetchina/Furion
|
||||
// Github:https://github.com/monksoul/Furion
|
||||
// 开源协议:Apache-2.0(https://gitee.com/dotnetchina/Furion/blob/master/LICENSE)
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
namespace HT.Cloud.Code
|
||||
{
|
||||
/// <summary>
|
||||
/// 短 ID 约束
|
||||
/// </summary>
|
||||
internal static class Constants
|
||||
{
|
||||
/// <summary>
|
||||
/// 最小长度
|
||||
/// </summary>
|
||||
public const int MinimumAutoLength = 8;
|
||||
|
||||
/// <summary>
|
||||
/// 最大长度
|
||||
/// </summary>
|
||||
public const int MaximumAutoLength = 14;
|
||||
|
||||
/// <summary>
|
||||
/// 最小可选字符长度
|
||||
/// </summary>
|
||||
public const int MinimumCharacterSetLength = 50;
|
||||
}
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
// -----------------------------------------------------------------------------
|
||||
// 让 .NET 开发更简单,更通用,更流行。
|
||||
// Copyright © 2020-2021 Furion, 百小僧, Baiqian Co.,Ltd.
|
||||
//
|
||||
// 框架名称:Furion
|
||||
// 框架作者:百小僧
|
||||
// 框架版本:2.7.9
|
||||
// 源码地址:Gitee: https://gitee.com/dotnetchina/Furion
|
||||
// Github:https://github.com/monksoul/Furion
|
||||
// 开源协议:Apache-2.0(https://gitee.com/dotnetchina/Furion/blob/master/LICENSE)
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
using System;
|
||||
|
||||
namespace HT.Cloud.Code
|
||||
{
|
||||
/// <summary>
|
||||
/// 随机数帮助类
|
||||
/// </summary>
|
||||
internal static class RandomHelpers
|
||||
{
|
||||
/// <summary>
|
||||
/// 随机数对象
|
||||
/// </summary>
|
||||
private static readonly Random Random = new();
|
||||
|
||||
/// <summary>
|
||||
/// 线程锁
|
||||
/// </summary>
|
||||
private static readonly object ThreadLock = new();
|
||||
|
||||
/// <summary>
|
||||
/// 生成线程安全的范围内随机数
|
||||
/// </summary>
|
||||
/// <param name="min"></param>
|
||||
/// <param name="max"></param>
|
||||
/// <returns></returns>
|
||||
public static int GenerateNumberInRange(int min, int max)
|
||||
{
|
||||
lock (ThreadLock)
|
||||
{
|
||||
return Random.Next(min, max);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
// -----------------------------------------------------------------------------
|
||||
// 让 .NET 开发更简单,更通用,更流行。
|
||||
// Copyright © 2020-2021 Furion, 百小僧, Baiqian Co.,Ltd.
|
||||
//
|
||||
// 框架名称:Furion
|
||||
// 框架作者:百小僧
|
||||
// 框架版本:2.7.9
|
||||
// 源码地址:Gitee: https://gitee.com/dotnetchina/Furion
|
||||
// Github:https://github.com/monksoul/Furion
|
||||
// 开源协议:Apache-2.0(https://gitee.com/dotnetchina/Furion/blob/master/LICENSE)
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
namespace HT.Cloud.Code
|
||||
{
|
||||
/// <summary>
|
||||
/// 短 ID 生成配置选项
|
||||
/// </summary>
|
||||
public class GenerationOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// 是否使用数字
|
||||
/// <para>默认 false</para>
|
||||
/// </summary>
|
||||
public bool UseNumbers { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否使用特殊字符
|
||||
/// <para>默认 true</para>
|
||||
/// </summary>
|
||||
public bool UseSpecialCharacters { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// 设置短 ID 长度
|
||||
/// </summary>
|
||||
public int Length { get; set; } = RandomHelpers.GenerateNumberInRange(Constants.MinimumAutoLength, Constants.MaximumAutoLength);
|
||||
}
|
||||
}
|
161
HT.Cloud.Code/DistributedIDGenerator/ShortID/ShortIDGen.cs
Normal file
161
HT.Cloud.Code/DistributedIDGenerator/ShortID/ShortIDGen.cs
Normal file
@ -0,0 +1,161 @@
|
||||
// -----------------------------------------------------------------------------
|
||||
// 让 .NET 开发更简单,更通用,更流行。
|
||||
// Copyright © 2020-2021 Furion, 百小僧, Baiqian Co.,Ltd.
|
||||
//
|
||||
// 框架名称:Furion
|
||||
// 框架作者:百小僧
|
||||
// 框架版本:2.7.9
|
||||
// 源码地址:Gitee: https://gitee.com/dotnetchina/Furion
|
||||
// Github:https://github.com/monksoul/Furion
|
||||
// 开源协议:Apache-2.0(https://gitee.com/dotnetchina/Furion/blob/master/LICENSE)
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace HT.Cloud.Code
|
||||
{
|
||||
/// <summary>
|
||||
/// 短 ID 生成核心代码
|
||||
/// <para>代码参考自:https://github.com/bolorundurowb/shortid </para>
|
||||
/// </summary>
|
||||
public static class ShortIDGen
|
||||
{
|
||||
/// <summary>
|
||||
/// 短 ID 生成器期初数据
|
||||
/// </summary>
|
||||
private static Random _random = new();
|
||||
|
||||
private const string Bigs = "ABCDEFGHIJKLMNPQRSTUVWXY";
|
||||
private const string Smalls = "abcdefghjklmnopqrstuvwxyz";
|
||||
private const string Numbers = "0123456789";
|
||||
private const string Specials = "_-";
|
||||
private static string _pool = $"{Smalls}{Bigs}";
|
||||
|
||||
/// <summary>
|
||||
/// 线程安全锁
|
||||
/// </summary>
|
||||
private static readonly object ThreadLock = new();
|
||||
|
||||
/// <summary>
|
||||
/// 生成目前比较主流的短 ID
|
||||
/// <para>包含字母、数字,不包含特殊字符</para>
|
||||
/// <para>默认生成 8 位</para>
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static string NextID()
|
||||
{
|
||||
return NextID(new GenerationOptions
|
||||
{
|
||||
UseNumbers = true,
|
||||
UseSpecialCharacters = false,
|
||||
Length = 8
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 生成短 ID
|
||||
/// </summary>
|
||||
/// <param name="options"></param>
|
||||
/// <returns></returns>
|
||||
public static string NextID(GenerationOptions options)
|
||||
{
|
||||
// 配置必填
|
||||
if (options == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(options));
|
||||
}
|
||||
|
||||
// 判断生成的长度是否小于规定的长度,规定为 8
|
||||
if (options.Length < Constants.MinimumAutoLength)
|
||||
{
|
||||
throw new ArgumentException(
|
||||
$"The specified length of {options.Length} is less than the lower limit of {Constants.MinimumAutoLength} to avoid conflicts.");
|
||||
}
|
||||
|
||||
var characterPool = _pool;
|
||||
var poolBuilder = new StringBuilder(characterPool);
|
||||
|
||||
// 是否包含数字
|
||||
if (options.UseNumbers)
|
||||
{
|
||||
poolBuilder.Append(Numbers);
|
||||
}
|
||||
|
||||
// 是否包含特殊字符
|
||||
if (options.UseSpecialCharacters)
|
||||
{
|
||||
poolBuilder.Append(Specials);
|
||||
}
|
||||
|
||||
var pool = poolBuilder.ToString();
|
||||
|
||||
// 生成拼接
|
||||
var output = new char[options.Length];
|
||||
for (var i = 0; i < options.Length; i++)
|
||||
{
|
||||
lock (ThreadLock)
|
||||
{
|
||||
var charIndex = _random.Next(0, pool.Length);
|
||||
output[i] = pool[charIndex];
|
||||
}
|
||||
}
|
||||
|
||||
return new string(output);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置参与运算的字符,最少 50 位
|
||||
/// </summary>
|
||||
/// <param name="characters"></param>
|
||||
public static void SetCharacters(string characters)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(characters))
|
||||
{
|
||||
throw new ArgumentException("The replacement characters must not be null or empty.");
|
||||
}
|
||||
|
||||
var charSet = characters
|
||||
.ToCharArray()
|
||||
.Where(x => !char.IsWhiteSpace(x))
|
||||
.Distinct()
|
||||
.ToArray();
|
||||
|
||||
if (charSet.Length < Constants.MinimumCharacterSetLength)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"The replacement characters must be at least {Constants.MinimumCharacterSetLength} letters in length and without whitespace.");
|
||||
}
|
||||
|
||||
lock (ThreadLock)
|
||||
{
|
||||
_pool = new string(charSet);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置种子步长
|
||||
/// </summary>
|
||||
/// <param name="seed"></param>
|
||||
public static void SetSeed(int seed)
|
||||
{
|
||||
lock (ThreadLock)
|
||||
{
|
||||
_random = new Random(seed);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 重置所有配置
|
||||
/// </summary>
|
||||
public static void Reset()
|
||||
{
|
||||
lock (ThreadLock)
|
||||
{
|
||||
_random = new Random();
|
||||
_pool = $"{Smalls}{Bigs}";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user