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

Redis distributed cache: add HybridCache usage signal #59543

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

internal bool HybridCacheActive { get; set; }

// StackExchange.Redis will also be trying to reconnect internally,
// so limit how often we recreate the ConnectionMultiplexer instance
// in an attempt to reconnect
Expand Down Expand Up @@ -375,6 +377,11 @@ private void TryAddSuffix(IConnectionMultiplexer connection)
{
connection.AddLibraryNameSuffix("aspnet");
connection.AddLibraryNameSuffix("DC");

if (HybridCacheActive)
{
connection.AddLibraryNameSuffix("HC");
}
}
catch (Exception ex)
{
Expand Down
13 changes: 11 additions & 2 deletions src/Caching/StackExchangeRedis/src/RedisCacheImpl.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using Microsoft.Extensions.Caching.Hybrid;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace Microsoft.Extensions.Caching.StackExchangeRedis;

internal sealed class RedisCacheImpl : RedisCache
{
public RedisCacheImpl(IOptions<RedisCacheOptions> optionsAccessor, ILogger<RedisCache> logger)
public RedisCacheImpl(IOptions<RedisCacheOptions> optionsAccessor, ILogger<RedisCache> logger, IServiceProvider services)
: base(optionsAccessor, logger)
{
HybridCacheActive = IsHybridCacheDefined(services);
}

public RedisCacheImpl(IOptions<RedisCacheOptions> optionsAccessor)
public RedisCacheImpl(IOptions<RedisCacheOptions> optionsAccessor, IServiceProvider services)
: base(optionsAccessor)
{
HybridCacheActive = IsHybridCacheDefined(services);
}

// 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
@@ -1,9 +1,15 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Hybrid;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
Expand Down Expand Up @@ -121,4 +127,41 @@ public void AddStackExchangeRedisCache_UsesLoggerFactoryAlreadyRegisteredWithSer

loggerFactory.Verify();
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public void AddStackExchangeRedisCache_HybridCacheDetected(bool hybridCacheActive)
{
// Arrange
var services = new ServiceCollection();

services.AddLogging();

// Act
services.AddStackExchangeRedisCache(options => { });
if (hybridCacheActive)
{
services.TryAddSingleton<HybridCache>(new DummyHybridCache());
}

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

sealed class DummyHybridCache : HybridCache
{
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();

public override ValueTask RemoveAsync(string key, CancellationToken cancellationToken = default)
=> throw new NotSupportedException();

public override ValueTask RemoveByTagAsync(string tag, CancellationToken cancellationToken = default)
=> throw new NotSupportedException();

public override ValueTask SetAsync<T>(string key, T value, HybridCacheEntryOptions options = null, IEnumerable<string> tags = null, CancellationToken cancellationToken = default)
=> throw new NotSupportedException();
}
}
Loading