Skip to content

Commit d638bba

Browse files
authored
Backport to net8 - Caching: SE.Redis update and fix naming inconsistency (#54413)
* Caching: SE.Redis update and fix naming inconsistency (#54239) * - rev to SE.Redis 2.7.27 - use new AddLibraryNameSuffix API (2.7.27) - do not use ConfigurationOptions.Clone() - impacts key rotation - attach suffix even if ConnectionMultiplexerFactory used (Aspire) * nit extra ;
1 parent ece10b1 commit d638bba

File tree

10 files changed

+47
-33
lines changed

10 files changed

+47
-33
lines changed

eng/Versions.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@
318318
<SeleniumWebDriverVersion>4.17.0</SeleniumWebDriverVersion>
319319
<SerilogExtensionsLoggingVersion>1.4.0</SerilogExtensionsLoggingVersion>
320320
<SerilogSinksFileVersion>4.0.0</SerilogSinksFileVersion>
321-
<StackExchangeRedisVersion>2.6.122</StackExchangeRedisVersion>
321+
<StackExchangeRedisVersion>2.7.27</StackExchangeRedisVersion>
322322
<SystemReactiveLinqVersion>5.0.0</SystemReactiveLinqVersion>
323323
<SwashbuckleAspNetCoreVersion>6.4.0</SwashbuckleAspNetCoreVersion>
324324
<XunitAbstractionsVersion>2.0.3</XunitAbstractionsVersion>

src/Caching/StackExchangeRedis/src/RedisCache.Log.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,8 @@ private static partial class Log
1212
{
1313
[LoggerMessage(1, LogLevel.Warning, "Could not determine the Redis server version. Falling back to use HMSET command instead of HSET.", EventName = "CouldNotDetermineServerVersion")]
1414
public static partial void CouldNotDetermineServerVersion(ILogger logger, Exception exception);
15+
16+
[LoggerMessage(2, LogLevel.Debug, "Unable to add library name suffix.", EventName = "UnableToAddLibraryNameSuffix")]
17+
internal static partial void UnableToAddLibraryNameSuffix(ILogger logger, Exception exception);
1518
}
1619
}

src/Caching/StackExchangeRedis/src/RedisCache.cs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -257,14 +257,7 @@ private IDatabase Connect()
257257
IConnectionMultiplexer connection;
258258
if (_options.ConnectionMultiplexerFactory is null)
259259
{
260-
if (_options.ConfigurationOptions is not null)
261-
{
262-
connection = ConnectionMultiplexer.Connect(_options.ConfigurationOptions);
263-
}
264-
else
265-
{
266-
connection = ConnectionMultiplexer.Connect(_options.Configuration!);
267-
}
260+
connection = ConnectionMultiplexer.Connect(_options.GetConfiguredOptions());
268261
}
269262
else
270263
{
@@ -308,7 +301,7 @@ private async ValueTask<IDatabase> ConnectSlowAsync(CancellationToken token)
308301
IConnectionMultiplexer connection;
309302
if (_options.ConnectionMultiplexerFactory is null)
310303
{
311-
connection = await ConnectionMultiplexer.ConnectAsync(_options.GetConfiguredOptions("asp.net DC")).ConfigureAwait(false);
304+
connection = await ConnectionMultiplexer.ConnectAsync(_options.GetConfiguredOptions()).ConfigureAwait(false);
312305
}
313306
else
314307
{
@@ -332,6 +325,7 @@ private void PrepareConnection(IConnectionMultiplexer connection)
332325
WriteTimeTicks(ref _lastConnectTicks, DateTimeOffset.UtcNow);
333326
ValidateServerFeatures(connection);
334327
TryRegisterProfiler(connection);
328+
TryAddSuffix(connection);
335329
}
336330

337331
private void ValidateServerFeatures(IConnectionMultiplexer connection)
@@ -369,6 +363,19 @@ private void TryRegisterProfiler(IConnectionMultiplexer connection)
369363
}
370364
}
371365

366+
private void TryAddSuffix(IConnectionMultiplexer connection)
367+
{
368+
try
369+
{
370+
connection.AddLibraryNameSuffix("aspnet");
371+
connection.AddLibraryNameSuffix("DC");
372+
}
373+
catch (Exception ex)
374+
{
375+
Log.UnableToAddLibraryNameSuffix(_logger, ex);
376+
}
377+
}
378+
372379
private byte[]? GetAndRefresh(string key, bool getData)
373380
{
374381
ArgumentNullThrowHelper.ThrowIfNull(key);

src/Caching/StackExchangeRedis/src/RedisCacheOptions.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,13 @@ static bool GetDefaultValue() =>
5959
set => _useForceReconnect = value;
6060
}
6161

62-
internal ConfigurationOptions GetConfiguredOptions(string libSuffix)
62+
internal ConfigurationOptions GetConfiguredOptions()
6363
{
64-
var options = ConfigurationOptions?.Clone() ?? ConfigurationOptions.Parse(Configuration!);
64+
var options = ConfigurationOptions ?? ConfigurationOptions.Parse(Configuration!);
6565

6666
// we don't want an initially unavailable server to prevent DI creating the service itself
6767
options.AbortOnConnectFail = false;
6868

69-
if (!string.IsNullOrWhiteSpace(libSuffix))
70-
{
71-
var provider = DefaultOptionsProvider.GetProvider(options.EndPoints);
72-
options.LibraryName = $"{provider.LibraryName} {libSuffix}";
73-
}
7469
return options;
7570
}
7671
}

