K
- the type of cache keysV
- the type of cache values@PublicSpi public interface ValueCache<K,V>
ValueCache
is used by data loaders that use caching and want a long-lived or external cache
of values. The ValueCache
is used as a place to cache values when they come back from
It differs from CacheMap
which is in fact a cache of promises to values aka CompletableFuture
<V> and it rather suited
to be a wrapper of a long lived or external value cache. CompletableFuture
s cant be easily placed in an external cache
outside the JVM say, hence the need for the ValueCache
.
DataLoader
s use a two stage cache strategy if caching is enabled. If the CacheMap
already has the promise to a value
that is used. If not then the ValueCache
is asked for a value, if it has one then that is returned (and cached as a promise in the CacheMap
.
If there is no value then the key is queued and loaded via the BatchLoader
calls. The returned values will then be stored in
the ValueCache
and the promises to those values are also stored in the CacheMap
.
The default implementation is a no-op store which replies with the key always missing and doesn't
store any actual results. This is to avoid duplicating the stored data between the CacheMap
out of the box.
The API signature uses completable futures because the backing implementation MAY be a remote external cache and hence exceptions may happen in retrieving values.
Modifier and Type | Method and Description |
---|---|
java.util.concurrent.CompletableFuture<java.lang.Void> |
clear()
Clears all entries from the store.
|
static <K,V> ValueCache<K,V> |
defaultValueCache()
Creates a new value cache, using the default no-op implementation.
|
java.util.concurrent.CompletableFuture<java.lang.Void> |
delete(K key)
Deletes the entry with the specified key from the store, if it exists.
|
java.util.concurrent.CompletableFuture<V> |
get(K key)
Gets the specified key from the store.
|
java.util.concurrent.CompletableFuture<V> |
set(K key,
V value)
Stores the value with the specified key, or updates it if the key already exists.
|
static <K,V> ValueCache<K,V> defaultValueCache()
K
- the type of cache keysV
- the type of cache valuesjava.util.concurrent.CompletableFuture<V> get(K key)
DataLoader
to load the key via batch loading
instead.key
- the key to retrievejava.util.concurrent.CompletableFuture<V> set(K key, V value)
key
- the key to storevalue
- the value to storejava.util.concurrent.CompletableFuture<java.lang.Void> delete(K key)
key
- the key to deletejava.util.concurrent.CompletableFuture<java.lang.Void> clear()