LuBan.Redis
2026.7.13.2
dotnet add package LuBan.Redis --version 2026.7.13.2
NuGet\Install-Package LuBan.Redis -Version 2026.7.13.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="LuBan.Redis" Version="2026.7.13.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="LuBan.Redis" Version="2026.7.13.2" />
<PackageReference Include="LuBan.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 LuBan.Redis --version 2026.7.13.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: LuBan.Redis, 2026.7.13.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 LuBan.Redis@2026.7.13.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=LuBan.Redis&version=2026.7.13.2
#tool nuget:?package=LuBan.Redis&version=2026.7.13.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
English | 中文
LuBan.Redis
作者: yswenli | 联系邮箱: yswenli@outlook.com | 代码仓库: https://github.com/yswenli/luban-framework
一站式 Redis 解决方案 —— 缓存、队列、发布订阅、分布式锁,一个 SDK 全搞定。
Related Projects: LuBan.Framework | LuBan.Common | LuBan.Web.Core
为什么选择 LuBan.Redis?
在日常 .NET 开发中,Redis 的使用场景无处不在:缓存加速、消息队列、分布式锁、实时通知…… 但底层 API 过于繁琐,每次都要重复编写连接管理、序列化、重试逻辑。
LuBan.Redis 提供了开箱即用的高层抽象:
- 单例门面
LuBanRedis,一行代码完成初始化 - 内置分布式锁(可重入 / 非重入双模式,Lua 脚本原子操作,指数退避防惊群)
- List 队列 + Stream 消息流 + Pub/Sub 发布订阅,三种消息模型按需选用
RedisCache实现IServiceCache接口,无缝接入框架缓存体系- 键过期监听器,轻松实现延迟任务
快速预览
// 初始化(自动从 配置中心读取 RedisOptions)
var redis = LuBanRedis.Instance;
// 缓存:获取或自动填充
var user = await redis.GetFromCacheAsync<User>("user:1001", async () =>
{
return await _userService.GetByIdAsync(1001);
}, timeout: 60000);
// 分布式锁(using 自动释放)
var lockObj = redis.GetDistributedLock("order:pay:12345", timeout: 30000);
await using var token = await lockObj.AcquireAsync(waitTimeout: TimeSpan.FromSeconds(5));
if (token != null)
{
// 执行业务逻辑
}
// 发布订阅
var publisher = redis.GetPublisher("notification");
await publisher.PublishAsync("Hello World");
var subscriber = redis.GetSubscriber("notification");
subscriber.OnMessageReceived += (sub, msg) => Console.WriteLine(msg);
await subscriber.StartAsync();
// Stream 消息队列
var producer = redis.GetRedisProducer("task-stream");
await producer.PublishAsync(new { TaskId = 1, Name = "数据同步" });
var consumer = redis.GetRedisConsumer("task-stream", groupName: "worker-group");
var messages = await consumer.GetMessagesAsync<MyTask>(count: 10);
安装
dotnet add package LuBan.Redis
功能概览
| 功能模块 | 核心类 | 说明 |
|---|---|---|
| 快速初始化 | LuBanRedis |
单例门面,自动读取配置 |
| 连接构建 | RedisClientBuilder |
支持自定义多实例构建(带缓存) |
| 缓存 | RedisCache |
实现 IServiceCache,支持过期、前缀删除 |
| 分布式锁 | RedisDistributedLockV3 |
Lua 脚本原子操作,支持可重入/非重入 |
| 锁令牌 | DistributedLockToken |
支持 using / await using 自动释放 |
| List 队列 | RedisQueue<T> |
入队/出队/阻塞出队/队列状态管理 |
| Stream 生产者 | RedisProducer |
基于 Redis Stream 的消息发布 |
| Stream 消费者 | RedisConsumer |
消费组支持,ACK 确认机制 |
| Pub/Sub 发布 | RedisPublisher |
频道消息发布(字符串/对象) |
| Pub/Sub 订阅 | RedisSubscriber / RedisSubscriber<T> |
事件驱动消息订阅 |
| 键过期监听 | RedisKeyExpireListener |
自动配置 keyspace notifications |
详细用法
配置
{
"RedisOptions": {
"Type": 0,
"Masters": "127.0.0.1:6379",
"Password": "your_password",
"Slaves": "",
"ServiceName": "mymaster",
"DefaultDatabase": 0,
"AllowAdmin": true,
"KeepAlive": 180,
"ConnectTimeout": 10000,
"ConnectRetry": 1,
"CommandTimeout": 60000
}
}
分布式锁
// 非重入锁(性能最优,SET NX PX 单命令)
var lockObj = redis.GetDistributedLock("resource-key", timeout: 60000, reentrant: false);
await using var token = await lockObj.AcquireAsync(
waitTimeout: TimeSpan.FromSeconds(10),
retryDelay: TimeSpan.FromMilliseconds(50));
if (token != null)
{
// 续期
await lockObj.RenewAsync(TimeSpan.FromSeconds(30));
}
// 可重入锁(Hash 结构,支持同一线程多次获取)
var reentrantLock = redis.GetDistributedLock("reentrant-key", reentrant: true, token: "my-thread-id");
Stream 消息队列
// 生产者
var producer = redis.GetRedisProducer("order-events", maxLength: 50000);
var msgId = await producer.PublishAsync(new { OrderId = 1001, Amount = 99.9m });
// 消费者(消费组模式)
var consumer = redis.GetRedisConsumer("order-events", groupName: "order-workers");
var messages = await consumer.GetMessagesAsync<OrderEvent>(count: 20);
if (messages != null)
{
foreach (var msg in messages)
{
// 处理消息
Console.WriteLine($"MessageId: {msg.Id}, Data: {msg.Data.OrderId}");
}
// ACK 确认
await consumer.AcknowledgeAsync(messages.Select(m => m.Id).ToList());
}
键过期监听
var listener = redis.GetKeyExpireListener();
listener.OnKeyExpired += (sender, key) =>
{
Console.WriteLine($"Key expired: {key}");
};
await listener.StartAsync();
RedisCache 缓存
var cache = RedisCache.Instance;
// 设置缓存(带过期时间)
cache.Set("config:app", appConfig, TimeSpan.FromHours(1));
// 获取或自动填充
var config = cache.GetOrSet("config:app", key => LoadFromDb(), TimeSpan.FromHours(1));
// 前缀批量删除
var removed = cache.RemoveByPrefix("config:");
使用提示
LuBanRedis为单例模式,全局使用LuBanRedis.Instance即可,无需重复创建- 分布式锁推荐使用
await using语法,确保异常时也能正确释放 - 非重入锁性能优于可重入锁,无嵌套加锁需求时优先选择非重入模式
- Stream 消费组的
groupName不能为空,否则退化为普通读取模式 - 键过期监听需要 Redis 服务端支持
notify-keyspace-events配置(组件会自动设置) RedisClientBuilder.Build()内部使用ConcurrentDictionary缓存连接,相同配置不会重复创建
许可证
Copyright (c) yswenli. All rights reserved.
| Product | Versions 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 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.
-
net8.0
- LuBan.Common (>= 2026.7.13.2)
- SqlSugarCore (>= 5.1.4.216)
- StackExchange.Redis (>= 3.0.17)
- System.Security.Cryptography.Xml (>= 10.0.9)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on LuBan.Redis:
| Package | Downloads |
|---|---|
|
LuBan.Web.Core
LuBan Framework中api核心功能项目,基于aspnetcore集成di、jwt、swagger、codefirtst、支持多种常见数据库、nacos配置中心、统一接口回复参数、全局异常捕获、全局接口日志、防重放攻击、图形验证码、快捷上下文对象、上传下载、数据导入导出等功能 |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 2026.7.13.2 | 129 | 7/13/2026 |
| 2026.7.13.1 | 125 | 7/13/2026 |
| 2026.7.12.2 | 137 | 7/12/2026 |
| 2026.7.12.1 | 150 | 7/12/2026 |
| 2026.7.11.2 | 134 | 7/11/2026 |
LuBan Frameworkredis sdk