Skip to content

Commit

Permalink
Add HybridCache usage signal, similar to DC (#59543)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgravell authored Jan 15, 2025
1 parent e9d445e commit c4c8d3a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
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;
}
43 changes: 43 additions & 0 deletions src/Caching/StackExchangeRedis/test/CacheServiceExtensionsTests.cs
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();
}
}

0 comments on commit c4c8d3a

Please sign in to comment.