Lycoris.Snowflakes 6.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package Lycoris.Snowflakes --version 6.0.1
                    
NuGet\Install-Package Lycoris.Snowflakes -Version 6.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Lycoris.Snowflakes" Version="6.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Lycoris.Snowflakes" Version="6.0.1" />
                    
Directory.Packages.props
<PackageReference Include="Lycoris.Snowflakes" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Lycoris.Snowflakes --version 6.0.1
                    
#r "nuget: Lycoris.Snowflakes, 6.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Lycoris.Snowflakes@6.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Lycoris.Snowflakes&version=6.0.1
                    
Install as a Cake Addin
#tool nuget:?package=Lycoris.Snowflakes&version=6.0.1
                    
Install as a Cake Tool

雪花Id生成器

本地雪花Id生成器

雪花Id生成器注册
  • 1. 基础注册
builder.Services.AddLocalSnowflake();
  • 2. 启用雪花Id存储桶来提高获取Id的效率
builder.Services.AddLocalSnowflake(builder =>
{
    builder.Configure(opt =>
    {
        // 启用雪花Id存储桶,不设置的话默认不启用
        opt.EnabledSnowflakeBucket = true;
        // 雪花Id存储桶上限,默认:50
        // 设置上限的请根据业务量和机器内存来确定
        // 存储桶没有雪花Id的时候方法会自动切换实时生成,不影响Id获取使用,所以不建议设置过大
        opt.SnowflakeBucketLimit = 5;
        // 雪花Id存储桶刷新时长,默认:1秒
        opt.SnowflakeBucketRefreshnterval = TimeSpan.FromSeconds(10);
    });
});
  • 3. 所有配置
builder.Services.AddLocalSnowflake(builder =>
{
    builder.Configure(opt =>
    {
        // 工作机器ID,不设置的话默认从1开始,用于防止时钟回拨导致的Id重复
        opt.WorkId = 1;
        // 工作机器id所占用的长度,最大10,默认10
        opt.WorkIdLength = 10;
        // 用于计算时间戳的开始时间,默认起始时间 UTC 2000-01-01
        opt.StartTimeStamp = DateTime.Now;
        // 启用雪花Id存储桶,不设置的话默认不启用
        opt.EnabledSnowflakeBucket = true;
        // 雪花Id存储桶上限,默认:50
        // 设置上限的请根据业务量和机器内存来确定
        // 存储桶没有雪花Id的时候方法会自动切换实时生成,不影响Id获取使用,所以不建议设置过大
        opt.SnowflakeBucketLimit = 5;
        // 雪花Id存储桶刷新时长,默认:1秒
        opt.SnowflakeBucketRefreshnterval = TimeSpan.FromSeconds(10);
        // 是否过滤基础日志 默认:环境变量 ASPNETCORE_ENVIRONMENT = Development 不过滤 其他情况过滤
        opt.FilterInfoLogger = true;
    });

    // 自定义雪花Id存储桶,允许自定义存储桶
    builder.AddSnowflakesBucket<CustomeSnowflakesBucket>();
    // 自定义日志工厂 需要实现 ILycorisLoggerFactory 接口
    builder.AddLoggerFactory<CustomeLoggerFactory>();
});
使用方法
public class TestWork : ITestWork
{
    private readonly ISnowflakeMaker _snowflakeMaker; 

    public TestWork(ISnowflakeMaker snowflakeMaker)
    {
        _snowflakeMaker = snowflakeMaker;
    }

    public void Test()
    {
       // 如果有启用存储桶,请使用以下方法获取Id
       var snowflakeId = _snowflakeMaker.BucketPriorityNextId();
       // 如果没有启用存储桶
       snowflakeId = _snowflakeMaker.RealTimeNextId();
    }
}

分布式雪花Id生成器

  • 1. 基础注册
builder.Services.AddDistributedSnowflake(builder => 
{
    // 添加分布式Id缓存服务 需要继承 'IDistributedSnowflakesRedis' 并实现相关redis操作方法
    builder.AddSnowflakesDistributedRedis<DistributedSnowflakesRedis>();
});
  • 2. 启用雪花Id存储桶来提高获取Id的效率
