Anit.Sequence.Redis.StackExchangeRedis 10.0.0.2

dotnet add package Anit.Sequence.Redis.StackExchangeRedis --version 10.0.0.2
                    
NuGet\Install-Package Anit.Sequence.Redis.StackExchangeRedis -Version 10.0.0.2
                    
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="Anit.Sequence.Redis.StackExchangeRedis" Version="10.0.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Anit.Sequence.Redis.StackExchangeRedis" Version="10.0.0.2" />
                    
Directory.Packages.props
<PackageReference Include="Anit.Sequence.Redis.StackExchangeRedis" />
                    
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 Anit.Sequence.Redis.StackExchangeRedis --version 10.0.0.2
                    
#r "nuget: Anit.Sequence.Redis.StackExchangeRedis, 10.0.0.2"
                    
#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 Anit.Sequence.Redis.StackExchangeRedis@10.0.0.2
                    
#: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=Anit.Sequence.Redis.StackExchangeRedis&version=10.0.0.2
                    
Install as a Cake Addin
#tool nuget:?package=Anit.Sequence.Redis.StackExchangeRedis&version=10.0.0.2
                    
Install as a Cake Tool

Anit.Sequence.Redis.StackExchangeRedis

Redis 序列生成器的 StackExchange.Redis 实现,基于 Redis 原子递增操作提供序列号生成功能。

此库使用 StackExchange.Redis 生成序列号,在使用前建议先了解 StackExchange.Redis 官方文档。

功能特性

  • 支持基本序列号生成(全局递增)
  • 支持每日序列号生成(按日重置)
  • 支持格式化序列号生成(带日期和固定长度)
  • 支持批量获取序列号
  • 线程安全,适用于高并发环境

安装

Install-Package Anit.Sequence.Redis.StackExchangeRedis

快速开始

配置依赖注入

// 配置 Anit.Sequence.Redis.StackExchangeRedis
builder.Services.AddAnitStackExchangeRedisSequence(
    builder.Configuration,
    (_, _) => ConnectionMultiplexer.Connect("127.0.0.1:6379").GetDatabase());

// 可选:自定义配置
builder.Services.Configure<AnitRedisSequenceOptions>(options =>
{
    options.KeyPrefix = "MyApp:Sequence";
});

配置文件示例:

{
  "Redis": {
    "Sequence": {
      "KeyPrefix": "MyApp:Sequence",
      "KeyExpireInDays": 7
    }
  }
}

配置选项

通过 AnitRedisSequenceOptions 配置以下选项:

选项名 描述 默认值 是否必填
KeyPrefix Redis 键前缀 "Sequence" 否
KeyExpireInDays 键过期天数,用于设置每日序列键的过期时间 7 天 否

使用示例

生成序列号

public class OrderService(IAnitRedisSequenceGenerator sequenceGenerator)
{
    public async Task<string> GenerateOrderSNAsync()
    {
        // 获取格式化的订单号,格式:AS25010100001
        return await sequenceGenerator.NextSerialNumberAsync("AS");
    }

    public async Task<string[]> BatchGenerateOrderSNsAsync(int batchSize)
    {
        // 批量获取订单号
        return await sequenceGenerator.NextSerialNumbersAsync(batchSize, "AS");
    }
}

注意事项

  • 确保 Redis 服务可用且连接配置正确
  • 每日序列号会在次日自动重置
  • 在分布式环境中,确保所有实例使用相同的 Redis 服务器以保证序列号唯一性
  • 使用 Keyed 服务注入时,需要确保注册时使用的键与消费时使用的键一致
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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 is compatible.  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 is compatible.  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
10.0.0.2 148 2/2/2026