-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Add extension method to help set DI injectable MemoryCache
with enabled statistics tracking ability
#67769
Comments
Tagging subscribers to this area: @dotnet/area-extensions-caching Issue DetailsWith #50406, we added The new In this issue we would want to add an extension method using namespace Microsoft.Extensions.DependencyInjection
{
public static partial class MemoryCacheServiceCollectionExtensions
{
public IServiceCollection AddDistributedMemoryCache(this IServiceCollection services, Action<MemoryDistributedCacheOptions> setupAction) { }
public static IServiceCollection AddMemoryCache(this IServiceCollection services) { }
public static IServiceCollection AddMemoryCache(this IServiceCollection services, Action<MemoryCacheOptions> setupAction) { }
+ public static IServiceCollection AddStatisticsTrackingMemoryCache(this IServiceCollection services) { }
}
} Alternative designWe could make the default behaviour for both existing AddMemoryCache(this IServiceCollection services)
{
services.AddOptions();
services.TryAdd(ServiceDesciptor.Singleon<IMemoryCache, MemoryCache>());
services.Configure(opt => { opt.TrackStatistics=true; });
// when a Name API is available we'd also set a default name for this cache
return services;
} Benefit with this design it that it helps take zero code changes/no rebuild/no redeploy for devs to get stats on the cache created by The downside of this alternative design is that it adds an element of surprise to the There is prior precedence in extensions libraries, e.g. with GoalThis issue is part of multi step process to provide a great user experience for tracking statistics for memory cache.
|
I think adding a new API largely defeats the purpose. The goal is that users will be able to get the stats for the cache created by AddMemoryCache() without needing to modify their code. Creating a new API and asking users to use it changes this back into an explicit opt-in that will be easy for users to miss until the day comes they want to see those stats. At that point it is generally very inconvenient to rebuild and redeploy their application with changes to enable these stats. |
I agree with @noahfalk. Since this issue has received 0 comments or upvotes from our customers I am going to close the issue. Thanks! |
With #50406, we added
IMemoryCache.GetCurrentStatistics()
API, which helps us track statistics such as cache hits, misses, cache count and size forIMemoryCache
. This capability is turned off by default.The new
GetCurrentStatistics()
API allows app developers to use either event counters or metrics APIs to track statistics with code snippets illustrated in this gist.In this issue we would want to add an extension method using
ServiceCollection
to add aMemoryCache
with statistics tracking enabled.namespace Microsoft.Extensions.DependencyInjection { public static partial class MemoryCacheServiceCollectionExtensions { public IServiceCollection AddDistributedMemoryCache(this IServiceCollection services, Action<MemoryDistributedCacheOptions> setupAction) { } public static IServiceCollection AddMemoryCache(this IServiceCollection services) { } public static IServiceCollection AddMemoryCache(this IServiceCollection services, Action<MemoryCacheOptions> setupAction) { } + public static IServiceCollection AddStatisticsTrackingMemoryCache(this IServiceCollection services) { } } }
Alternative design
We could make the default behaviour for both existing
AddMemoryCache
API overloads to always configureMemoryCacheOptions
withTrackStatistics
set to enabled by default:Benefit with this design it that it helps take zero code changes/no rebuild/no redeploy for devs to get stats on the cache created by
AddMemoryCache()
in dotnet-counters.The downside of this alternative design is that it adds an element of surprise to the
AddMemoryCache
API for those who assumed it should configure defaultMemoryCacheOptions
, by default settingTrackStatistics
bool flag to false.There is prior precedence in extensions libraries, e.g. with
AddConsole
andAddSimpleConsole
/AddJsonConsole
etc. whereAddConsole
configures options with default value but e.g.AddSimpleConsole
would configure similarly but also set formatter name tosimple
.Goal
This issue is part of multi step process to provide a great user experience for tracking statistics for memory cache.
AddStatisticsTrackingMemoryCache()
in M.E.C orAddMemoryCache
given that its default behaviour has tracking enabled.For more information refer to #66479 (comment)
cc @noahfalk @davidfowl
The text was updated successfully, but these errors were encountered: