Smart.StackRedis 3.0.1

dotnet add package Smart.StackRedis --version 3.0.1                
NuGet\Install-Package Smart.StackRedis -Version 3.0.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="Smart.StackRedis" Version="3.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Smart.StackRedis --version 3.0.1                
#r "nuget: Smart.StackRedis, 3.0.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.
// Install Smart.StackRedis as a Cake Addin
#addin nuget:?package=Smart.StackRedis&version=3.0.1

// Install Smart.StackRedis as a Cake Tool
#tool nuget:?package=Smart.StackRedis&version=3.0.1                

Smart.StackRedis

基于 StackExchange.Redis 封装的现代化 Redis 客户端,支持 .NET6/8/9 平台和服务依赖注入,提供类型安全且符合现代.NET开发习惯的操作接口。

NuGet Version

特性

  • 原生依赖注入支持
  • 全异步操作(async/await)
  • 全局键前缀自动附加
  • 内置分布式锁和原子操作
  • 完整的发布订阅支持
  • 模块化数据类型操作
  • 自动重连和连接池管理
  • JSON序列化集成

安装

bash
dotnet add package Smart.StackRedis

快速开始

服务注册

csharp
builder.Services.AddSmartRedis(new RedisOptions
{
  ConnectionString = Configuration.GetConnectionString("Redis"),
  KeyPrefix = "prod:"
});

基础使用

csharp
public class DataService(SmartRedisService redis) {
// 字符串操作
public async Task CacheUser(User user)
=> await redis.String.SetAsync($"users:{user.Id}", user);

// 哈希表操作
public async Task UpdateProfile(string userId, string field, object value)
=> await redis.Hash.SetAsync($"profiles:{userId}", field, value);

// 发布订阅
public async Task Notify(string message)
=> await redis.Subscribe.PublishAsync("notifications", message);
}

核心模块

模块 功能描述 示例方法
RedisString 字符串/对象存储 SetAsync<T>, GetAsync<T>
RedisHash 哈希表字段操作 SetAsync, GetAllAsync
RedisList 队列/栈操作 LeftPushAsync, RangeAsync
RedisStream 消息流处理 AddAsync, ReadGroupAsync
RedisLock 分布式锁 AcquireAsync, ReleaseAsync
RedisCounter 原子计数器 IncrementAsync, GetAsync
RedisSubscribe 发布订阅 PublishAsync, SubscribeAsync

配置选项

csharp
public class RedisOptions
{
  public string ConnectionString { get; set; } // Redis连接字符串
  public string KeyPrefix { get; set; } // 全局键前缀
}
json
// appsettings.json
{
"Redis": {
"ConnectionString": "localhost:6379,connectRetry=3",
"KeyPrefix": "prod:"
}
}

最佳实践

  1. 键设计规范
    业务:环境:实体:ID 格式,如 orders:prod:2024

  2. 批量操作优化

csharp
// 使用Pipeline批量写入
var batch = db.CreateBatch();
batch.StringSetAsync("key1", "value1");
batch.StringSetAsync("key2", "value2");
batch.Execute();
  1. 异常处理
csharp
try
{
  await redis.String.SetAsync(...);
}
catch (RedisConnectionException ex)
{
  // 处理连接异常
}

注意事项

⚠️ 连接字符串必须包含 abortConnect=false
⚠️ 敏感配置建议使用加密存储
⚠️ 避免在热路径中频繁创建连接
⚠️ 发布订阅需处理消息积压问题
⚠️ Stream操作要求Redis 5.0+版本

Developed by zenglei

Product Compatible and additional computed target framework versions.
.NET 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 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.0.1 48 2/15/2025
3.0.0 49 2/15/2025
2.1.2 47 2/13/2025
2.1.1 71 2/9/2025
2.1.0 90 12/29/2024
2.0.2 110 12/7/2024
2.0.1 94 12/7/2024
2.0.0 91 11/26/2024
1.2.0 93 10/31/2024
1.1.0.1 103 10/9/2024
1.1.0 120 9/27/2024
1.0.0.1 119 9/26/2024
1.0.0 111 9/26/2024