Class ResilientCacheLoader<K,V>

java.lang.Object
cloud.opencode.base.cache.resilience.ResilientCacheLoader<K,V>
Type Parameters:
K - key type | 键类型
V - value type | 值类型

Features | 主要功能:

  • Retry with configurable backoff - 可配置退避的重试
  • Circuit breaker integration - 熔断器集成
  • Bulkhead concurrency limiting - 舱壁并发限制
  • Timeout support - 超时支持

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
  • Null-safe: Yes - 空值安全: 是
All Implemented Interfaces:
Function<K,V>

public final class ResilientCacheLoader<K,V> extends Object implements Function<K,V>
Resilient Cache Loader - Wraps loaders with retry, circuit breaker, bulkhead, and timeout 弹性缓存加载器 - 用重试、熔断、舱壁和超时包装加载器

Provides comprehensive resilience patterns for cache loading operations:

为缓存加载操作提供全面的弹性模式:

  • Retry - Automatic retry with configurable backoff | 带可配置退避的自动重试
  • Circuit Breaker - Fail fast when backend is down | 后端故障时快速失败
  • Bulkhead - Limit concurrent loads | 限制并发加载
  • Timeout - Fail if load takes too long | 加载超时则失败

Usage Examples | 使用示例:

// Create resilient loader with all patterns
Function<String, User> baseLoader = key -> userService.findById(key);

Function<String, User> resilientLoader = ResilientCacheLoader.<String, User>builder()
    .loader(baseLoader)
    .retry(RetryPolicy.exponentialBackoffWithJitter(3,
        Duration.ofMillis(100), Duration.ofSeconds(5)))
    .circuitBreaker(CircuitBreaker.builder()
        .failureThreshold(5)
        .resetTimeout(Duration.ofSeconds(30))
        .build())
    .bulkhead(Bulkhead.semaphore(10))
    .timeout(Duration.ofSeconds(5))
    .build();

// Use with cache
Cache<String, User> cache = OpenCache.getOrCreate("users");
User user = cache.get("user:1", resilientLoader);

// Batch loader variant
Function<Set<String>, Map<String, User>> batchLoader =
    ResilientCacheLoader.<String, User>batchBuilder()
        .loader(keys -> userService.findAllByIds(keys))
        .retry(RetryPolicy.fixedDelay(2, Duration.ofMillis(200)))
        .build();
Since:
JDK 25, opencode-base-cache V1.9.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • builder

      public static <K,V> ResilientCacheLoader.Builder<K,V> builder()
      Create builder for single-key loader 创建单键加载器构建器
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Returns:
      builder | 构建器
    • batchBuilder

      public static <K,V> ResilientCacheLoader.BatchBuilder<K,V> batchBuilder()
      Create builder for batch loader 创建批量加载器构建器
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Returns:
      batch builder | 批量构建器
    • wrap

      public static <K,V> Function<K,V> wrap(Function<K,V> loader)
      Wrap a simple loader with default resilience settings 使用默认弹性设置包装简单加载器
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      loader - the loader | 加载器
      Returns:
      resilient loader | 弹性加载器
    • apply

      public V apply(K key)
      Specified by:
      apply in interface Function<K,V>
    • load

      public V load(K key)
      Load value with resilience patterns 使用弹性模式加载值
      Parameters:
      key - the key | 键
      Returns:
      loaded value | 加载的值