/// <summary> /// Creates a new instance. /// </summary> /// <param name="healthCheck">The health check to cache</param> /// <param name="cache">The memory cache</param> /// <param name="properties">The health check properties</param> /// <param name="logger">An optional logger instance</param> /// <exception cref="ArgumentNullException"></exception> public CachedHealthCheck(IHealthCheck healthCheck, IMemoryCache cache, CachedHealthCheckProperties properties, ILogger <CachedHealthCheck> logger = null) { HealthCheck = healthCheck ?? throw new ArgumentNullException(nameof(healthCheck)); Cache = cache ?? throw new ArgumentNullException(nameof(cache)); Properties = properties ?? throw new ArgumentNullException(nameof(properties)); Logger = logger ?? NullLogger <CachedHealthCheck> .Instance; CacheKey = nameof(CachedHealthCheck) + "->" + HealthCheck.Name; }
/// <summary> /// Adds a cache status wrapper to the contained registrations /// </summary> /// <param name="builder">The health check builder</param> /// <param name="cachedBuilder">The builder that will register health checks to be cached</param> /// <param name="properties">The cache properties</param> /// <returns>The builder after changes</returns> /// <exception cref="ArgumentNullException"></exception> public static IHealthCheckBuilder AddCached(this IHealthCheckBuilder builder, Action <IHealthCheckBuilder> cachedBuilder, CachedHealthCheckProperties properties) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } if (cachedBuilder == null) { throw new ArgumentNullException(nameof(cachedBuilder)); } if (properties == null) { throw new ArgumentNullException(nameof(properties)); } var cfg = new HealthCheckBuilder(); cachedBuilder(cfg); foreach (var descriptor in cfg.Descriptors) { builder.Add( p => new CachedHealthCheck( descriptor.Factory(p), p.GetRequiredService <IMemoryCache>(), properties, p.GetService <ILogger <CachedHealthCheck> >()), descriptor.Lifetime); } return(builder); }