src/Middleware/Microsoft.AspNetCore.OutputCaching.StackExchangeRedis/src/RedisOutputCacheOptions.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,13 @@ static bool GetDefaultValue() =>
5454
set => _useForceReconnect = value;
5555
}
5656

57-
internal ConfigurationOptions GetConfiguredOptions(string libSuffix)
57+
internal ConfigurationOptions GetConfiguredOptions()
5858
{
59-
var options = ConfigurationOptions?.Clone() ?? ConfigurationOptions.Parse(Configuration!);
59+
var options = ConfigurationOptions ?? ConfigurationOptions.Parse(Configuration!);
6060

6161
// we don't want an initially unavailable server to prevent DI creating the service itself
6262
options.AbortOnConnectFail = false;
6363

64-
if (!string.IsNullOrWhiteSpace(libSuffix))
65-
{
66-
var provider = DefaultOptionsProvider.GetProvider(options.EndPoints);
67-
options.LibraryName = $"{provider.LibraryName} {libSuffix}";
68-
}
6964
return options;
7065
}
7166
}

src/Middleware/Microsoft.AspNetCore.OutputCaching.StackExchangeRedis/src/RedisOutputCacheStore.Log.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,7 @@ internal partial class RedisOutputCacheStore
1414

1515
[LoggerMessage(2, LogLevel.Error, "Fatal error occurred executing redis output-cache GC loop.", EventName = "RedisOutputCacheGCFatalError")]
1616
internal static partial void RedisOutputCacheGCFatalError(ILogger logger, Exception exception);
17+
18+
[LoggerMessage(3, LogLevel.Debug, "Unable to add library name suffix.", EventName = "UnableToAddLibraryNameSuffix")]
19+
internal static partial void UnableToAddLibraryNameSuffix(ILogger logger, Exception exception);
1720
}

src/Middleware/Microsoft.AspNetCore.OutputCaching.StackExchangeRedis/src/RedisOutputCacheStore.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ private async ValueTask<IDatabase> ConnectSlowAsync(CancellationToken token)
332332
IConnectionMultiplexer connection;
333333
if (_options.ConnectionMultiplexerFactory is null)
334334
{
335-
connection = await ConnectionMultiplexer.ConnectAsync(_options.GetConfiguredOptions("asp.net OC")).ConfigureAwait(false);
335+
connection = await ConnectionMultiplexer.ConnectAsync(_options.GetConfiguredOptions()).ConfigureAwait(false);
336336
}
337337
else
338338
{
@@ -415,6 +415,7 @@ private void PrepareConnection(IConnectionMultiplexer connection)
415415
WriteTimeTicks(ref _lastConnectTicks, DateTimeOffset.UtcNow);
416416
ValidateServerFeatures(connection);
417417
TryRegisterProfiler(connection);
418+
TryAddSuffix(connection);
418419
}
419420

420421
private void ValidateServerFeatures(IConnectionMultiplexer connection)
@@ -451,6 +452,19 @@ private void TryRegisterProfiler(IConnectionMultiplexer connection)
451452
}
452453
}
453454

455+
private void TryAddSuffix(IConnectionMultiplexer connection)
456+
{
457+
try
458+
{
459+
connection.AddLibraryNameSuffix("aspnet");
460+
connection.AddLibraryNameSuffix("OC");
461+
}
462+
catch (Exception ex)
463+
{
464+
UnableToAddLibraryNameSuffix(_logger, ex);
465+
}
466+
}
467+
454468
private static void WriteTimeTicks(ref long field, DateTimeOffset value)
455469
{
456470
var ticks = value == DateTimeOffset.MinValue ? 0L : value.UtcTicks;

src/Middleware/Microsoft.AspNetCore.OutputCaching.StackExchangeRedis/test/RedisConnectionFixture.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ public RedisConnectionFixture()
1313
var options = new RedisOutputCacheOptions
1414
{
1515
Configuration = "127.0.0.1:6379", // TODO: CI test config here
16-
}.GetConfiguredOptions("CI test");
16+
}.GetConfiguredOptions();
1717
_muxer = ConnectionMultiplexer.Connect(options);
18+
_muxer.AddLibraryNameSuffix("test");
1819
}
1920

2021
public IDatabase Database => _muxer.GetDatabase();

src/SignalR/server/StackExchangeRedis/src/RedisHubLifetimeManager.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using Microsoft.Extensions.Logging;
1414
using Microsoft.Extensions.Options;
1515
using StackExchange.Redis;
16+
using RedisProtocol = Microsoft.AspNetCore.SignalR.StackExchangeRedis.Internal.RedisProtocol; // to disambiguate from StackExchange.Redis.RedisProtocol
1617

1718
namespace Microsoft.AspNetCore.SignalR.StackExchangeRedis;
1819

src/SignalR/server/StackExchangeRedis/test/TestConnectionMultiplexer.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System;
54
using System.Collections.Concurrent;
6-
using System.Collections.Generic;
7-
using System.Diagnostics;
8-
using System.IO;
95
using System.Net;
106
using System.Reflection;
11-
using System.Threading;
12-
using System.Threading.Tasks;
137
using StackExchange.Redis;
148
using StackExchange.Redis.Maintenance;
159
using StackExchange.Redis.Profiling;
16-
using Xunit;
1710

1811
namespace Microsoft.AspNetCore.SignalR.Tests;
1912

@@ -237,6 +230,8 @@ public IServer[] GetServers()
237230
}
238231

239232
public ValueTask DisposeAsync() => default;
233+
234+
public void AddLibraryNameSuffix(string suffix) { } // don't need to implement
240235
}
241236

242237
public class TestRedisServer

0 commit comments

Comments
 (0)