Skip to content

Commit

Permalink
Adds AddStatisticsTrackingMemoryCache extension method
Browse files Browse the repository at this point in the history
  • Loading branch information
maryamariyan committed Apr 8, 2022
1 parent ff6abfe commit 3d4f4d0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,6 @@ public static partial class MemoryCacheServiceCollectionExtensions
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddDistributedMemoryCache(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Action<Microsoft.Extensions.Caching.Memory.MemoryDistributedCacheOptions> setupAction) { throw null; }
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddMemoryCache(this Microsoft.Extensions.DependencyInjection.IServiceCollection services) { throw null; }
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddMemoryCache(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, System.Action<Microsoft.Extensions.Caching.Memory.MemoryCacheOptions> setupAction) { throw null; }
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddStatisticsTrackingMemoryCache(this Microsoft.Extensions.DependencyInjection.IServiceCollection services) { throw null; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ public static IServiceCollection AddMemoryCache(this IServiceCollection services
return services;
}

/// <summary>
/// Adds a non distributed in memory implementation of <see cref="IMemoryCache"/> to the
/// <see cref="IServiceCollection" />.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
public static IServiceCollection AddStatisticsTrackingMemoryCache(this IServiceCollection services!!)
{
return services.AddMemoryCache(o => o.TrackStatistics = true);
}

/// <summary>
/// Adds a non distributed in memory implementation of <see cref="IMemoryCache"/> to the
/// <see cref="IServiceCollection" />.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ public void AddMemoryCache_RegistersMemoryCacheAsSingleton()
Assert.Equal(ServiceLifetime.Singleton, memoryCache.Lifetime);
}

#if NET6_0_OR_GREATER
[Fact]
public void AddStatisticsTrackingMemoryCache_RegistersMemoryCacheAsWithTrackingEnabled()
{
var memoryCache = new ServiceCollection()
.AddStatisticsTrackingMemoryCache()
.BuildServiceProvider()
.GetRequiredService<IMemoryCache>();

Assert.IsType<MemoryCache>(memoryCache);
Assert.NotNull(memoryCache);
Assert.NotNull(memoryCache.GetCurrentStatistics());
}
#endif

[Fact]
public void AddDistributedMemoryCache_DoesntConflictWithMemoryCache()
{
Expand Down

0 comments on commit 3d4f4d0

Please sign in to comment.