From df525cd6b1f6ebd29073cd885ec2191be89cb972 Mon Sep 17 00:00:00 2001 From: SVause Date: Fri, 16 Apr 2021 14:24:45 -0400 Subject: [PATCH 1/4] Added factory func to RedisCacheOptions for setting ConnectionMultiplexer --- .../src/PublicAPI.Shipped.txt | 2 ++ .../StackExchangeRedis/src/RedisCache.cs | 28 ++++++++++++++----- .../src/RedisCacheOptions.cs | 7 ++++- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/Caching/StackExchangeRedis/src/PublicAPI.Shipped.txt b/src/Caching/StackExchangeRedis/src/PublicAPI.Shipped.txt index 634f447d6a20..c102aa71760e 100644 --- a/src/Caching/StackExchangeRedis/src/PublicAPI.Shipped.txt +++ b/src/Caching/StackExchangeRedis/src/PublicAPI.Shipped.txt @@ -19,4 +19,6 @@ Microsoft.Extensions.DependencyInjection.StackExchangeRedisCacheServiceCollectio ~Microsoft.Extensions.Caching.StackExchangeRedis.RedisCacheOptions.ConfigurationOptions.set -> void ~Microsoft.Extensions.Caching.StackExchangeRedis.RedisCacheOptions.InstanceName.get -> string ~Microsoft.Extensions.Caching.StackExchangeRedis.RedisCacheOptions.InstanceName.set -> void +~Microsoft.Extensions.Caching.StackExchangeRedis.RedisCacheOptions.ConnectionMultiplexerFactory.get -> System.Func +~Microsoft.Extensions.Caching.StackExchangeRedis.RedisCacheOptions.ConnectionMultiplexerFactory.set -> void ~static Microsoft.Extensions.DependencyInjection.StackExchangeRedisCacheServiceCollectionExtensions.AddStackExchangeRedisCache(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Action setupAction) -> Microsoft.Extensions.DependencyInjection.IServiceCollection diff --git a/src/Caching/StackExchangeRedis/src/RedisCache.cs b/src/Caching/StackExchangeRedis/src/RedisCache.cs index 25704e1a9218..bee9a52009f7 100644 --- a/src/Caching/StackExchangeRedis/src/RedisCache.cs +++ b/src/Caching/StackExchangeRedis/src/RedisCache.cs @@ -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 != null) + { + _connection = ConnectionMultiplexer.Connect(_options.ConfigurationOptions); + } + else + { + _connection = ConnectionMultiplexer.Connect(_options.Configuration); + } } else { - _connection = ConnectionMultiplexer.Connect(_options.Configuration); + _connection = _options.ConnectionMultiplexerFactory() as ConnectionMultiplexer; } 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 != 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 = _options.ConnectionMultiplexerFactory() as ConnectionMultiplexer; } 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..f0a37d7aab61 100644 --- a/src/Caching/StackExchangeRedis/src/RedisCacheOptions.cs +++ b/src/Caching/StackExchangeRedis/src/RedisCacheOptions.cs @@ -17,13 +17,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. /// From 0a14822e2061302d0df58250695aef291477482c Mon Sep 17 00:00:00 2001 From: SVause Date: Fri, 16 Apr 2021 15:01:30 -0400 Subject: [PATCH 2/4] Wrong PublicAPI file --- src/Caching/StackExchangeRedis/src/PublicAPI.Shipped.txt | 2 -- src/Caching/StackExchangeRedis/src/PublicAPI.Unshipped.txt | 2 ++ 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Caching/StackExchangeRedis/src/PublicAPI.Shipped.txt b/src/Caching/StackExchangeRedis/src/PublicAPI.Shipped.txt index c102aa71760e..634f447d6a20 100644 --- a/src/Caching/StackExchangeRedis/src/PublicAPI.Shipped.txt +++ b/src/Caching/StackExchangeRedis/src/PublicAPI.Shipped.txt @@ -19,6 +19,4 @@ Microsoft.Extensions.DependencyInjection.StackExchangeRedisCacheServiceCollectio ~Microsoft.Extensions.Caching.StackExchangeRedis.RedisCacheOptions.ConfigurationOptions.set -> void ~Microsoft.Extensions.Caching.StackExchangeRedis.RedisCacheOptions.InstanceName.get -> string ~Microsoft.Extensions.Caching.StackExchangeRedis.RedisCacheOptions.InstanceName.set -> void -~Microsoft.Extensions.Caching.StackExchangeRedis.RedisCacheOptions.ConnectionMultiplexerFactory.get -> System.Func -~Microsoft.Extensions.Caching.StackExchangeRedis.RedisCacheOptions.ConnectionMultiplexerFactory.set -> void ~static Microsoft.Extensions.DependencyInjection.StackExchangeRedisCacheServiceCollectionExtensions.AddStackExchangeRedisCache(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Action setupAction) -> Microsoft.Extensions.DependencyInjection.IServiceCollection diff --git a/src/Caching/StackExchangeRedis/src/PublicAPI.Unshipped.txt b/src/Caching/StackExchangeRedis/src/PublicAPI.Unshipped.txt index 93b5d12522b9..a24a45bc88d6 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 From b1dcb3e5c8ccdebc2b07fbda41ed90cb73063b7f Mon Sep 17 00:00:00 2001 From: Shawn Vause Date: Mon, 26 Apr 2021 14:08:40 -0400 Subject: [PATCH 3/4] Update is not null check per feedback Co-authored-by: Pranav K --- src/Caching/StackExchangeRedis/src/RedisCache.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Caching/StackExchangeRedis/src/RedisCache.cs b/src/Caching/StackExchangeRedis/src/RedisCache.cs index bee9a52009f7..8e0da0dbf1bc 100644 --- a/src/Caching/StackExchangeRedis/src/RedisCache.cs +++ b/src/Caching/StackExchangeRedis/src/RedisCache.cs @@ -192,7 +192,7 @@ private void Connect() { if(_options.ConnectionMultiplexerFactory == null) { - if (_options.ConfigurationOptions != null) + if (_options.ConfigurationOptions is not null) { _connection = ConnectionMultiplexer.Connect(_options.ConfigurationOptions); } From 6639fdbe4cf1d5fb683554a3d5416e464faa1c0f Mon Sep 17 00:00:00 2001 From: SVause Date: Tue, 27 Apr 2021 00:12:55 -0400 Subject: [PATCH 4/4] Code review feedback --- .../StackExchangeRedis/src/PublicAPI.Unshipped.txt | 2 +- src/Caching/StackExchangeRedis/src/RedisCache.cs | 8 ++++---- src/Caching/StackExchangeRedis/src/RedisCacheOptions.cs | 3 ++- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/Caching/StackExchangeRedis/src/PublicAPI.Unshipped.txt b/src/Caching/StackExchangeRedis/src/PublicAPI.Unshipped.txt index a24a45bc88d6..1312348c8fb3 100644 --- a/src/Caching/StackExchangeRedis/src/PublicAPI.Unshipped.txt +++ b/src/Caching/StackExchangeRedis/src/PublicAPI.Unshipped.txt @@ -1,5 +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.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 8e0da0dbf1bc..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; @@ -203,7 +203,7 @@ private void Connect() } else { - _connection = _options.ConnectionMultiplexerFactory() as ConnectionMultiplexer; + _connection = _options.ConnectionMultiplexerFactory().GetAwaiter().GetResult(); } TryRegisterProfiler(); @@ -233,7 +233,7 @@ private void Connect() { if(_options.ConnectionMultiplexerFactory == null) { - if (_options.ConfigurationOptions != null) + if (_options.ConfigurationOptions is not null) { _connection = await ConnectionMultiplexer.ConnectAsync(_options.ConfigurationOptions).ConfigureAwait(false); } @@ -244,7 +244,7 @@ private void Connect() } else { - _connection = _options.ConnectionMultiplexerFactory() as ConnectionMultiplexer; + _connection = await _options.ConnectionMultiplexerFactory(); } TryRegisterProfiler(); diff --git a/src/Caching/StackExchangeRedis/src/RedisCacheOptions.cs b/src/Caching/StackExchangeRedis/src/RedisCacheOptions.cs index f0a37d7aab61..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; @@ -27,7 +28,7 @@ public class RedisCacheOptions : IOptions /// /// Gets or sets a delegate to create the ConnectionMultiplexer instance. /// - public Func ConnectionMultiplexerFactory { get; set; } + public Func> ConnectionMultiplexerFactory { get; set; } /// /// The Redis instance name.