From 1907aa06c19168c4f5aeaa84034702675b988319 Mon Sep 17 00:00:00 2001 From: glucaci Date: Thu, 18 Mar 2021 10:40:03 +0200 Subject: [PATCH 1/4] Add option to register profiling session --- src/Caching/StackExchangeRedis/src/RedisCache.cs | 4 ++++ src/Caching/StackExchangeRedis/src/RedisCacheOptions.cs | 9 ++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Caching/StackExchangeRedis/src/RedisCache.cs b/src/Caching/StackExchangeRedis/src/RedisCache.cs index 9604174de237..61f4ef275260 100644 --- a/src/Caching/StackExchangeRedis/src/RedisCache.cs +++ b/src/Caching/StackExchangeRedis/src/RedisCache.cs @@ -194,10 +194,12 @@ private void Connect() if (_options.ConfigurationOptions != null) { _connection = ConnectionMultiplexer.Connect(_options.ConfigurationOptions); + _connection.RegisterProfiler(_options.ProfilingSession); } else { _connection = ConnectionMultiplexer.Connect(_options.Configuration); + _connection.RegisterProfiler(_options.ProfilingSession); } _cache = _connection.GetDatabase(); } @@ -226,10 +228,12 @@ private void Connect() if (_options.ConfigurationOptions != null) { _connection = await ConnectionMultiplexer.ConnectAsync(_options.ConfigurationOptions).ConfigureAwait(false); + _connection.RegisterProfiler(_options.ProfilingSession); } else { _connection = await ConnectionMultiplexer.ConnectAsync(_options.Configuration).ConfigureAwait(false); + _connection.RegisterProfiler(_options.ProfilingSession); } _cache = _connection.GetDatabase(); diff --git a/src/Caching/StackExchangeRedis/src/RedisCacheOptions.cs b/src/Caching/StackExchangeRedis/src/RedisCacheOptions.cs index eb38b92eae31..5bba6f3ddbcd 100644 --- a/src/Caching/StackExchangeRedis/src/RedisCacheOptions.cs +++ b/src/Caching/StackExchangeRedis/src/RedisCacheOptions.cs @@ -2,8 +2,10 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System; using Microsoft.Extensions.Options; using StackExchange.Redis; +using StackExchange.Redis.Profiling; namespace Microsoft.Extensions.Caching.StackExchangeRedis { @@ -28,9 +30,14 @@ public class RedisCacheOptions : IOptions /// public string InstanceName { get; set; } + /// + /// The Redis profiling session + /// + public Func ProfilingSession { get; set; } + RedisCacheOptions IOptions.Value { get { return this; } } } -} \ No newline at end of file +} From 4b9b01246c4cca179f3eec078d8df24180f6680a Mon Sep 17 00:00:00 2001 From: glucaci Date: Thu, 18 Mar 2021 11:31:36 +0200 Subject: [PATCH 2/4] Add API change --- src/Caching/StackExchangeRedis/src/PublicAPI.Unshipped.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Caching/StackExchangeRedis/src/PublicAPI.Unshipped.txt b/src/Caching/StackExchangeRedis/src/PublicAPI.Unshipped.txt index 7dc5c58110bf..eec119508865 100644 --- a/src/Caching/StackExchangeRedis/src/PublicAPI.Unshipped.txt +++ b/src/Caching/StackExchangeRedis/src/PublicAPI.Unshipped.txt @@ -1 +1,3 @@ #nullable enable +~Microsoft.Extensions.Caching.StackExchangeRedis.RedisCacheOptions.ProfilingSession.get -> Func +~Microsoft.Extensions.Caching.StackExchangeRedis.RedisCacheOptions.ProfilingSession.set -> void From c76a6da827c595d2c0a465161235de7c9ce3e242 Mon Sep 17 00:00:00 2001 From: glucaci Date: Thu, 18 Mar 2021 11:34:16 +0200 Subject: [PATCH 3/4] Fix public API declaration --- src/Caching/StackExchangeRedis/src/PublicAPI.Unshipped.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Caching/StackExchangeRedis/src/PublicAPI.Unshipped.txt b/src/Caching/StackExchangeRedis/src/PublicAPI.Unshipped.txt index eec119508865..93b5d12522b9 100644 --- a/src/Caching/StackExchangeRedis/src/PublicAPI.Unshipped.txt +++ b/src/Caching/StackExchangeRedis/src/PublicAPI.Unshipped.txt @@ -1,3 +1,3 @@ #nullable enable -~Microsoft.Extensions.Caching.StackExchangeRedis.RedisCacheOptions.ProfilingSession.get -> Func +~Microsoft.Extensions.Caching.StackExchangeRedis.RedisCacheOptions.ProfilingSession.get -> System.Func ~Microsoft.Extensions.Caching.StackExchangeRedis.RedisCacheOptions.ProfilingSession.set -> void From 1ccd08ccf8eb1b6b22fecaf9d085d993bb507d8a Mon Sep 17 00:00:00 2001 From: glucaci Date: Fri, 19 Mar 2021 23:48:48 +0200 Subject: [PATCH 4/4] Cleanup --- src/Caching/StackExchangeRedis/src/RedisCache.cs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/Caching/StackExchangeRedis/src/RedisCache.cs b/src/Caching/StackExchangeRedis/src/RedisCache.cs index 61f4ef275260..1e7099209bcd 100644 --- a/src/Caching/StackExchangeRedis/src/RedisCache.cs +++ b/src/Caching/StackExchangeRedis/src/RedisCache.cs @@ -194,13 +194,13 @@ private void Connect() if (_options.ConfigurationOptions != null) { _connection = ConnectionMultiplexer.Connect(_options.ConfigurationOptions); - _connection.RegisterProfiler(_options.ProfilingSession); } else { _connection = ConnectionMultiplexer.Connect(_options.Configuration); - _connection.RegisterProfiler(_options.ProfilingSession); } + + TryRegisterProfiler(); _cache = _connection.GetDatabase(); } } @@ -228,14 +228,13 @@ private void Connect() if (_options.ConfigurationOptions != null) { _connection = await ConnectionMultiplexer.ConnectAsync(_options.ConfigurationOptions).ConfigureAwait(false); - _connection.RegisterProfiler(_options.ProfilingSession); } else { _connection = await ConnectionMultiplexer.ConnectAsync(_options.Configuration).ConfigureAwait(false); - _connection.RegisterProfiler(_options.ProfilingSession); } + TryRegisterProfiler(); _cache = _connection.GetDatabase(); } } @@ -245,6 +244,14 @@ private void Connect() } } + private void TryRegisterProfiler() + { + if (_connection != null && _options.ProfilingSession != null) + { + _connection.RegisterProfiler(_options.ProfilingSession); + } + } + private byte[] GetAndRefresh(string key, bool getData) { if (key == null)