NewLife.Redis.Extensions 6.3.2025.1001-beta1743

This is a prerelease version of NewLife.Redis.Extensions.
There is a newer version of this package available.
See the version list below for details.
dotnet add package NewLife.Redis.Extensions --version 6.3.2025.1001-beta1743
                    
NuGet\Install-Package NewLife.Redis.Extensions -Version 6.3.2025.1001-beta1743
                    
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="NewLife.Redis.Extensions" Version="6.3.2025.1001-beta1743" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="NewLife.Redis.Extensions" Version="6.3.2025.1001-beta1743" />
                    
Directory.Packages.props
<PackageReference Include="NewLife.Redis.Extensions" />
                    
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 NewLife.Redis.Extensions --version 6.3.2025.1001-beta1743
                    
#r "nuget: NewLife.Redis.Extensions, 6.3.2025.1001-beta1743"
                    
#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 NewLife.Redis.Extensions@6.3.2025.1001-beta1743
                    
#: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=NewLife.Redis.Extensions&version=6.3.2025.1001-beta1743&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=NewLife.Redis.Extensions&version=6.3.2025.1001-beta1743&prerelease
                    
Install as a Cake Tool

NewLife.Redis - 高性能 Redis 客户端组件

GitHub top language GitHub License Nuget Downloads Nuget Nuget (with prereleases)

[English]

NewLife.Redis 是新生命团队打造的 高性能 / 高吞吐 / 易集成 的 Redis 客户端,核心目标:支撑实时计算、海量缓存、可靠消息、分布式基础设施等场景。组件自 2017 年起在多个千万 / 百亿级数据与高并发生产平台稳定运行,经受日均 80+ 亿次 调用考验。


目录


核心特性

  • 经大规模生产验证:200+ Redis 实例,日峰值 1 亿+ 业务对象写入 / 80+ 亿命令调用
  • 低延迟:单次 Get/Set 往返 200~600µs(微秒级,含网络)
  • 高吞吐:内置 连接池 (最大 100000 并发) + 同步高效协议解析 + 可选自动管道合并
  • 自动重试与多地址故障切换:Server 支持逗号分隔多节点,网络型异常快速切换
  • 丰富高级结构:List / Hash / Set / Queue / Stack / 延迟与可靠消费(基于 RPOPLPUSH/BRPOPLPUSH)
  • 批量优化:GetAll / SetAll / 管道聚合显著降低 RTT
  • 可插拔编码器:默认 JSON,可扩展二进制 / 自定义序列化,以减小包长与 GC 压力
  • 追踪与监控:支持 ITracer(APM 链路),可插入性能计数器 PerfCounter
  • 强命名 + 多目标框架:一份包覆盖 net45 / net461 / netstandard2.0 / netstandard2.1 / (扩展包含 netcoreapp3.1→net9)
  • 零外部重量级依赖,充分复用 NewLife 生态(日志、配置、序列化、安全)

架构与模块划分

┌──────────────────────────────────────────┐
│ Redis (基础核心)                         │
│  ├─ 连接池 IPool<ObjectPool<RedisClient>>│
│  ├─ 协议解析 RedisClient / RESP          │
│  ├─ 基础 KV / 批量 / 过期 / 计数器       │
│  ├─ 重试 & 多节点切换 / 超时 / 限制       │
│  ├─ Pipeline 管理 (Start/Stop/Auto)      │
│  └─ 序列化编码 (IPacketEncoder)          │
│                                          │
│ FullRedis (扩展)                         │
│  ├─ 列表 RedisList<T>                    │
│  ├─ 哈希 RedisHash<T> (键值字典)         │
│  ├─ 队列 / 栈 / Set / 延迟消费           │
│  ├─ 发布订阅 (若启用) / 搜索辅助         │
│  └─ 可靠消息 (RPOPLPUSH / BRPOPLPUSH)    │
│                                          │
│ Extensions (DI 集成)                     │
│  ├─ IDistributedCache 实现               │
│  └─ IDataProtection 后端存储             │
└──────────────────────────────────────────┘

关键类说明:

  • Redis:核心客户端,提供协议管线、连接复用、自动管道、批量操作、性能统计、重试与故障转移。
  • RedisClient:底层连接与 RESP 命令执行单元(内部对象,池化管理)。
  • FullRedis:在 Redis 基础上追加更丰富数据结构及更高级场景能力。
  • RedisList<T> / RedisHash<T> 等:对 Redis 原生命令进行泛型包装,统一编码与序列化策略。
  • IPacketEncoder:编解码策略,可切换 JSON / 二进制。

对比说明

能力 NewLife.Redis StackExchange.Redis 备注
连接池 内置,支持 Max=100000 Multiplexer(逻辑复用) 大并发场景更直观可控
自动管道 支持 AutoPipeline/FullPipeline 默认批量写入合并 精细控制与显式 StartPipeline
性能追踪 ITracer 接口 需外部集成 生态一致 APM 链路
序列化 可插拔 Encoder 仅字节/字符串,需外部序列化 降低调用侧样板代码
可靠消费 RPOPLPUSH/BRPOPLPUSH 包装 需手动实现 队列式工作流场景便利
生态整合 与 NewLife.* 组件无缝 同一风格 & 工具链

安装与快速开始

NuGet:NewLife.Redis

# 稳定版
dotnet add package NewLife.Redis
# 或开发版 (包含预发布)
dotnet add package NewLife.Redis -v *-*

示例(推荐单例,线程安全):

using NewLife.Caching;
using NewLife.Log;

XTrace.UseConsole();
var rds = new FullRedis("127.0.0.1:6379", "pass", 7)
{
    Log = XTrace.Log,
    ClientLog = XTrace.Log,   // 调试阶段打开
    AutoPipeline = 100        // 达到阈值自动提交管道
};

rds.Set("user:1", new { Name = "Alice", Time = DateTime.Now }, 3600);
var user = rds.Get<String>("user:1");
Console.WriteLine(user);

基础用法

rds.Set("k1", 123, 600);          // 设置并指定过期
var v = rds.Get<Int32>("k1");
var ok = rds.Add("k2", "init");   // 仅在不存在时写入
var old = rds.Replace("k2", "new");
rds.Increment("counter", 1);
rds.Decrement("counter", 2);

过期管理:SetExpire(key, TimeSpan)GetExpire(key)


批量与集合操作

rds.SetAll(new Dictionary<String,Object>{{"a",1},{"b",2},{"c",3}}, 300);
var dict = rds.GetAll<Int32>(new[]{"a","b","c"});

泛型集合:

var list = rds.GetList<String>("queue:demo");
list.Add("job1");
var first = list[0];

注意:基础 Redis 实例仅支持字符串类操作,高级集合请实例化 FullRedis


管道 Pipeline 与自动合并

场景:减少 RTT、提升批量操作吞吐。

var client = rds.StartPipeline();
for (var i = 0; i < 1000; i++) rds.Set($"p:{i}", i);
var results = rds.StopPipeline(); // results 为命令返回集合

自动模式:设置 AutoPipeline = 100 后,写操作累积到阈值自动提交;FullPipeline = true 时读请求也进入管道。


消息 / 队列 / 可靠消费

利用 List + RPOPLPUSH / BRPOPLPUSH 保障“取出-处理中-确认”原子性:

var src = rds.GetList<String>("jobs:ready");
var bak = rds.GetList<String>("jobs:working");
var job = src.RPOPLPUSH(bak.Key); // 取出放入备份队列
// 处理成功后从备份列表删除
bak.Remove(job);

阻塞获取:BRPOPLPUSH(destKey, timeoutSeconds),timeout=0 表示永久阻塞。


序列化与编码器

默认编码:RedisJsonEncoder(内置 JSON 主机)。可通过实现 IPacketEncoder 定制二进制格式以减少内存与网络:

rds.Encoder = new MyBinaryEncoder();

