CachingHealthIndicator implements HealthIndicator { @Override public void check(HealthIndicatorCallback callback) { CacheEntry cacheEntry = this.cachedHealth; long currentTime = System.nanoTime(); if (currentTime > cacheEntry.getExpirationTime()) { if (busy.compareAndSet(false, true)) { try { delegate.check(h -> { this.cachedHealth = new CacheEntry(currentTime + interval, Health.from(h).withDetail(Health.CACHE_KEY, true).build()); callback.inform(h); }); return; } finally { busy.set(false); } } } callback.inform(cacheEntry.getHealth()); } private  CachingHealthIndicator(HealthIndicator delegate, long interval, TimeUnit units); @Override void check(HealthIndicatorCallback callback); static CachingHealthIndicator wrap(HealthIndicator delegate, long interval, TimeUnit units); String getName();  }
@Test public void testWithoutCaching() { for (int x = 0; x < 10; x++) { testHealthIndicator.check(h -> cachedCount.incrementAndGet()); } assertEquals(10, realCount.get()); assertEquals(10, cachedCount.get()); }