Path: redb-route/redb.Route.Cache/RouteCacheOptions.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.Cache/RouteCacheOptions.cs
----
using System.Globalization;
namespace redb.Route.Cache;
/// Which cache backs a node.
public enum CacheProvider
{
/// In-process IMemoryCache (created on demand when none is registered).
Memory,
/// IDistributedCache registered in DI or on the context (Redis, SQL Server, …).
Distributed,
}
/// Package-wide settings: context.UseCache(o => …) / services.AddRedbRouteCache(o => …).
public sealed class RouteCacheOptions
{
/// Upper bound on entries of the package-created in-process cache (one unit per entry); null = unbounded. Ignored for a cache registered by the host.
public long? MaxEntries { get; set; }
/// TTL applied when a node does not give one; null = no expiration.
public TimeSpan? DefaultTtl { get; set; }
/// Provider used when a node does not choose one. Default .
public CacheProvider DefaultProvider { get; set; } = CacheProvider.Memory;
}
/// Durations as people write them in URIs: 500ms, 30s, 5m, 2h, 1d, or hh:mm:ss.
public static class CacheDuration
{
/// Parses a duration; null / empty → null. Anything unusable is a .
public static TimeSpan? Parse(string? text)
{
if (string.IsNullOrWhiteSpace(text)) return null;
var value = text.Trim();
TimeSpan? parsed = null;
foreach (var (suffix, millisecondsPerUnit) in Units)
{
if (!value.EndsWith(suffix, StringComparison.OrdinalIgnoreCase)) continue;
var number = value[..^suffix.Length].Trim();
if (double.TryParse(number, NumberStyles.Float, CultureInfo.InvariantCulture, out var amount))
{
// A number TimeSpan cannot hold (too large, NaN, infinity) is a typo like any other, so it
// is the same FormatException, not the OverflowException / ArgumentException of TimeSpan.
var milliseconds = amount * millisecondsPerUnit;
if (!(Math.Abs(milliseconds) <= TimeSpan.MaxValue.TotalMilliseconds))
throw new FormatException($"'{text}' is not a duration a TimeSpan can hold (at most {TimeSpan.MaxValue.TotalDays:F0} days).");
parsed = TimeSpan.FromMilliseconds(milliseconds);
break;
}
}
// A bare number is ambiguous — TimeSpan would read "5" as five days — so a unit is required.
if (parsed is null && double.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out _))
throw new FormatException($"'{text}' has no unit: write 5s, 5m, 2h, 1d or hh:mm:ss.");
if (parsed is null && TimeSpan.TryParse(value, CultureInfo.InvariantCulture, out var span))
parsed = span;
if (parsed is null)
throw new FormatException($"'{text}' is not a duration (use 500ms, 30s, 5m, 2h, 1d or hh:mm:ss).");
if (parsed.Value <= TimeSpan.Zero)
throw new FormatException($"'{text}' is not a positive duration.");
return parsed;
}
private static readonly (string Suffix, double MillisecondsPerUnit)[] Units =
[
("ms", 1),
("s", 1_000),
("m", 60_000),
("h", 3_600_000),
("d", 86_400_000),
];
}