-
-
Save maryamariyan/487816f724993c88a39fa29fe397aede to your computer and use it in GitHub Desktop.
Caching demo using metrics
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
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>net6.0</TargetFramework> | |
<ImplicitUsings>enable</ImplicitUsings> | |
<Nullable>enable</Nullable> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="7.0.0-preview.5.22253.2" /> | |
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="7.0.0-preview.5.22253.2" /> | |
</ItemGroup> | |
</Project> |
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
using Microsoft.Extensions.Caching.Memory; | |
using System.Diagnostics.Metrics; | |
class Program | |
{ | |
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); | |
s_meter.CreateObservableGauge<long>("cache-miss", GetCacheMiss); | |
s_meter.CreateObservableGauge<long>("cache-count", GetCurrentEntryCount); | |
s_meter.CreateObservableGauge<long>("cache-size", GetCurrentEstimatedSize); | |
mc1 = new MemoryCache(new MemoryCacheOptions() { TrackStatistics = true, SizeLimit = 30 }); | |
mc2 = new MemoryCache(new MemoryCacheOptions() { TrackStatistics = true, SizeLimit = 30 }); | |
var obj = new object(); | |
string key1 = "myKey1"; | |
string key2 = "myKey2"; | |
using (var entry = mc1.CreateEntry(key1)) | |
{ | |
entry.SetValue(obj); | |
entry.SetSize(10); | |
mc1.Set(key1, obj, new MemoryCacheEntryOptions() { Size = 10 }); | |
} | |
using (var entry = mc2.CreateEntry(key2)) | |
{ | |
entry.SetValue(obj); | |
entry.SetSize(10); | |
mc2.Set(key2, obj, new MemoryCacheEntryOptions() { Size = 10 }); | |
} | |
while (mc1.TryGetValue(key1, out object? value)) | |
{ | |
mc2.TryGetValue("missingKey", out object? value2); | |
mc2.TryGetValue("missingKey", out value2); | |
mc2.TryGetValue("missingKey", out value2); | |
mc2.TryGetValue(key2, out value2); | |
Thread.Sleep(1000); | |
} | |
Console.WriteLine("Press any key to exit"); | |
Console.ReadLine(); | |
} | |
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")), | |
}; | |
} | |
static IEnumerable<Measurement<long>> GetCacheMiss() | |
{ | |
return new Measurement<long>[] | |
{ | |
new Measurement<long>(mc1!.GetCurrentStatistics()!.TotalMisses, new KeyValuePair<string,object?>("CacheName", "mc1")), | |
new Measurement<long>(mc2!.GetCurrentStatistics()!.TotalMisses, new KeyValuePair<string,object?>("CacheName", "mc2")), | |
}; | |
} | |
static IEnumerable<Measurement<long>> GetCurrentEntryCount() | |
{ | |
return new Measurement<long>[] | |
{ | |
new Measurement<long>(mc1!.GetCurrentStatistics()!.CurrentEntryCount, new KeyValuePair<string,object?>("CacheName", "mc1")), | |
new Measurement<long>(mc2!.GetCurrentStatistics()!.CurrentEntryCount, new KeyValuePair<string,object?>("CacheName", "mc2")), | |
}; | |
} | |
static IEnumerable<Measurement<long>> GetCurrentEstimatedSize() | |
{ | |
return new Measurement<long>[] | |
{ | |
new Measurement<long>(mc1!.GetCurrentStatistics()!.CurrentEstimatedSize!.Value, new KeyValuePair<string,object?>("CacheName", "mc1")), | |
new Measurement<long>(mc2!.GetCurrentStatistics()!.CurrentEstimatedSize!.Value, new KeyValuePair<string,object?>("CacheName", "mc2")), | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Gives: