SyZero.Redis 1.1.8

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

SyZero.Redis

SyZero.Redis 提供 Redis 缓存、分布式锁、Redis 服务管理和 Redis 事件总线能力。

📦 安装

dotnet add package SyZero.Redis

✨ 特性

  • 🚀 分布式缓存 - ICache 基于 Redis 实现
  • 🔒 分布式锁 - ILockUtil 基于 Redis 实现
  • 🔍 服务发现 - RedisServiceManagement 支持注册、发现、健康检查、Pub/Sub 通知
  • 📣 事件总线 - RedisEventBus 基于 Redis Pub/Sub 实现跨实例广播
  • 💾 多模式连接 - 支持主从、哨兵、集群模式

🚀 快速开始

1. 配置 appsettings.json

{
  "Redis": {
    "Type": "MasterSlave",
    "Master": "localhost:6379,password=123456,defaultDatabase=0",
    "Slave": []
  },
  "RedisServiceManagement": {
    "EnableHealthCheck": true,
    "EnableLeaderElection": true,
    "EnablePubSub": true
  },
  "RedisEventBus": {
    "ChannelPrefix": "SyZero:EventBus:"
  }
}

2. 注册服务

using SyZero;

var builder = WebApplication.CreateBuilder(args);

builder.AddSyZero();
builder.Services.AddSyZeroRedis();

// 可选:Redis 服务管理
builder.Services.AddRedisServiceManagement();

// 可选:Redis 事件总线
builder.Services.AddRedisEventBus();

var app = builder.Build();
app.UseSyZero();
app.Run();

📖 缓存与锁

public class UserService
{
    private readonly ICache _cache;
    private readonly ILockUtil _lockUtil;

    public UserService(ICache cache, ILockUtil lockUtil)
    {
        _cache = cache;
        _lockUtil = lockUtil;
    }

    public async Task<User> GetUserAsync(long id)
    {
        var cacheKey = $"user:{id}";
        var user = await _cache.GetAsync<User>(cacheKey);

        if (user == null)
        {
            user = await LoadFromDbAsync(id);
            await _cache.SetAsync(cacheKey, user, 1800);
        }

        return user;
    }

    public async Task CreateOrderAsync(string orderNo)
    {
        using (await _lockUtil.LockAsync($"order:{orderNo}", TimeSpan.FromSeconds(30)))
        {
            // 在锁内执行业务逻辑
        }
    }
}

📖 Redis 服务管理

RedisServiceManagement 适合简单分布式部署,支持:

  • 服务注册 / 注销
  • 心跳与健康检查
  • Leader 选举
  • Redis Pub/Sub 实时通知
builder.Services.AddSyZeroRedis();
builder.Services.AddRedisServiceManagement(options =>
{
    options.EnableHealthCheck = true;
    options.EnableLeaderElection = true;
    options.EnablePubSub = true;
});

📖 Redis 事件总线

RedisEventBus 基于 Redis Pub/Sub,适合需要跨实例广播、但不要求持久化和可靠重试的场景。

如果你需要持久化、重试或死信队列,请改用 DBEventBusRabbitMQEventBus

注册

builder.Services.AddSyZeroRedis();
builder.Services.AddRedisEventBus(options =>
{
    options.ChannelPrefix = "SyZero:EventBus:";
});

定义事件与处理器

using SyZero.EventBus;

public class OrderCreatedEvent : EventBase
{
    public long OrderId { get; set; }
}

public class OrderCreatedHandler : IEventHandler<OrderCreatedEvent>
{
    public Task HandleAsync(OrderCreatedEvent @event)
    {
        Console.WriteLine($"order created: {@event.OrderId}");
        return Task.CompletedTask;
    }
}

订阅与发布

public class OrderPublisher
{
    private readonly IEventBus _eventBus;

    public OrderPublisher(IEventBus eventBus)
    {
        _eventBus = eventBus;
        _eventBus.Subscribe<OrderCreatedEvent, OrderCreatedHandler>(() => new OrderCreatedHandler());
    }

    public Task PublishAsync(long orderId)
    {
        return _eventBus.PublishAsync(new OrderCreatedEvent
        {
            OrderId = orderId
        });
    }
}

📖 配置项

Redis

属性 类型 说明
Type MasterSlave / Sentinel / Cluster Redis 模式
Master string 主节点连接串或主服务名
Slave string[] 从节点列表
Sentinel string[] 哨兵节点列表

RedisServiceManagement

属性 默认值 说明
KeyPrefix syzero:services: 服务注册 Key 前缀
LeaderKeyPrefix syzero:leader: Leader 锁前缀
ServiceNamesKey syzero:service:names 服务名集合 Key
EnableHealthCheck true 是否启用健康检查
ServiceExpireSeconds 30 多久未心跳标记不健康
AutoCleanExpiredServices true 是否自动清理过期实例
EnableLeaderElection true 是否启用 Leader 选举
EnablePubSub true 是否启用服务变更通知

RedisEventBus

属性 默认值 说明
ChannelPrefix SyZero:EventBus: 事件总线频道前缀

⚠️ 注意事项

  1. RedisEventBus 使用 Pub/Sub,不提供事件持久化、重试和死信队列。
  2. 订阅关系保存在当前进程内,应用重启后需要重新执行订阅。
  3. 建议将订阅逻辑放在应用启动阶段或长期存活服务中,而不是高频短生命周期对象里重复订阅。

📄 许可证

MIT License - 详见 LICENSE

Product 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.

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.9 104 4/19/2026
1.1.9-dev.2 51 4/19/2026
1.1.9-dev.1 52 4/17/2026
1.1.8 94 4/17/2026
1.1.6 95 4/17/2026
1.1.6-dev.1 45 4/17/2026
1.1.5 110 4/13/2026
1.1.5-dev.3 58 4/13/2026
1.1.5-dev.2 70 2/11/2026
1.1.5-dev.1 67 1/29/2026
1.1.4 142 1/2/2026
1.1.4-dev.2 72 1/2/2026
1.1.4-dev.1 73 12/30/2025
1.1.3 133 12/30/2025
1.1.3-dev.6 70 12/30/2025
1.1.3-dev.3 130 1/19/2024
1.1.3-dev.2 207 11/3/2023
1.1.3-dev.1 208 3/21/2023
1.1.2 461 3/15/2023
1.0.0 113 4/17/2026
Loading failed