Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Repository files navigation

Simple Cache Wrapper

A simple implementations of a cache wrapper:

  • cache-wrapper-reactive-* (for reactive implementations)

Examples:

Cache wrapper (reactive)

pom.xml:

    <dependency>
      <groupId>com.github.elgleidson</groupId>
      <artifactId>cache-wrapper-reactive-redis</artifactId>
    </dependency>

MyService:

@Service
public class MyService {

  private final ReactiveCacheWrapper<String, MyResult> myResultReactiveCache;

  @Autowire
  public MyService(ReactiveCacheWrapperRedisFactory reactiveCacheWrapperRedisFactory) {
    this.myResultReactiveCache = reactiveCacheWrapperRedisFactory.create(MyResult.class);
  }

  public Mono<MyResult> doSomething() {
    return myResultReactiveCache.wrap("my-key", Duration.ofHours(1), () -> {
      return myHeavyMethod();
    });
  }

  private Mono<MyResult> myHeavyMethod() {
    // do something heavy: CPU / IO / third-party call... 
  }
}

Or if you want to cache manually:

  public Mono<MyResult> doSomething() {
    return myResultReactiveCache.getFromCache("my-key")
      .switchIfEmpty(Mono.defer(() -> myHeavyMethod()
        .flatMap(myResult -> myResultReactiveCache.cache("my-key", Duration.ofHours(1), myResult).thenReturn(myResult))
      ));
  }

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors