public class CacheMono extends Object
Mono 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 entry points example:
AtomicReference<Context> storeRef = new AtomicReference<>(Context.empty());
Mono<Integer> cachedMono = CacheMono
.lookup(k -> Mono.justOrEmpty(storeRef.get().<Integer>getOrEmpty(k))
.map(Signal::next),
key)
.onCacheMissResume(Mono.just(123))
.andWriteWith((k, sig) -> Mono.fromRunnable(() ->
storeRef.updateAndGet(ctx -> ctx.put(k, sig.get())));
Map entry points example:
String key = "myId";
LoadingCache<String, Object> graphs = Caffeine
.newBuilder()
.maximumSize(10_000)
.expireAfterWrite(5, TimeUnit.MINUTES)
.refreshAfterWrite(1, TimeUnit.MINUTES)
.build(key -> createExpensiveGraph(key));
Mono<Integer> cachedMyId = CacheMono
.lookup(graphs.asMap(), key)
.onCacheMissResume(repository.findOneById(key));
| Modifier and Type | Class and Description |
|---|---|
static interface |
CacheMono.MonoCacheBuilderCacheMiss<KEY,VALUE>
Setup original source to fallback to in case of cache miss.
|
static interface |
CacheMono.MonoCacheBuilderCacheWriter<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 |
CacheMono.MonoCacheBuilderMapMiss<VALUE>
|
| Modifier and Type | Method and Description |
|---|---|
static <KEY,VALUE> |
lookup(java.util.function.Function<KEY,Mono<Signal<? extends VALUE>>> reader,
KEY key)
Restore a
Mono<VALUE> from the reader Function
(see below) given a provided key. |
static <KEY,VALUE> |
lookup(Map<KEY,? super Signal<? extends VALUE>> cacheMap,
KEY key)
Restore a
Mono<VALUE> from the cache-map given a provided key. |
static <KEY,VALUE> |
lookup(Map<KEY,? super Signal<? extends VALUE>> cacheMap,
KEY key,
Class<VALUE> valueClass)
Restore a
Mono<VALUE> from the cache-map given a provided key. |
public static <KEY,VALUE> CacheMono.MonoCacheBuilderMapMiss<VALUE> lookup(Map<KEY,? super Signal<? extends VALUE>> cacheMap, KEY key)
Mono<VALUE> from the cache-map given a provided key. 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 Mono.empty(). The
behaviour is similar for erroring sources, except cache hits would then return
Mono.error(Throwable).
Note that the wrapped Mono is lazy, meaning that subscribing twice in a row
to the returned Mono on an empty cache will trigger a cache miss then a
cache hit.
For maps that are too generic (eg. Map<String, Object>), a variant is provided
that allows to refine the resulting Mono value type by explicitly providing
a Class.
KEY - Key TypeVALUE - Value TypecacheMap - Map wrapper of a cachekey - mapped keybuilder step to use to set up the sourcelookup(Map, Object, Class)public static <KEY,VALUE> CacheMono.MonoCacheBuilderMapMiss<VALUE> lookup(Map<KEY,? super Signal<? extends VALUE>> cacheMap, KEY key, Class<VALUE> valueClass)
Mono<VALUE> from the cache-map given a provided key. 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 Mono.empty(). The
behaviour is similar for erroring sources, except cache hits would then return
Mono.error(Throwable).
Note that the wrapped Mono is lazy, meaning that subscribing twice in a row
to the returned Mono on an empty cache will trigger a cache miss then a
cache hit.
This variant is for maps that are too generic (eg. Map<String, Object>),
helping the compiler refine the generic typ of the resulting Mono by
explicitly providing a Class. Value stored in the map for the key is
expected to be a Signal of a value of that Class.
KEY - Key TypeVALUE - Value TypecacheMap - Map wrapper of a cachekey - mapped keyvalueClass - the generic Class of the resulting Monobuilder step to use to set up the sourcelookup(Map, Object, Class)public static <KEY,VALUE> CacheMono.MonoCacheBuilderCacheMiss<KEY,VALUE> lookup(java.util.function.Function<KEY,Mono<Signal<? extends VALUE>>> reader, KEY key)
Mono<VALUE> from the reader Function
(see below) given a provided key.
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
Mono.empty(). The behaviour is similar for erroring sources, except cache
hits would then return Mono.error(Throwable).
Note that the wrapped Mono is lazy, meaning that subscribing twice in a row
to the returned Mono on an empty cache will trigger a cache miss then a
cache hit.
The cache reader Function takes a key and returns a Mono
of a Signal representing the value (or Signal.complete() if the
cached Mono was empty). See CacheMono for an example.
KEY - Key TypeVALUE - Value Typereader - a Function that looks up Signal from a cache, returning
them as a Mono<Signal>key - mapped keybuilder step to use to set up the source