Smart.StackRedis
4.2.2
dotnet add package Smart.StackRedis --version 4.2.2
NuGet\Install-Package Smart.StackRedis -Version 4.2.2
<PackageReference Include="Smart.StackRedis" Version="4.2.2" />
<PackageVersion Include="Smart.StackRedis" Version="4.2.2" />
<PackageReference Include="Smart.StackRedis" />
paket add Smart.StackRedis --version 4.2.2
#r "nuget: Smart.StackRedis, 4.2.2"
#:package Smart.StackRedis@4.2.2
#addin nuget:?package=Smart.StackRedis&version=4.2.2
#tool nuget:?package=Smart.StackRedis&version=4.2.2
Smart.StackRedis
<a name="english"></a>
English
Smart.StackRedis is a Redis helper library built on StackExchange.Redis. It provides dependency injection support, key prefixing, JSON serialization, Redis data type modules, distributed lock helpers, counters, transactions, and pub/sub with re-subscription after connection restore. It supports .NET 8, 9, and 10.
Installation
dotnet add package Smart.StackRedis
Registration
using Microsoft.Extensions.DependencyInjection;
using Smart.StackRedis;
services.AddSmartRedis(new RedisOptions
{
Server = "127.0.0.1:6379,password=password,abortConnect=false",
Prefix = "prod:",
ConnectTimeout = 15000,
SyncTimeout = 5000,
AbortOnConnectFail = false,
ConnectRetry = 3
});
Examples
public class UserService
{
private readonly SmartRedisService _redis;
public UserService(SmartRedisService redis)
{
_redis = redis;
}
public Task<bool> CacheUserAsync(User user)
{
return _redis.String.SetAsync($"users:{user.Id}", user, TimeSpan.FromHours(1));
}
public Task<User?> GetUserAsync(string userId)
{
return _redis.String.GetAsync<User>($"users:{userId}");
}
}
await redis.Hash.SetAsync("profiles:1", "name", "Tom");
string? name = await redis.Hash.GetAsync("profiles:1", "name");
await redis.List.RightPushAsync("jobs", job);
Job? next = await redis.List.LeftPopAsync<Job>("jobs");
await redis.Set.AddAsync("tags", "redis", "cache");
string[] tags = await redis.Set.MembersAsync("tags");
bool locked = await redis.Lock.TakeAsync("order:1", "worker-1", expiry: 30);
try
{
if (locked)
{
await ProcessOrderAsync();
}
}
finally
{
if (locked)
{
await redis.Lock.ReleaseAsync("order:1", "worker-1");
}
}
await redis.Counter.InitialValueAsync("article:1:views", 0);
long views = await redis.Counter.IncrementAsync("article:1:views");
long current = await redis.Counter.CurrentValueAsync("article:1:views");
redis.SubscribeReceived += (channel, message) =>
{
Console.WriteLine($"Received message on {channel}: {message}");
};
await redis.Subscribe.SubscribeAsync("notifications");
await redis.Subscribe.PublishAsync("notifications", "hello");
Modules
| Module | Description |
|---|---|
Key |
Redis key operations |
String |
String and JSON object storage |
Hash |
Hash field operations |
List |
List operations |
Set |
Set operations |
SortedSet |
Sorted set operations |
Stream |
Redis stream operations |
Lock |
Distributed lock helpers |
Counter |
Atomic counter helpers |
Subscribe |
Publish/subscribe helpers |
<a name="chinese"></a>
中文
Smart.StackRedis 是基于 StackExchange.Redis 的 Redis 工具库,提供依赖注入、键前缀、JSON 序列化、Redis 数据类型模块、分布式锁、计数器、事务和发布订阅能力,并在连接恢复后自动重新订阅。兼容 .NET 8/9/10。
安装
dotnet add package Smart.StackRedis
注册
using Microsoft.Extensions.DependencyInjection;
using Smart.StackRedis;
services.AddSmartRedis(new RedisOptions
{
Server = "127.0.0.1:6379,password=password,abortConnect=false",
Prefix = "prod:",
ConnectTimeout = 15000,
SyncTimeout = 5000,
AbortOnConnectFail = false,
ConnectRetry = 3
});
示例
public class UserService
{
private readonly SmartRedisService _redis;
public UserService(SmartRedisService redis)
{
_redis = redis;
}
public Task<bool> CacheUserAsync(User user)
{
return _redis.String.SetAsync($"users:{user.Id}", user, TimeSpan.FromHours(1));
}
public Task<User?> GetUserAsync(string userId)
{
return _redis.String.GetAsync<User>($"users:{userId}");
}
}
await redis.Hash.SetAsync("profiles:1", "name", "Tom");
string? name = await redis.Hash.GetAsync("profiles:1", "name");
await redis.List.RightPushAsync("jobs", job);
Job? next = await redis.List.LeftPopAsync<Job>("jobs");
await redis.Set.AddAsync("tags", "redis", "cache");
string[] tags = await redis.Set.MembersAsync("tags");
bool locked = await redis.Lock.TakeAsync("order:1", "worker-1", expiry: 30);
try
{
if (locked)
{
await ProcessOrderAsync();
}
}
finally
{
if (locked)
{
await redis.Lock.ReleaseAsync("order:1", "worker-1");
}
}
await redis.Counter.InitialValueAsync("article:1:views", 0);
long views = await redis.Counter.IncrementAsync("article:1:views");
long current = await redis.Counter.CurrentValueAsync("article:1:views");
redis.SubscribeReceived += (channel, message) =>
{
Console.WriteLine($"在 {channel} 收到消息: {message}");
};
await redis.Subscribe.SubscribeAsync("notifications");
await redis.Subscribe.PublishAsync("notifications", "hello");
模块
| 模块 | 说明 |
|---|---|
Key |
Redis Key 操作 |
String |
字符串和 JSON 对象存储 |
Hash |
Hash 字段操作 |
List |
List 操作 |
Set |
Set 操作 |
SortedSet |
Sorted Set 操作 |
Stream |
Redis Stream 操作 |
Lock |
分布式锁 |
Counter |
原子计数器 |
Subscribe |
发布订阅 |
Developed by zenglei
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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 is compatible. 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. |
-
net10.0
- Microsoft.Extensions.DependencyInjection (>= 10.0.8)
- StackExchange.Redis (>= 2.13.1)
-
net8.0
- Microsoft.Extensions.DependencyInjection (>= 10.0.8)
- StackExchange.Redis (>= 2.13.1)
-
net9.0
- Microsoft.Extensions.DependencyInjection (>= 10.0.8)
- StackExchange.Redis (>= 2.13.1)
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 |
|---|---|---|
| 4.2.2 | 41 | 5/16/2026 |
| 4.2.1 | 113 | 3/19/2026 |
| 4.2.0 | 115 | 2/14/2026 |
| 4.1.1 | 119 | 2/11/2026 |
| 4.1.0 | 121 | 2/9/2026 |
| 4.0.1 | 133 | 12/30/2025 |
| 4.0.0 | 213 | 4/5/2025 |
| 3.2.0 | 211 | 3/27/2025 |
| 3.1.1 | 218 | 3/16/2025 |
| 3.1.0 | 166 | 2/26/2025 |
| 3.0.1 | 185 | 2/15/2025 |
| 3.0.0 | 189 | 2/15/2025 |
| 2.1.2 | 188 | 2/13/2025 |
| 2.1.1 | 188 | 2/9/2025 |
| 2.1.0 | 179 | 12/29/2024 |
| 2.0.2 | 215 | 12/7/2024 |
| 2.0.1 | 190 | 12/7/2024 |
| 2.0.0 | 183 | 11/26/2024 |
| 1.2.0 | 189 | 10/31/2024 |
| 1.1.0.1 | 191 | 10/9/2024 |