Skip to content

Commit d2b772b

Browse files
John LuoSVauseShawn Vausepranavkm
authored
Add option to RedisCacheOptions to allow custom creation of IConnectionMultiplexer (#32266)
* Added factory func to RedisCacheOptions for setting ConnectionMultiplexer Co-authored-by: SVause <[email protected]> Co-authored-by: Shawn Vause <[email protected]> Co-authored-by: Pranav K <[email protected]>
1 parent 4629299 commit d2b772b

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
#nullable enable
22
~Microsoft.Extensions.Caching.StackExchangeRedis.RedisCacheOptions.ProfilingSession.get -> System.Func<StackExchange.Redis.Profiling.ProfilingSession>
33
~Microsoft.Extensions.Caching.StackExchangeRedis.RedisCacheOptions.ProfilingSession.set -> void
4+
~Microsoft.Extensions.Caching.StackExchangeRedis.RedisCacheOptions.ConnectionMultiplexerFactory.get -> System.Func<System.Threading.Tasks.Task<StackExchange.Redis.IConnectionMultiplexer>>
5+
~Microsoft.Extensions.Caching.StackExchangeRedis.RedisCacheOptions.ConnectionMultiplexerFactory.set -> void

src/Caching/StackExchangeRedis/src/RedisCache.cs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class RedisCache : IDistributedCache, IDisposable
3333
private const string DataKey = "data";
3434
private const long NotPresent = -1;
3535

36-
private volatile ConnectionMultiplexer _connection;
36+
private volatile IConnectionMultiplexer _connection;
3737
private IDatabase _cache;
3838
private bool _disposed;
3939

@@ -190,13 +190,20 @@ private void Connect()
190190
{
191191
if (_cache == null)
192192
{
193-
if (_options.ConfigurationOptions != null)
193+
if(_options.ConnectionMultiplexerFactory == null)
194194
{
195-
_connection = ConnectionMultiplexer.Connect(_options.ConfigurationOptions);
195+
if (_options.ConfigurationOptions is not null)
196+
{
197+
_connection = ConnectionMultiplexer.Connect(_options.ConfigurationOptions);
198+
}
199+
else
200+
{
201+
_connection = ConnectionMultiplexer.Connect(_options.Configuration);
202+
}
196203
}
197204
else
198205
{
199-
_connection = ConnectionMultiplexer.Connect(_options.Configuration);
206+
_connection = _options.ConnectionMultiplexerFactory().GetAwaiter().GetResult();
200207
}
201208

202209
TryRegisterProfiler();
@@ -224,13 +231,20 @@ private void Connect()
224231
{
225232
if (_cache == null)
226233
{
227-
if (_options.ConfigurationOptions != null)
234+
if(_options.ConnectionMultiplexerFactory is null)
228235
{
229-
_connection = await ConnectionMultiplexer.ConnectAsync(_options.ConfigurationOptions).ConfigureAwait(false);
236+
if (_options.ConfigurationOptions is not null)
237+
{
238+
_connection = await ConnectionMultiplexer.ConnectAsync(_options.ConfigurationOptions).ConfigureAwait(false);
239+
}
240+
else
241+
{
242+
_connection = await ConnectionMultiplexer.ConnectAsync(_options.Configuration).ConfigureAwait(false);
243+
}
230244
}
231245
else
232246
{
233-
_connection = await ConnectionMultiplexer.ConnectAsync(_options.Configuration).ConfigureAwait(false);
247+
_connection = await _options.ConnectionMultiplexerFactory();
234248
}
235249

236250
TryRegisterProfiler();
@@ -449,7 +463,7 @@ private void Refresh(string key, DateTimeOffset? absExpr, TimeSpan? sldExpr)
449463
options.AbsoluteExpiration.Value,
450464
"The absolute expiration value must be in the future.");
451465
}
452-
466+
453467
if (options.AbsoluteExpirationRelativeToNow.HasValue)
454468
{
455469
return creationTime + options.AbsoluteExpirationRelativeToNow;

src/Caching/StackExchangeRedis/src/RedisCacheOptions.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5+
using System.Threading.Tasks;
56
using Microsoft.Extensions.Options;
67
using StackExchange.Redis;
78
using StackExchange.Redis.Profiling;
@@ -17,13 +18,18 @@ public class RedisCacheOptions : IOptions<RedisCacheOptions>
1718
/// The configuration used to connect to Redis.
1819
/// </summary>
1920
public string Configuration { get; set; }
20-
21+
2122
/// <summary>
2223
/// The configuration used to connect to Redis.
2324
/// This is preferred over Configuration.
2425
/// </summary>
2526
public ConfigurationOptions ConfigurationOptions { get; set; }
2627

28+
/// <summary>
29+
/// Gets or sets a delegate to create the ConnectionMultiplexer instance.
30+
/// </summary>
31+
public Func<Task<IConnectionMultiplexer>> ConnectionMultiplexerFactory { get; set; }
32+
2733
/// <summary>
2834
/// The Redis instance name.
2935
/// </summary>

0 commit comments

Comments
 (0)