-
-
Save lgolubyev/6381c91455712323b4e54bc9d43480ad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static Meter s_meter = new Meter("Microsoft.Extensions.Caching.Memory.MemoryCache", "1.0.0"); | |
static IMemoryCache? mc1; | |
static IMemoryCache? mc2; | |
static void Main(string[] args) | |
{ | |
s_meter.CreateObservableGauge<long>("cache-hits", GetCacheHits); | |
mc1 = new MemoryCache(new MemoryCacheOptions() { TrackStatistics = true, SizeLimit = 30 }); | |
mc2 = new MemoryCache(new MemoryCacheOptions() { TrackStatistics = true, SizeLimit = 30 }); | |
// call to: mc1.TryGetValue(key1, out object? value) | |
// or: mc2.TryGetValue(key2, out value2) | |
// increments TotalHits | |
} | |
// metrics callback for cache hits | |
static IEnumerable<Measurement<long>> GetCacheHits() | |
{ | |
return new Measurement<long>[] | |
{ | |
new Measurement<long>(mc1!.GetCurrentStatistics()!.TotalHits, new KeyValuePair<string,object?>("CacheName", "mc1")), | |
new Measurement<long>(mc2!.GetCurrentStatistics()!.TotalHits, new KeyValuePair<string,object?>("CacheName", "mc2")), | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment