Lycoris.Snowflakes 6.0.4

dotnet add package Lycoris.Snowflakes --version 6.0.4
                    
NuGet\Install-Package Lycoris.Snowflakes -Version 6.0.4
                    
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.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Lycoris.Snowflakes" Version="6.0.4" />
                    
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.4
                    
#r "nuget: Lycoris.Snowflakes, 6.0.4"
                    
#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.4
                    
#: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.4
                    
Install as a Cake Addin
#tool nuget:?package=Lycoris.Snowflakes&version=6.0.4
                    
Install as a Cake Tool

雪花Id工具服务

安装方式

// net cli
dotnet add package Lycoris.Snowflakes
// package manager
Install-Package Lycoris.Snowflakes

单机服务

安装方式

// 默认注册
// 注册为单例服务
builder.Services.AddSnowflake().AsService();
// 注册为静态实例
builder.Services.AddSnowflake().AsHelper();

// 详细配置(单例服务与静态实例配置相同,不做多其他实例)
builder.Services.AddSnowflake(opt=>
{
    // 工作机器ID,默认从1开始,用于防止时钟回拨导致的Id重复
    opt.WorkId = 1;
    // 工作机器id所占用的长度,最大10,默认10
    opt.WorkIdLength = 10;
    // 用于计算时间戳的开始时间,默认起始时间 UTC 2000-01-01
    // 设置为固定时间,请不要设置为DateTime.Now
    opt.StartTimeStamp = new DateTime(2022, 1, 1)
}).AsService();

使用方式

// 单例服务
public class Demo
{
    private readonly ISnowflakeMaker _snowflakeMaker;

    public Demo(ISnowflakeMaker snowflakeMaker)
    {
        _snowflakeMaker = snowflakeMaker
    }

    public void Test()
    {
        var id = _snowflakeMaker.GetNextId();
    }

    public void TestAsync()
    {
        var id = await _snowflakeMaker.GetNextIdAsync();
    }
}

// 静态实例
public class Demo
{
    public void Test()
    {
        var id = SnowflakeHelper.GetNextId();
    }

    public void TestAsync()
    {
        var id = await SnowflakeHelper.GetNextIdAsync();
    }
}

分布式

由于分布式需要同一个服务或集群内保证唯一Id,需要分配给不同实例分配对应的机器Id,故需要redis做辅助,此处推荐 CSRedisCore 做为redis服务端演示

单例服务

创建redis辅助服务

创建redis辅助服务类 DistributedSnowflakesRedis 继承 IDistributedSnowflakesRedis 接口并实现对应功能,此处以 CSRedisCore 做为演示,其他redis相关服务请使用对应的方法,以下指令与redis cli指令名称相同

public class DistributedSnowflakesRedis : IDistributedSnowflakesRedis
{
    private readonly CSRedisClient client;

    public DistributedSnowflakesRedis()
    {
        client = new CSRedisClient("host:port,password=password,defaultDatabase=0");
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="key"></param>
    /// <param name="expire"></param>
    /// <returns></returns>
    public Task<bool> ExpireAsync(string key, TimeSpan expire) => client.ExpireAsync(key, expire);

    /// <summary>
    /// 
    /// </summary>
    /// <param name="key"></param>
    /// <param name="value"></param>
    /// <returns></returns>
    public Task<long> IncrByAsync(string key, long value) => client.IncrByAsync(key, value);

    /// <summary>
    /// 
    /// </summary>
    /// <param name="key"></param>
    /// <param name="scoreMembers"></param>
    /// <returns></returns>
    public Task<long> ZAddAsync(string key, params (decimal, object)[] scoreMembers) => client.ZAddAsync(key, scoreMembers);

    /// <summary>
    /// 
    /// </summary>
    /// <param name="key"></param>
    /// <param name="min"></param>
    /// <param name="max"></param>
    /// <param name="count"></param>
    /// <param name="offset"></param>
    /// <returns></returns>
    public Task<(string member, decimal score)[]> ZRangeByScoreWithScoresAsync(string key, decimal min, decimal max, long? count = null, long offset = 0) => client.ZRangeByScoreWithScoresAsync(key, min, max, count, offset);

    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="key"></param>
    /// <param name="member"></param>
    /// <returns></returns>
    public Task<long> ZRemAsync<T>(string key, params T[] member) => client.ZRemAsync(key, member);
}
注册服务
// 默认注册
builder.Services.AddDistributedSnowflake().AddSnowflakesRedisService<DistributedSnowflakesRedis>().AsService();

// 注册服务
builder.Services.AddDistributedSnowflake(opt => 
{
    // 工作机器ID,默认从1开始,用于防止时钟回拨导致的Id重复
    opt.WorkId = 1;
    // 工作机器id所占用的长度,最大10,默认10
    opt.WorkIdLength = 10;
    // 用于计算时间戳的开始时间,默认起始时间 UTC 2000-01-01
    // 设置为固定时间,请不要设置为DateTime.Now
    opt.StartTimeStamp = new DateTime(2022, 1, 1);
    // 分布式路由前缀(根据对应集群或者服务类别配置不同的前缀,如果未设置,则会随机生成guid)
    opt.RedisPrefix = "";
    // 分布式Id 刷新存活状态的间隔时间,默认1小时
    opt.RefreshAliveInterval = TimeSpan.FromHours(1);
})
// 添加redis辅助服务
.AddSnowflakesRedisService<DistributedSnowflakesRedis>()
.AsService();
使用方式
// 单例服务
public class Demo
{
    private readonly ISnowflakeMaker _snowflakeMaker;

    public Demo(ISnowflakeMaker snowflakeMaker)
    {
        _snowflakeMaker = snowflakeMaker
    }

    public void Test()
    {
        var id = _snowflakeMaker.GetNextId();
    }

    public void TestAsync()
    {
        var id = await _snowflakeMaker.GetNextIdAsync();
    }
}

静态实例

创建redis辅助服务

创建redis辅助服务类 DistributedSnowflakesRedis 继承 IDistributedSnowflakesRedis 接口并实现对应功能,此处以 CSRedisCore 做为演示,其他redis相关服务请使用对应的方法,以下指令与redis cli指令名称相同

public class DistributedSnowflakesRedis : IDistributedSnowflakesRedis
{
    private readonly CSRedisClient client;

    public DistributedSnowflakesRedis()
    {
        client = new CSRedisClient("host:port,password=password,defaultDatabase=0");
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="key"></param>
    /// <param name="expire"></param>
    /// <returns></returns>
    public Task<bool> ExpireAsync(string key, TimeSpan expire) => client.ExpireAsync(key, expire);

    /// <summary>
    /// 
    /// </summary>
    /// <param name="key"></param>
    /// <param name="value"></param>
    /// <returns></returns>
    public Task<long> IncrByAsync(string key, long value) => client.IncrByAsync(key, value);

    /// <summary>
    /// 
    /// </summary>
    /// <param name="key"></param>
    /// <param name="scoreMembers"></param>
    /// <returns></returns>
    public Task<long> ZAddAsync(string key, params (decimal, object)[] scoreMembers) => client.ZAddAsync(key, scoreMembers);

    /// <summary>
    /// 
    /// </summary>
    /// <param name="key"></param>
    /// <param name="min"></param>
    /// <param name="max"></param>
    /// <param name="count"></param>
    /// <param name="offset"></param>
    /// <returns></returns>
    public Task<(string member, decimal score)[]> ZRangeByScoreWithScoresAsync(string key, decimal min, decimal max, long? count = null, long offset = 0) => client.ZRangeByScoreWithScoresAsync(key, min, max, count, offset);

    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="key"></param>
    /// <param name="member"></param>
    /// <returns></returns>
    public Task<long> ZRemAsync<T>(string key, params T[] member) => client.ZRemAsync(key, member);
}
注册服务
// 基础注册
// 这种注册方式,需要 DistributedSnowflakesRedis 没有构造函数,或 DistributedSnowflakesRedis 有构造函数但是必须为无参的构造函数
builder.Services.AddDistributedSnowflake().AddSnowflakesRedisHelper<DistributedSnowflakesRedis>().AsHelper();

// 有参构造函数请使用以下方式
// 入参仅为示例,具体请根据服务实际情况自行处理
var redisHelper = new DistributedSnowflakesRedis(redis,...);
builder.Services.AddDistributedSnowflake().AddSnowflakesRedisHelper(redisHelper).AsHelper();

// 注册服务
builder.Services.AddDistributedSnowflake(opt => 
{
    // 工作机器ID,默认从1开始,用于防止时钟回拨导致的Id重复
    opt.WorkId = 1;
    // 工作机器id所占用的长度,最大10,默认10
    opt.WorkIdLength = 10;
    // 用于计算时间戳的开始时间,默认起始时间 UTC 2000-01-01
    // 设置为固定时间,请不要设置为DateTime.Now
    opt.StartTimeStamp = new DateTime(2022, 1, 1);
    // 分布式路由前缀(根据对应集群或者服务类别配置不同的前缀,如果未设置,则会随机生成guid)
    opt.RedisPrefix = "";
    // 分布式Id 刷新存活状态的间隔时间,默认1小时
    opt.RefreshAliveInterval = TimeSpan.FromHours(1);
})
// 添加redis辅助服务
.AddSnowflakesRedisHelper<DistributedSnowflakesRedis>()
.AsHelper();
使用方式
// 静态实例
public class Demo
{
    public void Test()
    {
        var id = DistributedSnowflakeHelper.GetNextId();
    }

    public void TestAsync()
    {
        var id = await DistributedSnowflakeHelper.GetNextIdAsync();
    }
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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.