Nice
2 years ago
7 changed files with 74 additions and 3 deletions
@ -0,0 +1,20 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk"> |
|||
|
|||
<PropertyGroup> |
|||
<TargetFramework>net6.0</TargetFramework> |
|||
<ImplicitUsings>enable</ImplicitUsings> |
|||
<GeneratePackageOnBuild>True</GeneratePackageOnBuild> |
|||
<Version>1.1.0</Version> |
|||
<RootNamespace /> |
|||
<!--<Nullable>enable</Nullable>--> |
|||
</PropertyGroup> |
|||
|
|||
<ItemGroup> |
|||
<PackageReference Include="Yitter.IdGenerator" Version="1.0.12" /> |
|||
</ItemGroup> |
|||
|
|||
<ItemGroup> |
|||
<ProjectReference Include="..\src\Easy.DI\Easy.DI.csproj" /> |
|||
</ItemGroup> |
|||
|
|||
</Project> |
@ -0,0 +1,16 @@ |
|||
using Easy.Snowflakes; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
|
|||
namespace Easy.Extensions; |
|||
public static class ServiceCollectionServiceExtensions |
|||
{ |
|||
public static IServiceCollection AddDriftingSnowflakes(this IServiceCollection services, ushort workId) |
|||
{ |
|||
services.AddSingleton(typeof(ISnowflakeIdGenerator), o => |
|||
{ |
|||
return new DriftingSnowflakeIdGenerator(workId); |
|||
}); |
|||
|
|||
return services; |
|||
} |
|||
} |
@ -0,0 +1,24 @@ |
|||
using Yitter.IdGenerator; |
|||
|
|||
namespace Easy.Snowflakes; |
|||
public class DriftingSnowflakeIdGenerator : ISnowflakeIdGenerator |
|||
{ |
|||
private readonly IIdGenerator Generator; |
|||
public DriftingSnowflakeIdGenerator(ushort workerId) |
|||
{ |
|||
Generator = new DefaultIdGenerator(new IdGeneratorOptions() |
|||
{ |
|||
WorkerId = workerId, |
|||
BaseTime = DateTime.Parse("2022-01-01 00:00:00") |
|||
}); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 生成新的Id
|
|||
/// </summary>
|
|||
/// <returns></returns>
|
|||
public long Create() |
|||
{ |
|||
return Generator.NewLong(); |
|||
} |
|||
} |
@ -0,0 +1,5 @@ |
|||
namespace Easy.Snowflakes; |
|||
public interface ISnowflakeIdGenerator |
|||
{ |
|||
long Create(); |
|||
} |
Loading…
Reference in new issue