如需共享 JSON 配置或自定义时间/数字格式,可设置:rds.JsonHost = RedisJsonEncoder.GetJsonHost();


性能测试参考

源码内置 Benchmark(Redis.Bench),典型结果(40 逻辑处理器,批量优化):

写入 400,000 项 4 线程  ~576,368 ops
读取 800,000 项 8 线程  ~647,249 ops
删除 800,000 项 8 线程  ~1,011,378 ops

可执行:

rds.Bench(rand:true, batch:100);  // 随机 + 批量

实际性能受网络 RTT / 序列化复杂度 / Value 大小影响。建议单 Value 控制在 1.4KB 附近提升整体效率。


最佳实践与经验

  • 多实例拆分:按 Key 哈希 (CRC16/CRC32) 分布到多 Redis,提高扩展性
  • 合理 Value 大小:控制在 1~2KB;过大分片 / 压缩 / 结构化
  • 批量优先:能 GetAll/SetAll 不循环单键;利用管道降低往返
  • 高可靠消费:RPOPLPUSH + 备份列表手动确认
  • 序列化:二进制优于 JSON;必要时,用池化缓冲减少 GC
  • 性能监控:开启 Counter / Tracer 仅在需要时,避免热路径开销

多实例 & 高可用策略

Server 可配置:"10.0.1.10:6379,10.0.1.11:6379"

  • 发生网络型异常(Socket/IO)时自动切换下一个地址
  • ShieldingTime 控制不可用节点屏蔽窗口
  • 一段时间后自动尝试回切主节点

扩展包 (Extensions)

NewLife.Redis.Extensions 提供 ASP.NET Core 集成:

  • IDistributedCache 后端实现
  • IDataProtection 密钥存储

安装:

dotnet add package NewLife.Redis.Extensions

示例:

builder.Services.AddRedisCaching(options =>
{
    options.Server = "127.0.0.1:6379";
    options.Password = "pass";
});

与 MemoryCache 的协同

建议以 ICache 编程:小数据或临时热点 → MemoryCache;规模上升/跨进程共享 → 切换 Redis/FullRedis,无须修改业务逻辑。


常见问题 FAQ

Q: 是否支持发布订阅 / Stream / Cluster?
A: 基础代码已具备扩展点,发布订阅/更多结构可在 FullRedis 扩展层或后续版本完善。Cluster 分片可通过多实例 + Key 路由策略实现。
Q: 如何处理反序列化失败?
A: TryGetValue 返回是否存在键,即使反序列化失败仍可感知,用于容错与告警。
Q: 如何降低大 Value 带来的慢查询?
A: 拆分结构 + 批量 + 二进制编码 + 控制 MaxMessageSize(默认 1MB)。


路线图 Roadmap

  • [*] 发布订阅友好封装(模式订阅 / 回调)
  • 更完善的分布式锁 / RedLock 支持
  • [*] Stream / 消费组封装
  • 更灵活的二进制/Span Encoder 示例
  • 内置指标导出(Prometheus 适配器)
  • 单测覆盖率提升 & BenchmarkDotNet 场景脚本化

欢迎通过 Issue / PR 参与投票或补充需求。


新生命项目矩阵

(节选) | 项目 | 说明 | | ---- | ---- | | NewLife.Core | 核心库,日志/配置/缓存/序列化/APM | | NewLife.XCode | 大数据 ORM,百亿级 + 分表 + 读写分离 | | NewLife.Net | 超高性能网络库(千万级吞吐) | | Stardust | 分布式服务/配置/注册/发布中心 | | AntJob | 分布式计算 & 调度平台 | | NewLife.RocketMQ | RocketMQ 纯托管客户端 | | ... | 更多见官网与组织首页 |

完整矩阵、企业级解决方案与商业支持请访问:https://newlifex.com


贡献指南 & 社区

  1. 提交前阅读仓库 .github/copilot-instructions.md(编码规范 & 审核清单)
  2. 提交 PR:保持最小变更、添加必要注释与测试说明
  3. Issue:提供版本、运行环境、最小复现场景

