-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryCacheHealthChecker.cs
More file actions
35 lines (27 loc) · 1.21 KB
/
MemoryCacheHealthChecker.cs
File metadata and controls
35 lines (27 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Diagnostics.HealthChecks;
namespace Netcorext.Diagnostics.HealthChecks.MemoryCache;
public class MemoryCacheHealthChecker : IHealthCheck
{
private readonly MemoryCacheHealthCheckerOptions _options;
private readonly IMemoryCache _memoryCache;
public MemoryCacheHealthChecker(MemoryCacheHealthCheckerOptions options, IMemoryCache memoryCache)
{
_options = options;
_memoryCache = memoryCache;
}
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = new CancellationToken())
{
if (_options.CheckKeys == null || _options.CheckKeys.Count == 0)
return HealthCheckResult.Healthy();
var healthy = true;
var result = new Dictionary<string, object>();
foreach (var check in _options.CheckKeys)
{
if (!_memoryCache.TryGetValue<int>(check.Key, out var cacheCount) || cacheCount < check.Value)
healthy = false;
result.Add(check.Key, cacheCount);
}
return healthy ? HealthCheckResult.Healthy(data: result) : HealthCheckResult.Unhealthy(data: result);
}
}