Class CacheHealthIndicator

java.lang.Object
cloud.opencode.base.cache.spring.CacheHealthIndicator

public class CacheHealthIndicator extends Object
Cache Health Indicator for Spring Boot Actuator 缓存健康指示器,用于 Spring Boot Actuator

Provides health status and detailed metrics for all registered caches. Integrates with Spring Boot Actuator for monitoring and alerting.

为所有注册的缓存提供健康状态和详细指标。 与 Spring Boot Actuator 集成,用于监控和告警。

Features | 主要功能:

  • Overall cache health status - 整体缓存健康状态
  • Per-cache detailed metrics - 每个缓存的详细指标
  • Hit rate threshold alerting - 命中率阈值告警
  • Memory pressure detection - 内存压力检测

Health Status Rules | 健康状态规则:

  • UP: All caches healthy, hit rate above threshold
  • DEGRADED: Some caches below hit rate threshold or high eviction
  • DOWN: Cache manager unavailable or critical errors

Usage with Spring Boot | Spring Boot 使用:

// Auto-configured when spring-boot-actuator is on classpath
// Access at: GET /actuator/health/cache

// Manual configuration:
@Bean
public CacheHealthIndicator cacheHealthIndicator(CacheManager cacheManager) {
    return new CacheHealthIndicator(cacheManager)
        .hitRateThreshold(0.5)
        .evictionRateThreshold(0.3);
}

Response Example | 响应示例:

{
  "status": "UP",
  "details": {
    "cacheCount": 3,
    "totalEntries": 15000,
    "overallHitRate": 0.85,
    "caches": {
      "users": {
        "status": "UP",
        "size": 5000,
        "hitRate": 0.92,
        "missRate": 0.08,
        "evictionCount": 120
      },
      "products": {...}
    }
  }
}

Usage Examples | 使用示例:

// Create health indicator
CacheHealthIndicator indicator = new CacheHealthIndicator(CacheManager.getInstance());
Map<String, Object> health = indicator.health();

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
  • Null-safe: Yes - 空值安全: 是
Since:
JDK 25, opencode-base-cache V2.0.4
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • CacheHealthIndicator

      public CacheHealthIndicator(CacheManager cacheManager)
      Create health indicator with cache manager 使用缓存管理器创建健康指示器
      Parameters:
      cacheManager - the cache manager | 缓存管理器
  • Method Details

    • hitRateThreshold

      public CacheHealthIndicator hitRateThreshold(double threshold)
      Set hit rate threshold for warnings 设置命中率警告阈值
      Parameters:
      threshold - the threshold (0.0 to 1.0) | 阈值(0.0 到 1.0)
      Returns:
      this indicator | 此指示器
    • evictionRateThreshold

      public CacheHealthIndicator evictionRateThreshold(double threshold)
      Set eviction rate threshold for warnings 设置驱逐率警告阈值
      Parameters:
      threshold - the threshold (0.0 to 1.0) | 阈值(0.0 到 1.0)
      Returns:
      this indicator | 此指示器
    • maxSizeWarningThreshold

      public CacheHealthIndicator maxSizeWarningThreshold(long maxSize)
      Set max size warning threshold 设置最大大小警告阈值
      Parameters:
      maxSize - the max size | 最大大小
      Returns:
      this indicator | 此指示器
    • health

      Get health status 获取健康状态
      Returns:
      health result | 健康结果