Repository navigation
Expand file tree
/
Copy pathUnitTestExExtensions.Caching.cs
More file actions
32 lines (27 loc) · 1.95 KB
/
Copy pathUnitTestExExtensions.Caching.cs
File metadata and controls
32 lines (27 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#pragma warning disable IDE0130 // Namespace does not match folder structure; by design.
namespace UnitTestEx;
#pragma warning restore IDE0130 // Namespace does not match folder structure
public static partial class UnitTestExExtensions
{
/// <summary>
/// Clears the underlying L1/L2 cache by executing the <see cref="ZiggyCreatures.Caching.Fusion.IFusionCache.ClearAsync(bool, ZiggyCreatures.Caching.Fusion.FusionCacheEntryOptions?, CancellationToken)"/>.
/// </summary>
/// <param name="tester">The <see cref="TesterBase"/>.</param>
/// <remarks>The <see cref="ZiggyCreatures.Caching.Fusion.IFusionCache"/> service must be registered within the underlying test host.</remarks>
public static async Task ClearFusionCacheAsync(this TesterBase tester) => await tester.ThrowIfNull().Services.GetRequiredService<ZiggyCreatures.Caching.Fusion.IFusionCache>().ClearAsync(false);
/// <summary>
/// Clears the underlying Redis cache by executing the <see cref="StackExchange.Redis.IServer.FlushDatabaseAsync"/> operation.
/// </summary>
/// <param name="app">The <see cref="DistributedApplication"/>.</param>
/// <param name="aspireResourceName">The name of the Aspire resource to retrieve the connection string for.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
public static async Task ClearRedisCacheAsync(this DistributedApplication app, string aspireResourceName)
{
var cs = (await AspireTesterBase.GetConnectionStringAsync(app.ThrowIfNull(), aspireResourceName.ThrowIfNullOrEmpty()).ConfigureAwait(false)) ?? throw new InvalidOperationException($"The '{aspireResourceName}' connection string not found.");
var options = ConfigurationOptions.Parse(cs);
options.AllowAdmin = true;
await using var mux = await ConnectionMultiplexer.ConnectAsync(options);
foreach (var ep in mux.GetEndPoints())
await mux.GetServer(ep).FlushDatabaseAsync();
}
}