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>
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for batch resilient loaderstatic classBuilder for ResilientCacheLoaderstatic classException when bulkhead rejects the requeststatic classException when circuit breaker is openstatic classBase exception for loader failuresstatic classException when load is interruptedstatic classException when load times out -
Method Summary
Modifier and TypeMethodDescriptionstatic <K,V> ResilientCacheLoader.BatchBuilder <K, V> Create builder for batch loader 创建批量加载器构建器static <K,V> ResilientCacheLoader.Builder <K, V> builder()Create builder for single-key loader 创建单键加载器构建器Load value with resilience patterns 使用弹性模式加载值static <K,V> Function <K, V> Wrap a simple loader with default resilience settings 使用默认弹性设置包装简单加载器
-
Method Details
-
builder
Create builder for single-key loader 创建单键加载器构建器- Type Parameters:
K- key type | 键类型V- value type | 值类型- Returns:
- builder | 构建器
-
batchBuilder
Create builder for batch loader 创建批量加载器构建器- Type Parameters:
K- key type | 键类型V- value type | 值类型- Returns:
- batch builder | 批量构建器
-
wrap
-
apply
-
load
-