diff --git a/src/Caching/StackExchangeRedis/src/PublicAPI.Unshipped.txt b/src/Caching/StackExchangeRedis/src/PublicAPI.Unshipped.txt index 93b5d12522b9..1312348c8fb3 100644 --- a/src/Caching/StackExchangeRedis/src/PublicAPI.Unshipped.txt +++ b/src/Caching/StackExchangeRedis/src/PublicAPI.Unshipped.txt @@ -1,3 +1,5 @@ #nullable enable ~Microsoft.Extensions.Caching.StackExchangeRedis.RedisCacheOptions.ProfilingSession.get -> System.Func ~Microsoft.Extensions.Caching.StackExchangeRedis.RedisCacheOptions.ProfilingSession.set -> void +~Microsoft.Extensions.Caching.StackExchangeRedis.RedisCacheOptions.ConnectionMultiplexerFactory.get -> System.Func> +~Microsoft.Extensions.Caching.StackExchangeRedis.RedisCacheOptions.ConnectionMultiplexerFactory.set -> void diff --git a/src/Caching/StackExchangeRedis/src/RedisCache.cs b/src/Caching/StackExchangeRedis/src/RedisCache.cs index 25704e1a9218..b377f47bae42 100644 --- a/src/Caching/StackExchangeRedis/src/RedisCache.cs +++ b/src/Caching/StackExchangeRedis/src/RedisCache.cs @@ -33,7 +33,7 @@ public class RedisCache : IDistributedCache, IDisposable private const string DataKey = "data"; private const long NotPresent = -1; - private volatile ConnectionMultiplexer _connection; + private volatile IConnectionMultiplexer _connection; private IDatabase _cache; private bool _disposed; @@ -190,13 +190,20 @@ private void Connect() { if (_cache == null) { - if (_options.ConfigurationOptions != null) + if(_options.ConnectionMultiplexerFactory == null) { - _connection = ConnectionMultiplexer.Connect(_options.ConfigurationOptions); + if (_options.ConfigurationOptions is not null) + { + _connection = ConnectionMultiplexer.Connect(_options.ConfigurationOptions); + } + else + { + _connection = ConnectionMultiplexer.Connect(_options.Configuration); + } } else { - _connection = ConnectionMultiplexer.Connect(_options.Configuration); + _connection = _options.ConnectionMultiplexerFactory().GetAwaiter().GetResult(); } TryRegisterProfiler(); @@ -224,13 +231,20 @@ private void Connect() { if (_cache == null) { - if (_options.ConfigurationOptions != null) + if(_options.ConnectionMultiplexerFactory == null) { - _connection = await ConnectionMultiplexer.ConnectAsync(_options.ConfigurationOptions).ConfigureAwait(false); + if (_options.ConfigurationOptions is not null) + { + _connection = await ConnectionMultiplexer.ConnectAsync(_options.ConfigurationOptions).ConfigureAwait(false); + } + else + { + _connection = await ConnectionMultiplexer.ConnectAsync(_options.Configuration).ConfigureAwait(false); + } } else { - _connection = await ConnectionMultiplexer.ConnectAsync(_options.Configuration).ConfigureAwait(false); + _connection = await _options.ConnectionMultiplexerFactory(); } TryRegisterProfiler(); @@ -449,7 +463,7 @@ private void Refresh(string key, DateTimeOffset? absExpr, TimeSpan? sldExpr) options.AbsoluteExpiration.Value, "The absolute expiration value must be in the future."); } - + if (options.AbsoluteExpirationRelativeToNow.HasValue) { return creationTime + options.AbsoluteExpirationRelativeToNow; diff --git a/src/Caching/StackExchangeRedis/src/RedisCacheOptions.cs b/src/Caching/StackExchangeRedis/src/RedisCacheOptions.cs index 3b42427b4120..4ee57d4d666f 100644 --- a/src/Caching/StackExchangeRedis/src/RedisCacheOptions.cs +++ b/src/Caching/StackExchangeRedis/src/RedisCacheOptions.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System; +using System.Threading.Tasks; using Microsoft.Extensions.Options; using StackExchange.Redis; using StackExchange.Redis.Profiling; @@ -17,13 +18,18 @@ public class RedisCacheOptions : IOptions /// The configuration used to connect to Redis. /// public string Configuration { get; set; } - + /// /// The configuration used to connect to Redis. /// This is preferred over Configuration. /// public ConfigurationOptions ConfigurationOptions { get; set; } + /// + /// Gets or sets a delegate to create the ConnectionMultiplexer instance. + /// + public Func> ConnectionMultiplexerFactory { get; set; } + /// /// The Redis instance name. ///