Path: redb-route/redb.Route.Redis/RedisConnectionFactory.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Redis/RedisConnectionFactory.cs
----
using StackExchange.Redis;
using redb.Route.Core;
namespace redb.Route.Redis;
///
/// Connection factory for Redis. Register via DI or in the route registry
/// and reference by name in endpoint URIs (connectionFactory=myFactory).
///
public sealed class RedisConnectionFactory
{
/// Redis connection string (e.g., "localhost:6379" or "host1:6379,host2:6379").
[Sensitive]
public string ConnectionString { get; set; } = "localhost:6379";
/// Default database index (default: 0).
public int Database { get; set; }
/// Redis password (optional).
[Sensitive]
public string? Password { get; set; }
/// Redis ACL username (Redis 6+). Use together with Password for ACL authentication.
public string? User { get; set; }
/// Client name for identification in Redis INFO.
public string ClientName { get; set; } = "redb.Route";
// ── Timeouts ──
/// Connection timeout in milliseconds (default: 5000).
public int ConnectTimeout { get; set; } = 5000;
/// Synchronous operation timeout in milliseconds (default: 5000).
public int SyncTimeout { get; set; } = 5000;
/// Async operation timeout in milliseconds (default: 5000).
public int AsyncTimeout { get; set; } = 5000;
/// Number of connection retries (default: 3).
public int ConnectRetry { get; set; } = 3;
/// Keep-alive interval in seconds (default: 60).
public int KeepAlive { get; set; } = 60;
/// Abort on connection fail (default: false — allows reconnect).
public bool AbortOnConnectFail { get; set; }
/// Allow admin commands (default: false).
public bool AllowAdmin { get; set; }
// ── SSL ──
/// Enable SSL encryption.
public bool Ssl { get; set; }
/// SSL host for certificate validation.
public string? SslHost { get; set; }
/// SSL/TLS protocol versions (e.g. "Tls12", "Tls13"). Default = system default.
public string? SslProtocols { get; set; }
/// Enable certificate revocation checking (default: true).
public bool CheckCertificateRevocation { get; set; } = true;
// ── Advanced ──
/// Include detailed diagnostics in exceptions.
public bool IncludeDetailInExceptions { get; set; } = true;
/// Include performance counters in exceptions.
public bool IncludePerformanceCountersInExceptions { get; set; }
// ── Sentinel / Cluster / HA ──
/// Redis Sentinel service name. Required for Sentinel-based HA setups.
public string? ServiceName { get; set; }
/// Tie-breaker key for master detection in multi-master scenarios.
public string? TieBreaker { get; set; }
///
/// Reconnect retry policy: "exponential" or "linear" (default: exponential).
/// Controls backoff during reconnection attempts.
///
public string ReconnectRetryPolicy { get; set; } = "exponential";
/// Channel prefix for Pub/Sub (optional).
public string? ChannelPrefix { get; set; }
/// Creates from this factory's settings.
public ConfigurationOptions Build()
{
var config = ConfigurationOptions.Parse(ConnectionString);
config.DefaultDatabase = Database;
config.ClientName = ClientName;
config.ConnectTimeout = ConnectTimeout;
config.SyncTimeout = SyncTimeout;
config.AsyncTimeout = AsyncTimeout;
config.ConnectRetry = ConnectRetry;
config.KeepAlive = KeepAlive;
config.AbortOnConnectFail = AbortOnConnectFail;
config.AllowAdmin = AllowAdmin;
config.Ssl = Ssl;
config.IncludeDetailInExceptions = IncludeDetailInExceptions;
config.IncludePerformanceCountersInExceptions = IncludePerformanceCountersInExceptions;
config.CheckCertificateRevocation = CheckCertificateRevocation;
if (!string.IsNullOrEmpty(Password))
config.Password = Password;
if (!string.IsNullOrEmpty(User))
config.User = User;
if (!string.IsNullOrEmpty(SslHost))
config.SslHost = SslHost;
// A typo must not silently drop the protocol policy the admin pinned.
// Flags-enum: "Tls12, Tls13" is a valid value.
if (!string.IsNullOrEmpty(SslProtocols))
{
if (!Enum.TryParse(SslProtocols, true, out var protocols))
throw new ArgumentException(
$"Unknown value '{SslProtocols}' for 'sslProtocols'. Valid values: " +
"Tls12, Tls13 (comma-separated for several).");
config.SslProtocols = protocols;
}
if (!string.IsNullOrEmpty(ServiceName))
config.ServiceName = ServiceName;
if (!string.IsNullOrEmpty(TieBreaker))
config.TieBreaker = TieBreaker;
config.ReconnectRetryPolicy = ReconnectRetryPolicy?.Trim().ToLowerInvariant() switch
{
"linear" => new LinearRetry(ConnectTimeout),
_ => new ExponentialRetry(Math.Min(ConnectTimeout, 5000)),
};
if (!string.IsNullOrEmpty(ChannelPrefix))
config.ChannelPrefix = new RedisChannel(ChannelPrefix, RedisChannel.PatternMode.Literal);
return config;
}
}