SyZero.Redis
1.1.4-dev.1
This is a prerelease version of SyZero.Redis.
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package SyZero.Redis --version 1.1.4-dev.1
NuGet\Install-Package SyZero.Redis -Version 1.1.4-dev.1
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="SyZero.Redis" Version="1.1.4-dev.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SyZero.Redis" Version="1.1.4-dev.1" />
<PackageReference Include="SyZero.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 SyZero.Redis --version 1.1.4-dev.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: SyZero.Redis, 1.1.4-dev.1"
#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 SyZero.Redis@1.1.4-dev.1
#: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=SyZero.Redis&version=1.1.4-dev.1&prerelease
#tool nuget:?package=SyZero.Redis&version=1.1.4-dev.1&prerelease
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
SyZero.Redis
基于 FreeRedis 的 Redis 缓存组件,提供缓存操作和分布式锁支持。
📦 安装
dotnet add package SyZero.Redis
✨ 特性
- 🚀 多种部署模式 - 支持主从、哨兵、集群三种模式
- 💾 缓存接口 - 实现
ICache接口,提供统一的缓存操作 - 🔒 分布式锁 - 实现
ILockUtil接口,支持带等待的分布式锁 - 🌐 服务管理 - 实现
IServiceManagement接口,支持服务注册发现 - ⚡ 高性能 - 基于 FreeRedis,高性能 Redis 客户端
- 🔄 异步支持 - 所有操作都提供异步版本
- 🏥 健康检查 - 内置服务健康检查与自动清理机制
- 🗳️ Leader 选举 - 支持多实例部署的 Leader 选举机制
🚀 快速开始
1. 配置 Redis
在 appsettings.json 中添加 Redis 配置:
主从模式
{
"Redis": {
"Type": 0,
"Master": "127.0.0.1:6379,password=123456,defaultDatabase=0",
"Slave": [
"127.0.0.1:6380,password=123456,defaultDatabase=0",
"127.0.0.1:6381,password=123456,defaultDatabase=0"
]
}
}
哨兵模式
{
"Redis": {
"Type": 1,
"Master": "mymaster,password=123456,defaultDatabase=0",
"Sentinel": [
"127.0.0.1:26379",
"127.0.0.1:26380",
"127.0.0.1:26381"
]
}
}
集群模式
{
"Redis": {
"Type": 2,
"Master": "127.0.0.1:6379,password=123456",
"Slave": [
"127.0.0.1:6380,password=123456",
"127.0.0.1:6381,password=123456"
]
}
}
部署类型 Type 对应值:
| 值 | 模式 | 说明 |
|---|------|------|
| 0 | MasterSlave | 主从模式 |
| 1 | Sentinel | 哨兵模式 |
| 2 | Cluster | 集群模式 |
2. 注册服务
// Program.cs
var builder = WebApplication.CreateBuilder(args);
// 添加 Redis 服务
builder.Services.AddSyZeroRedis();
var app = builder.Build();
app.Run();
3. 使用缓存
public class UserService
{
private readonly ICache _cache;
public UserService(ICache cache)
{
_cache = cache;
}
public async Task<User> GetUserAsync(long id)
{
var cacheKey = $"user:{id}";
// 从缓存获取
var user = _cache.Get<User>(cacheKey);
if (user != null)
{
return user;
}
// 从数据库获取
user = await GetUserFromDbAsync(id);
// 写入缓存(过期时间 1 小时)
await _cache.SetAsync(cacheKey, user, 3600);
return user;
}
}
📖 API 说明
ICache 接口
| 方法 | 说明 |
|---|---|
Exist(string key) |
检查键是否存在 |
Get<T>(string key) |
获取缓存值 |
GetKeys(string pattern) |
按模式获取所有键 |
Set<T>(string key, T value, int expireTime) |
设置缓存值(默认过期时间 24 小时) |
Remove(string key) |
删除缓存 |
Refresh(string key) |
刷新缓存 |
所有方法都有对应的异步版本(带
Async后缀)
使用示例
// 设置缓存(过期时间 1 小时 = 3600 秒)
_cache.Set("myKey", myObject, 3600);
// 获取缓存
var obj = _cache.Get<MyClass>("myKey");
// 检查是否存在
if (_cache.Exist("myKey"))
{
// ...
}
// 删除缓存
_cache.Remove("myKey");
// 批量获取键
var keys = _cache.GetKeys("user:*");
🔒 分布式锁
ILockUtil 接口
| 方法 | 说明 |
|---|---|
GetLock(string lockKey, int expiresSenconds, int waitTimeSenconds) |
获取锁 |
GetLockAsync(...) |
异步获取锁 |
Release(string lockKey) |
释放锁 |
参数说明
| 参数 | 默认值 | 说明 |
|---|---|---|
lockKey |
- | 锁的键名 |
expiresSenconds |
10 | 锁的过期时间(秒),防止死锁 |
waitTimeSenconds |
10 | 等待获取锁的最大时间(秒),0 表示不等待 |
使用示例
public class OrderService
{
private readonly ILockUtil _lockUtil;
public OrderService(ILockUtil lockUtil)
{
_lockUtil = lockUtil;
}
public async Task<bool> ProcessOrderAsync(long orderId)
{
var lockKey = $"order:lock:{orderId}";
// 尝试获取锁(过期时间 30 秒,等待时间 10 秒)
if (!await _lockUtil.GetLockAsync(lockKey, 30, 10))
{
throw new Exception("获取锁失败,请稍后重试");
}
try
{
// 执行业务逻辑
await DoBusinessLogicAsync(orderId);
return true;
}
finally
{
// 释放锁
_lockUtil.Release(lockKey);
}
}
}
不等待的锁
// waitTimeSenconds = 0 时,立即返回获取结果
if (_lockUtil.GetLock("myLock", expiresSenconds: 10, waitTimeSenconds: 0))
{
try
{
// 获取成功,执行操作
}
finally
{
_lockUtil.Release("myLock");
}
}
else
{
// 获取失败,资源已被占用
}
🔧 高级用法
直接使用 RedisClient
如果需要更底层的 Redis 操作,可以直接注入 RedisClient:
public class MyService
{
private readonly RedisClient _redis;
public MyService(RedisClient redis)
{
_redis = redis;
}
public void DoSomething()
{
// 使用 FreeRedis 原生 API
_redis.HSet("myhash", "field1", "value1");
_redis.LPush("mylist", "item1");
_redis.ZAdd("myset", 1, "member1");
// 发布订阅
_redis.Publish("channel", "message");
}
}
缓存模式封装
public async Task<T> GetOrSetAsync<T>(string key, Func<Task<T>> factory, int expireSeconds = 3600)
{
var value = _cache.Get<T>(key);
if (value != null)
{
return value;
}
value = await factory();
if (value != null)
{
await _cache.SetAsync(key, value, expireSeconds);
}
return value;
}
// 使用
var user = await GetOrSetAsync(
$"user:{id}",
async () => await _userRepository.GetAsync(id),
3600
);
📁 项目结构
SyZero.Redis/
├── Cache.cs # ICache 实现
├── LockUtil.cs # ILockUtil 实现(分布式锁)
├── RedisOptions.cs # Redis 配置选项
├── RedisServiceManagement.cs # IServiceManagement 实现(服务管理)
├── RedisServiceManagementOptions.cs # 服务管理配置选项
└── SyZeroRedisExtension.cs # 依赖注入扩展方法
🌐 服务管理
Redis 服务管理实现了 IServiceManagement 接口,适用于分布式部署场景。
配置
在 appsettings.json 中添加配置:
{
"Redis": {
"Type": 0,
"Master": "127.0.0.1:6379,password=123456,defaultDatabase=0"
},
"RedisServiceManagement": {
"EnableHealthCheck": true,
"HealthCheckIntervalSeconds": 10,
"ServiceExpireSeconds": 30,
"AutoCleanExpiredServices": true,
"AutoCleanIntervalSeconds": 300,
"ServiceCleanSeconds": 600,
"EnableLeaderElection": true,
"LeaderLockExpireSeconds": 30,
"LeaderLockRenewIntervalSeconds": 10,
"EnablePubSub": true
}
}
注册服务
// Program.cs
var builder = WebApplication.CreateBuilder(args);
// 添加 Redis 服务管理
builder.Services.AddRedisServiceManagement();
// 或使用自定义配置
builder.Services.AddRedisServiceManagement(options =>
{
options.EnableHealthCheck = true;
options.HealthCheckIntervalSeconds = 10;
options.EnableLeaderElection = true;
});
var app = builder.Build();
app.Run();
使用示例
public class MyService
{
private readonly IServiceManagement _serviceManagement;
public MyService(IServiceManagement serviceManagement)
{
_serviceManagement = serviceManagement;
}
// 注册服务
public async Task RegisterAsync()
{
await _serviceManagement.RegisterService(new ServiceInfo
{
ServiceName = "MyService",
ServiceAddress = "192.168.1.100",
ServicePort = 5000,
HealthCheckUrl = "http://192.168.1.100:5000/health"
});
}
// 获取服务实例(负载均衡)
public async Task<ServiceInfo> GetServiceAsync()
{
return await _serviceManagement.GetServiceInstance("MyService");
}
// 订阅服务变更
public async Task SubscribeAsync()
{
await _serviceManagement.Subscribe("MyService", services =>
{
Console.WriteLine($"服务列表更新,当前实例数: {services.Count}");
});
}
}
配置选项说明
| 配置项 | 默认值 | 说明 |
|---|---|---|
KeyPrefix |
syzero:services: |
服务注册 Key 前缀 |
LeaderKeyPrefix |
syzero:leader: |
Leader 选举 Key 前缀 |
EnableHealthCheck |
true |
是否启用健康检查 |
HealthCheckIntervalSeconds |
10 |
健康检查间隔(秒) |
HealthCheckTimeoutSeconds |
5 |
健康检查超时时间(秒) |
ServiceExpireSeconds |
30 |
服务过期时间(秒) |
AutoCleanExpiredServices |
true |
是否自动清理过期服务 |
AutoCleanIntervalSeconds |
300 |
自动清理间隔(秒) |
ServiceCleanSeconds |
600 |
服务清理时间(秒) |
EnableLeaderElection |
true |
是否启用 Leader 选举 |
LeaderLockExpireSeconds |
30 |
Leader 锁过期时间(秒) |
LeaderLockRenewIntervalSeconds |
10 |
Leader 锁续期间隔(秒) |
EnablePubSub |
true |
是否启用发布/订阅通知 |
Leader 选举
当多个服务实例同时运行时,启用 Leader 选举可避免并发执行健康检查和清理操作:
- Leader 实例:负责执行健康检查和过期服务清理
- 非 Leader 实例:只进行服务注册和查询,不执行健康检查
Leader 选举基于 Redis 的 SETNX 命令实现,具有原子性保证。
⚠️ 注意事项
- 连接字符串格式 - 使用 FreeRedis 的连接字符串格式:
host:port,password=xxx,defaultDatabase=0 - 锁的释放 - 使用分布式锁时,务必在
finally块中释放锁 - 过期时间 - 合理设置缓存过期时间,避免内存溢出
- 哨兵模式 - Master 参数填写哨兵配置的主节点名称,而非 IP 地址
📄 许可证
MIT License - 详见 LICENSE
| Product | Versions 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 | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | 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.
-
.NETStandard 2.1
- FreeRedis (>= 0.3.5)
- Newtonsoft.Json (>= 13.0.1)
- SyZero (>= 1.1.4-dev.1)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on SyZero.Redis:
| Package | Downloads |
|---|---|
|
SyZero.Gateway
SyZero-Gateway |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.1.4 | 83 | 1/2/2026 |
| 1.1.4-dev.2 | 41 | 1/2/2026 |
| 1.1.4-dev.1 | 42 | 12/30/2025 |
| 1.1.3 | 91 | 12/30/2025 |
| 1.1.3-dev.6 | 42 | 12/30/2025 |
| 1.1.3-dev.3 | 109 | 1/19/2024 |
| 1.1.3-dev.2 | 177 | 11/3/2023 |
| 1.1.3-dev.1 | 181 | 3/21/2023 |
| 1.1.2 | 440 | 3/15/2023 |
| 1.1.2-dev.108.29344 | 178 | 3/15/2023 |
| 1.1.2-dev.108.28054 | 179 | 3/15/2023 |
| 1.1.2-dev.108.27487 | 170 | 3/15/2023 |
| 1.1.1 | 410 | 3/15/2023 |
| 1.1.1-dev.108.14980 | 174 | 3/15/2023 |
| 1.1.1-dev.108.13289 | 169 | 3/15/2023 |
| 1.1.1-dev.107.27144 | 171 | 3/14/2023 |
| 1.1.0 | 418 | 3/14/2023 |
| 1.1.0-workflow-dev.107.22552 | 174 | 3/14/2023 |
| 1.1.0-workflow-dev.107.21746 | 170 | 3/14/2023 |
| 1.1.0-workflow-dev.107.21506 | 176 | 3/14/2023 |
| 1.1.0-workflow-dev.107.20979 | 167 | 3/14/2023 |
| 1.1.0-dev.107.26364 | 168 | 3/14/2023 |
| 1.1.0-dev.107.24396 | 162 | 3/14/2023 |
| 1.1.0-dev.107.22787 | 173 | 3/14/2023 |
| 1.0.6 | 613 | 3/5/2022 |
| 1.0.4 | 627 | 6/13/2020 |
| 1.0.1 | 725 | 2/20/2020 |