org.zodiac.cache.reactive.ReactiveCache Maven / Gradle / Ivy
The newest version!
package org.zodiac.cache.reactive;
import org.reactivestreams.Publisher;
import reactor.cache.CacheFlux;
import reactor.cache.CacheMono;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
public interface ReactiveCache {
Flux getFlux(Object key);
Mono getMono(Object key);
Mono put(Object key, Publisher data);
Mono evict(Object key);
Flux getAll(Object... keys);
Mono evictAll(Iterable> key);
Mono clear();
default CacheFlux.FluxCacheBuilderMapMiss flux(Object key) {
return otherSupplier ->
Flux.defer(() ->
getFlux(key)
.switchIfEmpty(otherSupplier.get()
.collectList()
.flatMapMany(values -> put(key, Flux.fromIterable(values))
.thenMany(Flux.fromIterable(values)))));
}
default CacheMono.MonoCacheBuilderMapMiss mono(Object key) {
return otherSupplier ->
Mono.defer(() -> getMono(key)
.switchIfEmpty(otherSupplier.get()
.flatMap(value -> put(key, Mono.just(value)).thenReturn(value))));
}
}