首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >缓存运行状况检查Net Core

缓存运行状况检查Net Core
EN

Stack Overflow用户
提问于 2019-02-21 14:12:53
回答 2查看 1.3K关注 0票数 2

Net Core2.2支持为发布的服务执行health checks。我想缓存一个检查的响应。

我看到我可以使用HealthCheckOptions并为AllowCachingResponses属性设置true值。

代码语言:javascript
运行
复制
app.UseHealthChecks("/api/services/healthCheck",
    new Microsoft.AspNetCore.Diagnostics.HealthChecks.HealthCheckOptions()
    {
        AllowCachingResponses = true
    });

但我不明白如何设置时间缓存的数量。设置相应HTTP头(Cache-ControlExpires等)的最佳位置是什么?又是如何做到的?

我的服务由IIS发布。

EN

回答 2

Stack Overflow用户

发布于 2019-08-15 23:19:47

您提到的AllowCachingResponses选项只与HealthCheckMiddleware是否设置HTTP头有关。通常,中间服务器、代理等可以缓存GET请求的结果,并且这些头指示服务器每次都应该重新获取它们。

但是,如果负载均衡器使用这些检查来指示服务是否应该接收更多流量,则它很可能无论如何都不会缓存结果。

为了实现你想要的东西,你需要编写额外的逻辑。一种方法是编写一种HealthCheckCacher类型,如下所示:

代码语言:javascript
运行
复制
public class HealthCheckCacher : IHealthCheck
{
    private readonly SemaphoreSlim _mutex = new SemaphoreSlim(1);
    private readonly IHealthCheck _healthCheck;
    private readonly TimeSpan _timeToLive;

    private HealthCheckResult _result;
    private DateTime _lastCheck;

    public static readonly TimeSpan DefaultTimeToLive = TimeSpan.FromSeconds(30);

    /// <summary>
    /// Creates a new HealthCheckCacher which will cache the result for the amount of time specified.
    /// </summary>
    /// <param name="healthCheck">The underlying health check to perform.</param>
    /// <param name="timeToLive">The amount of time for which the health check should be cached. Defaults to 30 seconds.</param>
    public HealthCheckCacher(IHealthCheck healthCheck, TimeSpan? timeToLive = null)
    {
        _healthCheck = healthCheck;
        _timeToLive = timeToLive ?? DefaultTimeToLive;
    }

    public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
    {
        // you could improve thread concurrency by separating the read/write logic but this would require additional thread safety checks.
        // will throw OperationCanceledException if the token is canceled while we're waiting.
        await _mutex.WaitAsync(cancellationToken);

        try
        {
            // previous check is cached & not yet expired; just return it
            if (_lastCheck > DateTime.MinValue && DateTime.Now - _lastCheck < _timeToLive)
                return _result;

            // check has not been performed or is expired; run it now & cache the result
            _result = await _healthCheck.CheckHealthAsync(context, cancellationToken);
            _lastCheck = DateTime.Now;

            return _result;
        }
        finally
        {
            _mutex.Release();
        }
    }
}
票数 1
EN

Stack Overflow用户

发布于 2021-08-03 05:53:28

我的API中有一个调度的作业,它调用HealthCheckService.CheckHealthAsync()并存储HealthReport结果。然后,我只需创建返回此值的常规API端点。简单得多,不需要人工包装器健康检查。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54800413

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档