builder.Services.AddDistributedSnowflake(builder =>
{
    builder.Configure(opt =>
    {
        // 启用雪花Id存储桶,不设置的话默认不启用
        opt.EnabledSnowflakeBucket = true;
        // 雪花Id存储桶上限,默认:50
        // 设置上限的请根据业务量和机器内存来确定
        // 存储桶没有雪花Id的时候方法会自动切换实时生成,不影响Id获取使用,所以不建议设置过大
        opt.SnowflakeBucketLimit = 5;
        // 雪花Id存储桶刷新时长,默认:1秒
        opt.SnowflakeBucketRefreshnterval = TimeSpan.FromSeconds(10);
    });

    // 添加分布式Id缓存服务 需要继承 'IDistributedSnowflakesRedis' 并实现相关redis操作方法
    builder.AddSnowflakesDistributedRedis<DistributedSnowflakesRedis>();
});
  • 3. 所有配置
builder.Services.AddDistributedSnowflake(builder =>
{
    builder.Configure(opt =>
    {
        // 工作机器ID,不设置的话默认从1开始,用于防止时钟回拨导致的Id重复
        opt.WorkId = 1;
        // 工作机器id所占用的长度,最大10,默认10
        opt.WorkIdLength = 10;
        // 用于计算时间戳的开始时间,默认起始时间 UTC 2000-01-01
        opt.StartTimeStamp = DateTime.Now;
        // 启用雪花Id存储桶,不设置的话默认不启用
        opt.EnabledSnowflakeBucket = true;
        // 雪花Id存储桶上限,默认:50
        // 设置上限的请根据业务量和机器内存来确定
        // 存储桶没有雪花Id的时候方法会自动切换实时生成,不影响Id获取使用,所以不建议设置过大
        opt.SnowflakeBucketLimit = 5;
        // 雪花Id存储桶刷新时长,默认:1秒
        opt.SnowflakeBucketRefreshnterval = TimeSpan.FromSeconds(10);
        // 是否过滤基础日志 默认:环境变量 ASPNETCORE_ENVIRONMENT = Development 不过滤 其他情况过滤
        opt.FilterInfoLogger = true;
        // 分布式Id redis缓存前缀,默认值:LycorisSnowflake
        opt.RedisPrefix = "RedisPrefix";
    });

    // 添加分布式Id缓存服务 需要继承 'IDistributedSnowflakesRedis' 并实现相关redis操作方法
    builder.AddSnowflakesDistributedRedis<DistributedSnowflakesRedis>();

    // 自定义雪花Id存储桶
    builder.AddSnowflakesBucket<CustomeSnowflakesBucket>();
    // 自定义日志工厂 需要实现 ILycorisLoggerFactory 接口
    builder.AddLoggerFactory<CustomeLoggerFactory>();
});
使用方法
public class TestWork : ITestWork
{
    private readonly ISnowflakeMaker _snowflakeMaker; 

    public TestWork(ISnowflakeMaker snowflakeMaker)
    {
        _snowflakeMaker = snowflakeMaker;
    }

    public void Test()
    {
       // 如果有启用存储桶,请使用以下方法获取Id
       var snowflakeId = _snowflakeMaker.BucketPriorityNextId();
       // 如果没有启用存储桶
       snowflakeId = _snowflakeMaker.RealTimeNextId();
    }
}

扩展使用注意事项:

  • 1. 本地雪花Id生成器与分布式雪花Id生成器无法同时使用,请不要同时注册以免造成异常
  • 2. 分布式雪花Id生成器必须要继承 IDistributedSnowflakesRedis 并实现相关redis操作方法,并在添加扩展时候使用 AddSnowflakesDistributedRedis 进行注册

PS:如果你使用了多个Lycoris系列扩展,那你可以在注册这些扩展之前使用builder.Serovces.AddLycorisLoggerFactory<CustomeLoggerFactory>()进行替换,就不需要在每个扩展中使用AddLycorisLoggerFactory进行逐一替换了

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
6.0.4 218 9/25/2023
6.0.3 259 3/27/2023
6.0.2 391 11/28/2022
6.0.1 443 11/28/2022 6.0.1 is deprecated because it is no longer maintained.