Crping.TaskManager.Redis
3.0.0
dotnet add package Crping.TaskManager.Redis --version 3.0.0
NuGet\Install-Package Crping.TaskManager.Redis -Version 3.0.0
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="Crping.TaskManager.Redis" Version="3.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Crping.TaskManager.Redis" Version="3.0.0" />
<PackageReference Include="Crping.TaskManager.Redis" />
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 Crping.TaskManager.Redis --version 3.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Crping.TaskManager.Redis, 3.0.0"
#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 Crping.TaskManager.Redis@3.0.0
#: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=Crping.TaskManager.Redis&version=3.0.0
#tool nuget:?package=Crping.TaskManager.Redis&version=3.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Crping.TaskManager.Redis
Crping.TaskManager 的 Redis Stream 扩展,提供基于 Redis Stream 的消息队列支持。
版本更新说明
3.0.0
2026年7月30日 星期三
- 从
Crping.TaskManager拆分独立为Crping.TaskManager.Redis - 版本号统一更新至 3.0.0
1.0.0
2026年1月29日 星期三
- 初始版本
- 从 Crping.TaskManager 中拆分 Redis 相关功能
- 新增
IReceiverWithRedis接口 - 新增
ReceiverWithRedis<TReceiver>抽象基类 - 新增
RedisUtils序列化工具类 - 修复
AcknowledgedIds线程安全问题(从 static 改为实例字段)
功能特性
- Redis Stream 消费者组支持:基于 Redis Stream 实现可靠的消息队列消费
- 消息确认与删除:支持消息确认(ACK)和批量删除
- Pending 消息处理:自动读取未正常处理的 Pending 消息
- 对象序列化:支持对象与 Redis Stream Entry 之间的自动序列化/反序列化
- 消费者组管理:自动创建消费者组(如果不存在)
依赖
- Crping.TaskManager (>= 3.0.0)
- StackExchange.Redis (>= 3.0.17)
快速开始
1. 注册 Redis 服务
// 在 ConfigureServices 中注册 Redis 连接
services.AddSingleton<IConnectionMultiplexer>(n =>
{
var connection = configuration.GetConnectionString("RedisCache");
return ConnectionMultiplexer.Connect(connection!);
});
services.AddScoped(n =>
{
var redis = n.GetRequiredService<IConnectionMultiplexer>();
return redis.GetDatabase();
});
2. 创建 Redis 消息生产者
using Crping.TaskManager.Abstractions;
using Crping.TaskManager.Redis;
public class MyProducer : ReceiverWithRedis<MyProducer>
{
public override Task<List<ICommand>> CreateCommands(IServiceProvider sp, ILogger log)
{
// 创建 10 个命令
return CreateSimpleCommandsWithRedis<MyProducer>(10);
}
public override async Task Execute(IServiceProvider sp, ILogger log)
{
// 生产消息到 Redis Stream
var data = new { Id = 1, Name = "Test" };
await Cache.StreamAddAsync("my_stream", data.ToRedisNameValueEntries());
}
}
3. 创建 Redis 消息消费者
using Crping.TaskManager.Abstractions;
using Crping.TaskManager.Redis;
public class MyConsumer : ReceiverWithRedis<MyConsumer>
{
public override async Task<List<ICommand>> CreateCommands(IServiceProvider sp, ILogger log)
{
// 从 Redis Stream 读取消息
return await GetCommandsFromRedisAsync("my_stream");
}
public override async Task Execute(IServiceProvider sp, ILogger log)
{
// 处理消息
var model = Entry.FromRedisStreamEntry<MyModel>();
log.Trace($"处理消息:{model.Name}");
// 确认消息已处理
await StreamAcknowledgeAsync();
}
}
API 参考
ReceiverWithRedis<TReceiver>
抽象基类,提供 Redis Stream 消费功能。
属性
| 属性 | 类型 | 说明 |
|---|---|---|
Cache |
IDatabase |
Redis 数据库实例 |
Entry |
StreamEntry |
当前处理的消息条目 |
AcknowledgedIds |
ConcurrentBag<RedisValue> |
已确认的消息 ID 列表 |
StreamName |
string |
Redis Stream 名称 |
GroupName |
string |
消费者组名称 |
ConsumerName |
string |
消费者名称 |
方法
| 方法 | 说明 |
|---|---|
CreateConsumerGroupAsync() |
创建消费者组(如果不存在) |
GetCommandsFromRedisAsync(streamName, count, groupName, consumerName) |
从 Redis Stream 读取消息并转换为命令列表 |
StreamAcknowledgeAsync() |
确认当前消息已被处理 |
StreamDeleteAsync() |
批量删除已确认的消息 |
StreamAckAndDeleteAsync() |
确认并删除当前消息 |
CheckAndInitCache() |
检查并初始化 Cache 对象 |
CreateSimpleCommandsWithRedis<T>(count) |
创建简单的 Redis 命令列表 |
RedisUtils
提供对象与 Redis Stream Entry 之间的序列化/反序列化扩展方法。
扩展方法
| 方法 | 说明 |
|---|---|
ToRedisNameValueEntries<T>(this T obj) |
将对象序列化为 NameValueEntry 数组 |
FromRedisStreamEntry<T>(this StreamEntry entry) |
从 StreamEntry 反序列化为对象 |
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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.
-
net10.0
- Crping.TaskManager (>= 3.0.0)
- StackExchange.Redis (>= 3.0.17)
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 |
|---|---|---|
| 3.0.0 | 29 | 8/3/2026 |
Crping.TaskManager 的 Redis Stream 扩展