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 an async
cache store.
It differs from CacheMap
which is in fact a cache of promised values aka CompletableFuture
<V>'s.
ValueCache
is more suited to be a wrapper of a long-lived or externallly cached values. 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 CompletableFuture
s because the backing implementation MAY be a remote external cache
and hence exceptions may happen in retrieving values and they may take time to complete.
Modifier and Type | Method and Description |
---|---|
java.util.concurrent.CompletableFuture<java.lang.Void> |
clear()
Clears all entries from the value cache.
|
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 value cache, if it exists.
|
java.util.concurrent.CompletableFuture<V> |
get(K key)
Gets the specified key from the value cache.
|
default java.util.concurrent.CompletableFuture<java.util.List<Try<V>>> |
getValues(java.util.List<K> keys)
Gets the specified keys from the value cache, in a batch call.
|
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.
|
default java.util.concurrent.CompletableFuture<java.util.List<V>> |
setValues(java.util.List<K> keys,
java.util.List<V> values)
Stores the value with the specified keys, or updates it if the keys if they already exist.
|
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 retrievedefault java.util.concurrent.CompletableFuture<java.util.List<Try<V>>> getValues(java.util.List<K> keys)
get(Object)
for you
Each item in the returned list of values is a Try
. If the key could not be found then a failed Try just be returned otherwise
a successful Try contain the cached value is returned.
You MUST return a List that is the same size as the keys passed in. The code will assert if you do not.
keys
- the list of keys to get cached values for.Try
cached values for each key passed in.java.util.concurrent.CompletableFuture<V> set(K key, V value)
key
- the key to storevalue
- the value to storedefault java.util.concurrent.CompletableFuture<java.util.List<V>> setValues(java.util.List<K> keys, java.util.List<V> values)
set(Object, Object)
for youkeys
- the keys to storevalues
- the values to storejava.util.concurrent.CompletableFuture<java.lang.Void> delete(K key)
NOTE: Your implementation MUST not throw exceptions, rather it should return a CompletableFuture that has completed exceptionally. Failure
to do this may cause the DataLoader
code to not run properly.
key
- the key to deletejava.util.concurrent.CompletableFuture<java.lang.Void> clear()
NOTE: Your implementation MUST not throw exceptions, rather it should return a CompletableFuture that has completed exceptionally. Failure
to do this may cause the DataLoader
code to not run properly.