public class CacheFlux extends Object
Flux in an
arbitrary cache abstraction. A generic writer/reader entry point is provided, but cache
vendors that have a Map wrapper support can also be directly used:
Generic cache entry points:
AtomicReference<Context> storeRef = new AtomicReference<>(Context.empty());
Flux<Integer> cachedFlux = CacheFlux
.lookup(k -> Mono.justOrEmpty(storeRef.get().getOrEmpty(k))
.cast(Integer.class)
.flatMap(max -> Flux.range(1, max)
.materialize()
.collectList()),
key)
.onCacheMissResume(Flux.range(1, 10))
.andWriteWith((k, sigs) -> Flux.fromIterable(sigs)
.dematerialize()
.last()
.doOnNext(max -> storeRef.updateAndGet(ctx -> ctx.put(k, max)))
.then());
Map endpoints:
String key = "myCategory";
LoadingCache<String, Object> graphs = Caffeine
.newBuilder()
.maximumSize(10_000)
.expireAfterWrite(5, TimeUnit.MINUTES)
.refreshAfterWrite(1, TimeUnit.MINUTES)
.build(key -> createExpensiveGraph(key));
Flux<Integer> cachedMyCategory = CacheFlux
.lookup(graphs.asMap(), key, Integer.class)
.onCacheMissResume(repository.findAllByCategory(key));
| Modifier and Type | Class and Description |
|---|---|
static interface |
CacheFlux.FluxCacheBuilderCacheMiss<KEY,VALUE>
Setup original source to fallback to in case of cache miss.
|
static interface |
CacheFlux.FluxCacheBuilderCacheWriter<KEY,VALUE>
Set up the
cache writer BiFunction to use to store the source
data into the cache in case of cache miss. |
static interface |
CacheFlux.FluxCacheBuilderMapMiss<VALUE>
|
| Modifier and Type | Method and Description |
|---|---|
static <KEY,VALUE> |
lookup(java.util.function.Function<KEY,Mono<List<Signal<VALUE>>>> reader,
KEY key)
Restore a
Flux<VALUE> from the cache reader Function
given a provided key. |
static <KEY,VALUE> |
lookup(Map<KEY,? super List> cacheMap,
KEY key,
Class<VALUE> valueClass)
Restore a
Flux<VALUE> from the cache-map given a provided key. |
public static <KEY,VALUE> CacheFlux.FluxCacheBuilderMapMiss<VALUE> lookup(Map<KEY,? super List> cacheMap, KEY key, Class<VALUE> valueClass)
Flux<VALUE> from the cache-map given a provided key.
The cache is expected to store original values as a List of Signal
of T. If no value is in the cache, it will be calculated from the original source
which is set up in the next step. Note that if the source completes empty, this
result will be cached and all subsequent requests with the same key will return
Flux.empty(). The behaviour is similar for erroring sources, except cache
hits would then return Flux.error(Throwable).
Note that the wrapped Flux is lazy, meaning that subscribing twice in a row
to the returned Flux on an empty cache will trigger a cache miss then a
cache hit.
KEY - Key TypeVALUE - Value TypecacheMap - Map wrapper of a cachekey - mapped keybuilder step to use to set up the sourcepublic static <KEY,VALUE> CacheFlux.FluxCacheBuilderCacheMiss<KEY,VALUE> lookup(java.util.function.Function<KEY,Mono<List<Signal<VALUE>>>> reader, KEY key)
Flux<VALUE> from the cache reader Function
given a provided key.
The cache is expected to store original values as a List of Signal
of T. If no value is in the cache, it will be calculated from the original source
which is set up in the next step. Note that if the source completes empty, this
result will be cached and all subsequent requests with the same key will return
Flux.empty(). The behaviour is similar for erroring sources, except cache
hits would then return Flux.error(Throwable).
Note that the wrapped Flux is lazy, meaning that subscribing twice in a row
to the returned Flux on an empty cache will trigger a cache miss then a
cache hit.
KEY - Key TypeVALUE - Value Typereader - a cache reader Function function that looks up collection of Signal from a cachekey - mapped keybuilder step used to set up the source