社区:QQ群 1600800 / 1600838 ;GitHub Discussions / Issues 参与答疑。


许可证

MIT License。可自由商用 / 修改 / 再发行(无需额外授权)。保留版权声明即可。


新生命开发团队

XCode

团队自 2002 年迄今,维护 80+ .NET / IoT / 分布式相关开源项目,NuGet 累计下载超 400 万。产品与组件已广泛服务于电力、物流、工业控制、教育、通信、文博等行业。
网站:https://newlifex.com | 开源:https://github.com/NewLifeX
微信公众号:

智能大石头


若本文档未覆盖你的使用场景,欢迎提交 Issue 补充;一起让文档更完善!

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  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 is compatible.  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 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 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 is compatible. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.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 (1)

Showing the top 1 NuGet packages that depend on NewLife.Redis.Extensions:

Package Downloads
CodeRule

业务流水号规则生成器,便于快速生成流水号

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
6.3.2025.1001 151 10/1/2025
6.3.2025.1001-beta1743 133 10/1/2025
6.3.2025.914-beta1358 212 9/14/2025
6.3.2025.820-beta1830 151 8/20/2025
6.3.2025.816-beta1657 81 8/16/2025
6.3.2025.801 270 8/1/2025
6.3.2025.801-beta0244 126 8/1/2025
6.3.2025.723-beta0606 548 7/23/2025
6.3.2025.701 288 7/1/2025
6.3.2025.701-beta0404 162 7/1/2025
6.2.2025.629-beta0441 113 6/29/2025
6.2.2025.625-beta0722 155 6/25/2025
6.2.2025.623-beta1320 156 6/23/2025
6.2.2025.601 283 6/1/2025
6.2.2025.601-beta0825 123 6/1/2025
6.2.2025.505-beta1201 167 5/5/2025
6.2.2025.503 189 5/3/2025
6.2.2025.503-beta1516 96 5/3/2025
6.1.2025.426-beta0321 108 4/26/2025
6.1.2025.416-beta1257 199 4/16/2025
6.1.2025.415-beta1726 212 4/15/2025
6.1.2025.411 336 4/11/2025
6.1.2025.411-beta1712 146 4/11/2025
6.1.2025.401-beta0759 177 4/1/2025
6.1.2025.329-beta1226 119 3/29/2025
6.1.2025.328-beta0933 142 3/28/2025
6.1.2025.301 236 3/1/2025
6.1.2025.301-beta0150 114 3/1/2025
6.1.2025.225-beta0533 125 2/25/2025
6.1.2025.224-beta1147 126 2/24/2025
6.1.2025.211-beta1050 160 2/11/2025
6.1.2025.209 340 2/9/2025
6.1.2025.208-beta1528 130 2/8/2025
6.1.2025.202 201 2/2/2025
6.1.2025.202-beta0416 128 2/2/2025
6.1.2025.114-beta0537 129 1/14/2025
6.1.2025.109-beta1738 121 1/9/2025
6.0.2025.103-beta1037 153 1/3/2025
6.0.2025.103-beta0843 139 1/3/2025
6.0.2025.101 228 1/1/2025
6.0.2025.101-beta0851 120 1/1/2025
6.0.2024.1225-beta1617 147 12/25/2024
6.0.2024.1211-beta0604 125 12/11/2024
6.0.2024.1205 247 12/5/2024
6.0.2024.1205-beta1612 117 12/5/2024
6.0.2024.1202 9,426 12/2/2024
6.0.2024.1125-beta1236 122 11/25/2024
6.0.2024.1125-beta1221 117 11/25/2024
6.0.2024.1116-beta0354 117 11/16/2024
6.0.2024.1113-beta0608 107 11/13/2024
6.0.2024.1101 1,303 11/1/2024
6.0.2024.1101-beta1025 112 11/1/2024
6.0.2024.1031-beta0120 121 10/31/2024
6.0.2024.1030-beta0959 113 10/30/2024
6.0.2024.1022-beta1410 119 10/22/2024
6.0.2024.1017-beta0335 125 10/17/2024
6.0.2024.1015-beta1005 112 10/15/2024
6.0.2024.1014-beta0223 114 10/14/2024
6.0.2024.1009-beta0730 125 10/9/2024
6.0.2024.1007-beta0814 121 10/7/2024
6.0.2024.1006 251 10/6/2024
6.0.2024.1006-beta1712 115 10/6/2024
6.0.2024.925-beta1105 119 9/25/2024
6.0.2024.921-beta0817 136 9/21/2024
6.0.2024.919-beta0039 125 9/19/2024
6.0.2024.917-beta0105 119 9/17/2024
6.0.2024.829-beta1443 123 8/29/2024
6.0.2024.829-beta0325 116 8/29/2024
6.0.2024.827-beta0104 120 8/27/2024
5.7.2024.830-beta2338 129 8/30/2024
5.7.2024.801 437 8/1/2024
5.7.2024.801-beta1039 99 8/1/2024
5.5.2024.709 332 7/9/2024
5.5.2024.630-beta0606 133 6/30/2024
5.5.2024.617-beta1505 132 6/17/2024
5.5.2024.602 178 6/2/2024
5.5.2024.602-beta0456 147 6/2/2024
5.5.2024.526-beta1546 138 5/26/2024
5.5.2024.522-beta0209 124 5/22/2024
5.5.2024.508 357 5/8/2024
5.5.2024.508-beta1249 142 5/8/2024
5.5.2024.420-beta0006 133 4/20/2024
5.5.2024.419-beta0858 142 4/19/2024
5.5.2024.411-beta0755 205 4/11/2024
5.5.2024.409-beta0751 143 4/9/2024
5.5.2024.409-beta0726 134 4/9/2024
5.5.2024.403-beta0631 144 4/3/2024
5.5.2024.402 263 4/2/2024
5.5.2024.402-beta1733 128 4/2/2024
5.5.2024.319-beta0028 169 3/19/2024
5.5.2024.317-beta2316 151 3/17/2024
5.5.2024.307-beta0845 140 3/7/2024
5.5.2024.304 495 3/4/2024
5.5.2024.304-beta1306 139 3/4/2024
5.5.2024.223-beta1349 240 2/23/2024
5.5.2024.203 334 2/3/2024
5.5.2024.203-beta1654 109 2/3/2024
5.5.2024.131-beta1504 155 1/31/2024
5.5.2024.123-beta0119 123 1/23/2024
5.5.2024.105 526 1/5/2024
5.5.2024.105-beta0543 143 1/5/2024
5.5.2024.105-beta0302 143 1/5/2024
5.5.2024.104 215 1/4/2024
5.5.2024.104-beta0553 168 1/4/2024
5.5.2024.101 395 1/1/2024
5.5.2024.101-beta0946 145 1/1/2024
5.5.2023.1213-beta0814 174 12/13/2023
5.5.2023.1213-beta0309 147 12/13/2023
5.5.2023.1201 732 12/1/2023
5.5.2023.1201-beta0625 156 12/1/2023
5.5.2023.1129-beta1059 189 11/29/2023
5.5.2023.1129-beta1042 149 11/29/2023
5.5.2023.1129-beta0951 147 11/29/2023
5.5.2023.1129-beta0929 131 11/29/2023
5.5.2023.1128-beta0014 160 11/28/2023
5.5.2023.1116-beta0006 155 11/16/2023
5.5.2023.1109-beta0627 156 11/9/2023
5.5.2023.1103-beta1404 158 11/3/2023
5.5.2023.1102 596 11/2/2023
5.5.2023.1101-beta1333 133 11/1/2023
5.5.2023.1031-beta0703 133 10/31/2023
5.5.2023.1010-beta0839 153 10/10/2023
5.5.2023.1008-beta0422 160 10/8/2023
5.5.2023.1001 563 10/1/2023
5.5.2023.928-beta0019 152 9/28/2023

新增数据保护IDataProtection