Skip to content
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

[release/9.0] Caching SERedis critical bugfix; defer HC metadata detection because of DI cycle #60916

Merged
merged 1 commit into from
Apr 9, 2025
Merged
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
4 changes: 2 additions & 2 deletions src/Caching/StackExchangeRedis/src/RedisCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private static RedisValue[] GetHashFields(bool getData) => getData
private long _firstErrorTimeTicks;
private long _previousErrorTimeTicks;

internal bool HybridCacheActive { get; set; }
internal virtual bool IsHybridCacheActive() => false;

// StackExchange.Redis will also be trying to reconnect internally,
// so limit how often we recreate the ConnectionMultiplexer instance
Expand Down Expand Up @@ -378,7 +378,7 @@ private void TryAddSuffix(IConnectionMultiplexer connection)
connection.AddLibraryNameSuffix("aspnet");
connection.AddLibraryNameSuffix("DC");

if (HybridCacheActive)
if (IsHybridCacheActive())
{
connection.AddLibraryNameSuffix("HC");
}
Expand Down
13 changes: 7 additions & 6 deletions src/Caching/StackExchangeRedis/src/RedisCacheImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,20 @@ namespace Microsoft.Extensions.Caching.StackExchangeRedis;

internal sealed class RedisCacheImpl : RedisCache
{
private readonly IServiceProvider _services;

internal override bool IsHybridCacheActive()
=> _services.GetService<HybridCache>() is not null;

public RedisCacheImpl(IOptions<RedisCacheOptions> optionsAccessor, ILogger<RedisCache> logger, IServiceProvider services)
: base(optionsAccessor, logger)
{
HybridCacheActive = IsHybridCacheDefined(services);
_services = services; // important: do not check for HybridCache here due to dependency - creates a cycle
}

public RedisCacheImpl(IOptions<RedisCacheOptions> optionsAccessor, IServiceProvider services)
: base(optionsAccessor)
{
HybridCacheActive = IsHybridCacheDefined(services);
_services = services; // important: do not check for HybridCache here due to dependency - creates a cycle
}

// HybridCache optionally uses IDistributedCache; if we're here, then *we are* the DC
private static bool IsHybridCacheDefined(IServiceProvider services)
=> services.GetService<HybridCache>() is not null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Hybrid;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Moq;
using Xunit;

Expand Down Expand Up @@ -142,16 +144,46 @@ public void AddStackExchangeRedisCache_HybridCacheDetected(bool hybridCacheActiv
services.AddStackExchangeRedisCache(options => { });
if (hybridCacheActive)
{
services.TryAddSingleton<HybridCache>(new DummyHybridCache());
services.AddMemoryCache();
services.TryAddSingleton<HybridCache, DummyHybridCache>();
}

using var provider = services.BuildServiceProvider();
var cache = Assert.IsAssignableFrom<RedisCache>(provider.GetRequiredService<IDistributedCache>());
Assert.Equal(hybridCacheActive, cache.HybridCacheActive);
Assert.Equal(hybridCacheActive, cache.IsHybridCacheActive());
}

sealed class DummyHybridCache : HybridCache
{
// emulate the layout from HybridCache in dotnet/extensions
public DummyHybridCache(IOptions<HybridCacheOptions> options, IServiceProvider services)
{
if (services is null)
{
throw new ArgumentNullException(nameof(services));
}

var l1 = services.GetRequiredService<IMemoryCache>();
_ = options.Value;
var logger = services.GetService<ILoggerFactory>()?.CreateLogger(typeof(HybridCache)) ?? NullLogger.Instance;
// var clock = services.GetService<TimeProvider>() ?? TimeProvider.System;
var l2 = services.GetService<IDistributedCache>(); // note optional

// ignore L2 if it is really just the same L1, wrapped
// (note not just an "is" test; if someone has a custom subclass, who knows what it does?)
if (l2 is not null
&& l2.GetType() == typeof(MemoryDistributedCache)
&& l1.GetType() == typeof(MemoryCache))
{
l2 = null;
}

IHybridCacheSerializerFactory[] factories = services.GetServices<IHybridCacheSerializerFactory>().ToArray();
Array.Reverse(factories);
}

public class HybridCacheOptions { }

public override ValueTask<T> GetOrCreateAsync<TState, T>(string key, TState state, Func<TState, CancellationToken, ValueTask<T>> factory, HybridCacheEntryOptions options = null, IEnumerable<string> tags = null, CancellationToken cancellationToken = default)
=> throw new NotSupportedException();

Expand Down
Loading