|  | 
| 1 | 1 | // Licensed to the .NET Foundation under one or more agreements. | 
| 2 | 2 | // The .NET Foundation licenses this file to you under the MIT license. | 
| 3 | 3 | 
 | 
|  | 4 | +using System; | 
|  | 5 | +using System.Collections.Generic; | 
| 4 | 6 | using System.Linq; | 
|  | 7 | +using System.Threading; | 
|  | 8 | +using System.Threading.Tasks; | 
| 5 | 9 | using Microsoft.Extensions.Caching.Distributed; | 
|  | 10 | +using Microsoft.Extensions.Caching.Hybrid; | 
| 6 | 11 | using Microsoft.Extensions.DependencyInjection; | 
|  | 12 | +using Microsoft.Extensions.DependencyInjection.Extensions; | 
| 7 | 13 | using Microsoft.Extensions.Logging; | 
| 8 | 14 | using Microsoft.Extensions.Logging.Abstractions; | 
| 9 | 15 | using Moq; | 
| @@ -121,4 +127,41 @@ public void AddStackExchangeRedisCache_UsesLoggerFactoryAlreadyRegisteredWithSer | 
| 121 | 127 | 
 | 
| 122 | 128 |         loggerFactory.Verify(); | 
| 123 | 129 |     } | 
|  | 130 | + | 
|  | 131 | +    [Theory] | 
|  | 132 | +    [InlineData(true)] | 
|  | 133 | +    [InlineData(false)] | 
|  | 134 | +    public void AddStackExchangeRedisCache_HybridCacheDetected(bool hybridCacheActive) | 
|  | 135 | +    { | 
|  | 136 | +        // Arrange | 
|  | 137 | +        var services = new ServiceCollection(); | 
|  | 138 | + | 
|  | 139 | +        services.AddLogging(); | 
|  | 140 | + | 
|  | 141 | +        // Act | 
|  | 142 | +        services.AddStackExchangeRedisCache(options => { }); | 
|  | 143 | +        if (hybridCacheActive) | 
|  | 144 | +        { | 
|  | 145 | +            services.TryAddSingleton<HybridCache>(new DummyHybridCache()); | 
|  | 146 | +        } | 
|  | 147 | + | 
|  | 148 | +        using var provider = services.BuildServiceProvider(); | 
|  | 149 | +        var cache = Assert.IsAssignableFrom<RedisCache>(provider.GetRequiredService<IDistributedCache>()); | 
|  | 150 | +        Assert.Equal(hybridCacheActive, cache.HybridCacheActive); | 
|  | 151 | +    } | 
|  | 152 | + | 
|  | 153 | +    sealed class DummyHybridCache : HybridCache | 
|  | 154 | +    { | 
|  | 155 | +        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) | 
|  | 156 | +            => throw new NotSupportedException(); | 
|  | 157 | + | 
|  | 158 | +        public override ValueTask RemoveAsync(string key, CancellationToken cancellationToken = default) | 
|  | 159 | +            => throw new NotSupportedException(); | 
|  | 160 | + | 
|  | 161 | +        public override ValueTask RemoveByTagAsync(string tag, CancellationToken cancellationToken = default) | 
|  | 162 | +            => throw new NotSupportedException(); | 
|  | 163 | + | 
|  | 164 | +        public override ValueTask SetAsync<T>(string key, T value, HybridCacheEntryOptions options = null, IEnumerable<string> tags = null, CancellationToken cancellationToken = default) | 
|  | 165 | +            => throw new NotSupportedException(); | 
|  | 166 | +    } | 
| 124 | 167 | } | 
0 commit comments