Skip to content

Instantly share code, notes, and snippets.

@maryamariyan
Created May 3, 2022 22:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maryamariyan/487816f724993c88a39fa29fe397aede to your computer and use it in GitHub Desktop.
Save maryamariyan/487816f724993c88a39fa29fe397aede to your computer and use it in GitHub Desktop.
Caching demo using metrics
<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>
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")),
};
}
}
@maryamariyan
Copy link
Author

>  dotnet-counters monitor -n ConsoleApp78 --counters System.Runtime[cpu-usage,working-set],Microsoft.Extensions.Caching.Memory.MemoryCache

Gives:

Press p to pause, r to resume, q to quit.
    Status: Running

[System.Runtime]
    CPU Usage (%)                                      0
    Working Set (MB)                                  14
[Microsoft.Extensions.Caching.Memory.MemoryCache]
    cache-count
        CacheName=mc1                                  1
        CacheName=mc2                                  1
    cache-hits
        CacheName=mc1                             13,204
        CacheName=mc2                             13,204
    cache-miss
        CacheName=mc1                                  0
        CacheName=mc2                             39,612
    cache-size
        CacheName=mc1                                 10
        CacheName=mc2                                 10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment