SyZero.Redis 1.1.4

dotnet add package SyZero.Redis --version 1.1.4
                    
NuGet\Install-Package SyZero.Redis -Version 1.1.4
                    
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" />
                    
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" />
                    
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.4
                    
#r "nuget: SyZero.Redis, 1.1.4"
                    
#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
                    
#: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
                    
Install as a Cake Addin
#tool nuget:?package=SyZero.Redis&version=1.1.4
                    
Install as a Cake Tool

SyZero.Redis

SyZero 框架的 Redis 缓存和分布式锁模块。

📦 安装

dotnet add package SyZero.Redis

✨ 特性

  • 🚀 缓存 - 基于 Redis 的分布式缓存
  • 🔒 分布式锁 - 可靠的分布式锁实现
  • 💾 多模式 - 支持主从、哨兵、集群模式
  • 🔍 服务发现 - 可作为服务注册中心

🚀 快速开始

1. 配置 appsettings.json

{
  "Redis": {
    "Type": "MasterSlave",
    "Master": "localhost:6379,password=123456,defaultDatabase=0",
    "Slave": []
  }
}

2. 注册服务

// Program.cs
var builder = WebApplication.CreateBuilder(args);
// 添加SyZero
builder.AddSyZero();

// 注册服务方式1 - 使用配置文件
builder.Services.AddSyZeroRedis();

// 注册服务方式2 - 使用委托配置
builder.Services.AddSyZeroRedis(options =>
{
    options.Type = RedisType.MasterSlave;
    options.Master = "localhost:6379";
});

// 注册服务方式3 - 添加服务发现
builder.Services.AddSyZeroRedis()
    .AddRedisServiceManagement();

var app = builder.Build();
// 使用SyZero
app.UseSyZero();
app.Run();

3. 使用示例

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 LoadUserFromDbAsync(id);
            await _cache.SetAsync(cacheKey, user, TimeSpan.FromMinutes(30));
        }
        
        return user;
    }

    public async Task CreateOrderAsync(Order order)
    {
        var lockKey = $"order:create:{order.UserId}";
        
        using (await _lockUtil.LockAsync(lockKey, TimeSpan.FromSeconds(30)))
        {
            // 在锁内执行订单创建逻辑
        }
    }
}

📖 配置选项

属性 类型 默认值 说明
Type string "MasterSlave" Redis 模式(MasterSlave/Sentinel/Cluster)
Master string "" 主节点连接字符串
Slave string[] [] 从节点连接字符串列表
Sentinel string[] [] 哨兵节点列表

📖 API 说明

ICache 接口

方法 说明
GetAsync<T>(key) 获取缓存值
SetAsync<T>(key, value, expiration) 设置缓存值
RemoveAsync(key) 移除缓存
ExistsAsync(key) 检查缓存是否存在

ILockUtil 接口

方法 说明
LockAsync(key, expiration) 获取分布式锁

所有方法都有对应的异步版本(带 Async 后缀)


🔧 高级用法

哨兵模式

{
  "Redis": {
    "Type": "Sentinel",
    "Master": "mymaster",
    "Sentinel": ["localhost:26379", "localhost:26380"]
  }
}

集群模式

{
  "Redis": {
    "Type": "Cluster",
    "Master": "localhost:7000",
    "Slave": ["localhost:7001", "localhost:7002"]
  }
}

⚠️ 注意事项

  1. 连接字符串 - 确保 Redis 服务可访问
  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.4 82 1/2/2026
1.1.4-dev.2 40 1/2/2026
1.1.4-dev.1 41 12/30/2025
1.1.3 90 12/30/2025
1.1.3-dev.6 41 12/30/2025
1.1.3-dev.3 109 1/19/2024
1.1.3-dev.2 176 11/3/2023
1.1.3-dev.1 181 3/21/2023
1.1.2 439 3/15/2023
1.1.2-dev.108.29344 177 3/15/2023
1.1.2-dev.108.28054 178 3/15/2023
1.1.2-dev.108.27487 169 3/15/2023
1.1.1 409 3/15/2023
1.1.1-dev.108.14980 174 3/15/2023
1.1.1-dev.108.13289 167 3/15/2023
1.1.1-dev.107.27144 170 3/14/2023
1.1.0 418 3/14/2023
1.1.0-workflow-dev.107.22552 173 3/14/2023
1.1.0-workflow-dev.107.21746 169 3/14/2023
1.1.0-workflow-dev.107.21506 175 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 626 6/13/2020
1.0.1 724 2/20/2020