Skip to content

Commit

Permalink
Caching Redis: Add option to register profiling session (#31018)
Browse files Browse the repository at this point in the history
* Add option to register profiling session

* Add API change

* Fix public API declaration

* Cleanup
  • Loading branch information
glucaci authored Mar 20, 2021
1 parent ef0c2fc commit 30f376d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/Caching/StackExchangeRedis/src/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
#nullable enable
~Microsoft.Extensions.Caching.StackExchangeRedis.RedisCacheOptions.ProfilingSession.get -> System.Func<StackExchange.Redis.Profiling.ProfilingSession>
~Microsoft.Extensions.Caching.StackExchangeRedis.RedisCacheOptions.ProfilingSession.set -> void
11 changes: 11 additions & 0 deletions src/Caching/StackExchangeRedis/src/RedisCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ private void Connect()
{
_connection = ConnectionMultiplexer.Connect(_options.Configuration);
}

TryRegisterProfiler();
_cache = _connection.GetDatabase();
}
}
Expand Down Expand Up @@ -232,6 +234,7 @@ private void Connect()
_connection = await ConnectionMultiplexer.ConnectAsync(_options.Configuration).ConfigureAwait(false);
}

TryRegisterProfiler();
_cache = _connection.GetDatabase();
}
}
Expand All @@ -241,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)
Expand Down
9 changes: 8 additions & 1 deletion src/Caching/StackExchangeRedis/src/RedisCacheOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -28,9 +30,14 @@ public class RedisCacheOptions : IOptions<RedisCacheOptions>
/// </summary>
public string InstanceName { get; set; }

/// <summary>
/// The Redis profiling session
/// </summary>
public Func<ProfilingSession> ProfilingSession { get; set; }

RedisCacheOptions IOptions<RedisCacheOptions>.Value
{
get { return this; }
}
}
}
}

0 comments on commit 30f376d

Please sign in to comment.