토리맘의 한글라이즈 프로젝트 logo 토리맘의 한글라이즈 프로젝트

resilience4j 공식 레퍼런스를 한글로 번역한 문서입니다.

전체 목차는 여기에 있습니다.

목차


Create and configure Cache

다음 예제에서는 Cache 인터페이스를 통해 람다 표현식을 데코레이팅하는 방법을 보여준다. Cache는 람다 표현식의 결과를 캐시 인스턴스(JCache)에 넣고, 람다 표현식을 실행하기 전에 이전에 캐시된 결과를 조회해본다. 분산 캐시에서 캐시를 조회하지 못하면 예외를 처리하고 람다 표현식을 호출한다.

// Create a CacheContext by wrapping a JCache instance.
javax.cache.Cache<String, String> cacheInstance = Caching
  .getCache("cacheName", String.class, String.class);
Cache<String, String> cacheContext = Cache.of(cacheInstance);

// Decorate your call to BackendService.doSomething()
CheckedFunction1<String, String> cachedFunction = Decorators
    .ofCheckedSupplier(() -> backendService.doSomething())
    .withCache(cacheContext)
    .decorate();
String value = Try.of(() -> cachedFunction.apply("cacheKey")).get();

Consume emitted CacheEvents

Cache는 CacheEvent 스트림을 방출한다. 이벤트는 캐시 히트일 수도 있고, 캐시 미스나 에러일 수도 있다.

cacheContext.getEventPublisher()
    .onCacheHit(event -> logger.info(...))
    .onCacheMiss(event -> logger.info(...))
    .onError(event -> logger.info(...));

Ehcache example

build.gradle

compile 'org.ehcache:ehcache:3.7.1'
// Configure a cache (once)
this.cacheManager = Caching.getCachingProvider().getCacheManager();
this.cache = Cache.of(cacheManager
    .createCache("booksCache", new MutableConfiguration<>()));

// Get books using a cache
List<Book> books = Cache.decorateSupplier(cache, library::getBooks)
    .apply(BOOKS_CACHE_KEY);

JCache Reference Implementation을 사용하면 몇 가지 동시성 문제가 발생할 수 있으므로 프로덕션에선 사용하지 않는 게 좋다. Ehcache, Caffeine, Redisson, Hazelcast, Ignite나 다른 JCache 구현체를 사용해라.


Next :
Kotlin
코틀린 통합 모듈 resilience4j-kotlin 소개

전체 목차는 여기에 있습니다.

<< >>

TOP