Skip to content

RedisCacheOptions to Manually Set ConnectionMultiplexer #31879

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Caching/StackExchangeRedis/src/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#nullable enable
~Microsoft.Extensions.Caching.StackExchangeRedis.RedisCacheOptions.ProfilingSession.get -> System.Func<StackExchange.Redis.Profiling.ProfilingSession>
~Microsoft.Extensions.Caching.StackExchangeRedis.RedisCacheOptions.ProfilingSession.set -> void
~Microsoft.Extensions.Caching.StackExchangeRedis.RedisCacheOptions.ConnectionMultiplexerFactory.get -> System.Func<System.Threading.Tasks.Task<StackExchange.Redis.IConnectionMultiplexer>>
~Microsoft.Extensions.Caching.StackExchangeRedis.RedisCacheOptions.ConnectionMultiplexerFactory.set -> void
30 changes: 22 additions & 8 deletions src/Caching/StackExchangeRedis/src/RedisCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 7 additions & 1 deletion src/Caching/StackExchangeRedis/src/RedisCacheOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,13 +18,18 @@ public class RedisCacheOptions : IOptions<RedisCacheOptions>
/// The configuration used to connect to Redis.
/// </summary>
public string Configuration { get; set; }

/// <summary>
/// The configuration used to connect to Redis.
/// This is preferred over Configuration.
/// </summary>
public ConfigurationOptions ConfigurationOptions { get; set; }

/// <summary>
/// Gets or sets a delegate to create the ConnectionMultiplexer instance.
/// </summary>
public Func<Task<IConnectionMultiplexer>> ConnectionMultiplexerFactory { get; set; }

/// <summary>
/// The Redis instance name.
/// </summary>
Expand Down