StackRedis.Uri
1.0.1
dotnet add package StackRedis.Uri --version 1.0.1
NuGet\Install-Package StackRedis.Uri -Version 1.0.1
<PackageReference Include="StackRedis.Uri" Version="1.0.1" />
<PackageVersion Include="StackRedis.Uri" Version="1.0.1" />
<PackageReference Include="StackRedis.Uri" />
paket add StackRedis.Uri --version 1.0.1
#r "nuget: StackRedis.Uri, 1.0.1"
#:package StackRedis.Uri@1.0.1
#addin nuget:?package=StackRedis.Uri&version=1.0.1
#tool nuget:?package=StackRedis.Uri&version=1.0.1
StackRedis.Uri
A .NET 8 class library that parses redis:// and rediss:// URI strings into StackExchange.Redis ConfigurationOptions, with a ready-made Redis service and ASP.NET Core DI registration. Works with any Redis provider — Upstash, Redis Cloud, Fly.io, self-hosted, etc.
Installation
dotnet add package StackRedis.Uri
Configuration
Add the following to your appsettings.json:
"RedisConfig": {
"ConnectionString": "redis://default:your-password@your-host:6379"
}
Use
rediss://and port6380for TLS connections (e.g. Upstash over TLS).
| Scheme | Description |
|---|---|
redis:// |
Plain TCP connection |
rediss:// |
TLS/SSL connection (TLS 1.2 + 1.3 enabled automatically) |
URI format: redis[s]://[username]:[password]@[host]:[port]
Registration
From configuration section
// Program.cs
builder.Services.AddRedisFromUri(builder.Configuration.GetSection("RedisConfig"));
From a URI string directly
builder.Services.AddRedisFromUri("rediss://default:password@host:6380");
Lazy connection (connects on first use)
builder.Services.AddRedisFromUriLazy(builder.Configuration.GetSection("RedisConfig"));
// or
builder.Services.AddRedisFromUriLazy("rediss://default:password@host:6380");
All overloads register the following as singletons:
IConnectionMultiplexerIDatabaseIRedisService
Usage
Inject IRedisService
public class MyService(IRedisService redis)
{
public async Task CacheUserAsync(string userId, string data)
{
await redis.SetStringAsync($"user:{userId}", data, expirationMinutes: 60);
}
public async Task<string?> GetUserAsync(string userId)
{
return await redis.GetStringAsync($"user:{userId}");
}
public async Task InvalidateUserAsync(string userId)
{
await redis.DeleteAsync($"user:{userId}");
}
}
Inject IConnectionMultiplexer (advanced)
public class MyService(IConnectionMultiplexer redis)
{
public async Task PublishAsync(string channel, string message)
{
var pub = redis.GetSubscriber();
await pub.PublishAsync(channel, message);
}
}
API Reference
IRedisService
| Method | Description |
|---|---|
SetStringAsync(key, value, expirationMinutes) |
Stores a string value with a TTL |
GetStringAsync(key) |
Returns the value, or null if missing or expired |
DeleteAsync(key) |
Deletes a single key |
ExistsAsync(key) |
Returns true if the key exists |
ExistsBatchAsync(keys) |
Returns a HashSet<string> of which keys exist (batched pipeline) |
DeleteByPrefixAsync(prefix) |
Deletes all keys matching a prefix (e.g. "user:") |
RedisUriParser
Parses a URI string directly into ConfigurationOptions without DI.
var options = RedisUriParser.Parse("rediss://default:password@host:6380");
// With custom overrides
var options = RedisUriParser.Parse("rediss://default:password@host:6380", opt =>
{
opt.ConnectTimeout = 5000;
opt.SyncTimeout = 1000;
});
RedisConnectionFactory
Creates a ConnectionMultiplexer directly without DI.
// Async
var connection = await RedisConnectionFactory.ConnectAsync("rediss://default:password@host:6380");
// Sync
var connection = RedisConnectionFactory.Connect("rediss://default:password@host:6380");
IDatabase db = connection.GetDatabase();
RedisConfig
Options class for binding Redis connection settings from appsettings.json. Use this when you want to bind configuration manually without the DI extensions.
| Property | Type | Default | Description |
|---|---|---|---|
ConnectionString |
string |
"" |
The redis:// or rediss:// URI |
Manual binding example:
var redisConfig = new RedisConfig();
builder.Configuration.GetSection("RedisConfig").Bind(redisConfig);
var options = RedisUriParser.Parse(redisConfig.ConnectionString);
IOptions<RedisConfig> pattern:
// Program.cs
builder.Services.Configure<RedisConfig>(builder.Configuration.GetSection("RedisConfig"));
// Your service
public class MyService(IOptions<RedisConfig> redisConfig)
{
private readonly string _uri = redisConfig.Value.ConnectionString;
}
Default Connection Settings
| Setting | Value |
|---|---|
AbortOnConnectFail |
false |
ConnectTimeout |
15 000 ms |
SyncTimeout |
3 000 ms |
AsyncTimeout |
3 000 ms |
ConnectRetry |
3 |
ReconnectRetryPolicy |
Exponential backoff (base 5 000 ms) |
KeepAlive |
60 s |
| TLS (rediss://) | TLS 1.2 + TLS 1.3 |
All defaults can be overridden via the optional configure delegate on any registration method.
Requirements
- .NET 8
- A Redis instance accessible via a
redis://orrediss://URI
| 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 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. |
-
net8.0
- Microsoft.Extensions.Configuration.Abstractions (>= 8.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.3)
- StackExchange.Redis (>= 2.12.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.